[Back]

// (c) Donn Felker 
// http://blog.donnfelker.com/post/HOWTO-Build-a-Store-Locator-in-ASPNET.aspx
// http://codeplex.com/StoreLocator 
//
// DotNetNuke® - http://www.dotnetnuke.com
// Copyright (c) 2002-2009
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;

namespace AdefWebserver.Modules.StoreLocator
{
    public class GoogleGeoCoder
    {
        #region ISpatialCoordinate
        public interface ISpatialCoordinate
        {
            decimal Latitude { get; set; }
            decimal Longitude { get; set; }
        } 
        #endregion

        #region Coordinate
        /// <summary>
        /// Coordiate structure. Holds Latitude and Longitude.
        /// </summary>
        public struct Coordinate : ISpatialCoordinate
        {
            private decimal _latitude;
            private decimal _longitude;

            public Coordinate(decimal latitude, decimal longitude)
            {
                _latitude = latitude;
                _longitude = longitude;
            }

            #region ISpatialCoordinate Members

            public decimal Latitude
            {
                get
                {
                    return _latitude;
                }
                set
                {
                    this._latitude = value;
                }
            }

            public decimal Longitude
            {
                get
                {
                    return _longitude;
                }
                set
                {
                    this._longitude = value;
                }
            }

            #endregion
        } 
        #endregion

        #region Geocode
        public class Geocode
        {
            private const string _googleUri = "http://maps.google.com/maps/geo?q=";
            private const string _outputType = "csv"; // Available options: csv, xml, kml, json

            private static Uri GetGeocodeUri(string address, string googleApiKey)
            {
                address = HttpUtility.UrlEncode(address);
                return new Uri(String.Format("{0}{1}&output={2}&key={3}", _googleUri, address, _outputType, googleApiKey));
            }

            public static Coordinate GetCoordinates(string address, string googleApiKey)
            {
                Coordinate objCoordinate;
                WebClient client = new WebClient();
                Uri uri = GetGeocodeUri(address, googleApiKey);

                /*  The first number is the status code, 
                 * the second is the accuracy, 
                 * the third is the latitude, 
                 * the fourth one is the longitude.
                 */

                string[] geocodeInfo = client.DownloadString(uri).Split(',');

                if (Convert.ToDecimal(geocodeInfo[2]) > 0)
                {
                    objCoordinate = new Coordinate(Convert.ToDecimal(geocodeInfo[2]), Convert.ToDecimal(geocodeInfo[3]));
                }
                else
                {
                    objCoordinate = new Coordinate(Convert.ToDecimal(0), Convert.ToDecimal(0));
                }

                return objCoordinate;
            }
        } 
        #endregion
    }
}

Buy DotNetNuke Modules from Snowcovered

 DotNetNuke Powered!DotNetNuke is a registered trademark of DotNetNuke Corporation.