Friday, 5 July 2013

Get location (Latitude & Longitude) for given address in ASP.NET using google api

Get latitude and longitude using C# for given location

C#.NET:

public static List<string> GetLatLng(string address)
{
 dynamic requestUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=true", Uri.EscapeDataString(address));
 dynamic request = WebRequest.Create(requestUri);
 dynamic response = request.GetResponse();
 dynamic xdoc = XDocument.Load(response.GetResponseStream());
 dynamic latLngArray = new List<string>();
 dynamic xElement = xdoc.Element("GeocodeResponse");
 if (xElement != null) {
  dynamic result = xElement.Element("result");
  if (result != null) {
   dynamic element = result.Element("geometry");
   if (element != null) {
    dynamic locationElement = element.Element("location");
    if (locationElement != null) {
     dynamic xElement1 = locationElement.Element("lat");
     if (xElement1 != null) {
      dynamic lat = xElement1.Value;
      latLngArray.Add(lat);
     }
     dynamic element1 = locationElement.Element("lng");
     if (element1 != null) {
      dynamic lng = element1.Value;
      latLngArray.Add(lng);
     }
    }
   }
  }
 }
 return latLngArray;
}



Get latitude and longitude using VB.NET  for given location

VB.NET:


Public Shared Function GetLatLng(address As String) As List(Of String)
 Dim requestUri = String.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&sensor=true", Uri.EscapeDataString(address))
 Dim request = WebRequest.Create(requestUri)
 Dim response = request.GetResponse()
 Dim xdoc = XDocument.Load(response.GetResponseStream())
 Dim latLngArray = New List(Of String)()
 Dim xElement = xdoc.Element("GeocodeResponse")
 If xElement IsNot Nothing Then
  Dim result = xElement.Element("result")
  If result IsNot Nothing Then
   Dim element = result.Element("geometry")
   If element IsNot Nothing Then
    Dim locationElement = element.Element("location")
    If locationElement IsNot Nothing Then
     Dim xElement1 = locationElement.Element("lat")
     If xElement1 IsNot Nothing Then
      Dim lat = xElement1.Value
      latLngArray.Add(lat)
     End If
     Dim element1 = locationElement.Element("lng")
     If element1 IsNot Nothing Then
      Dim lng = element1.Value
      latLngArray.Add(lng)
     End If
    End If
   End If
  End If
 End If
 Return latLngArray
End Function

No comments:

Post a Comment

Convert SVG to PNG using svg.dll

      <div id="divsvg">        <svg width="350" height="300"><defs></defs> ............