Monday 24 October 2011

Find Latitude And Longitude From C# Asp.net


using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.Web.UI;

namespace GoogleGeocoder
{
   public interface ISpatialCoordinate
   {
      decimal Latitude {get; set; }
      decimal Longitude {get; set; }
   }
   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
   }

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

      private static Uri GetGeocodeUri(string address)
      {
         address = HttpUtility.UrlEncode(address);
         return new Uri(String.Format("{0}{1}&output={2}&key={3}", _googleUri, address, _outputType, _googleKey));
      }
      public static Coordinate GetCoordinates(string address)
      {
         WebClient client = new WebClient();
         Uri uri = GetGeocodeUri(address);
         string[] geocodeInfo = client.DownloadString(uri).Split(',');
         return new Coordinate(Convert.ToDecimal(geocodeInfo[2]), Convert.ToDecimal(geocodeInfo[3]));
      }

   }
}
------------------------------------
using GoogleGeocoder;

Coordinate coordinate = Geocode.GetCoordinates("limkon");
decimal latitude = coordinate.Latitude;
decimal longitude = coordinate.Longitude;

No comments:

Post a Comment

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...