Home
 
     

Google geolocation API in C# for finding latitude and longitude for dynamic Google maps in asp.net

Mar 08 2013

Well, today I was working with Google maps. I have more than 300,000 addresses and I needed to add dynamic Google maps to a website , I was looking around a little bit. At first I thought I have to use API 3 - with a limitation of 25,000 requests and that is a payed system-. Since, there are many misleading answers on the forums to this questions, I want to emphasize that API 3 is not for people who only want to add dynamic maps to a website !

Generate Map Onfly Google    
There is a free solution for that! I found this Google Maps API V3 for ASP.NET at Codeproject. It is a very fast way of learning how to put things in order (I did not even read it I just took a look at the JavaScript code ! ) and to be honest I don't understand the reason of putting this article in Asp.net category. It is only simple JavaScript, so you can use it in PHP or even simple HTML ! The problem is if you have the address but not latitude and longitude you can not still really use it, so I found this GeoCoding with C# and Google Geocoding API V3 . They described actually how you can get the information of an address using API for free. a sample would be http://maps.googleapis.com/maps/api/geocode/xml?address=Duettgatan+3+Karlstad+65636+Sweden &sensor=false and it gives you the information of the address including latitude and longitude! try it it is a XML ! try your address it is actually pretty much smart and flexible ! The rest was piece of cake; writing a class to take the address and give you the latitude and longitude so you can use it in the map ! here is the class I made ! Note : If you are using this c# code add a reference to the name space (Rooznamechi.Codes) at top of your codes so it works! using Rooznamechi.Codes ; Also lots of credit goes to mentioned article above, I just trimmed it reshaped it as a class and posted it !

using System; 
using System.Net; 
using System.Xml.XPath; 
namespace Rooznamechi.Codes {
 public class GoogleGeocode { 
public static string[] getLatLng(string adress) {
 string[] resuts = new string[] {"0","0"}; 
string url = "http://maps.googleapis.com/maps/api/geocode/" + "xml?address=" + adress + "&sensor=false";
 WebResponse response = null;
 try { 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET"; 
response = request.GetResponse();
 if (response != null) {
 XPathDocument document = new XPathDocument(response.GetResponseStream()); 
XPathNavigator navigator = document.CreateNavigator(); 
// get response 
status XPathNodeIterator statusIterator = navigator.Select("/GeocodeResponse/status"); 
while (statusIterator.MoveNext()) { 
if (statusIterator.Current.Value != "OK") { 
return resuts; } } 
// get results 
XPathNodeIterator resultIterator = navigator.Select("/GeocodeResponse/result");
 if (resultIterator.MoveNext()) { XPathNodeIterator geometryIterator = resultIterator.Current.Select("geometry");
 if (geometryIterator.MoveNext()) { XPathNodeIterator locationIterator = geometryIterator.Current.Select("location"); 
if (locationIterator.MoveNext()) { XPathNodeIterator latIterator = locationIterator.Current.Select("lat");
 if (latIterator.MoveNext()) { resuts[0] = latIterator.Current.Value; 
} XPathNodeIterator lngIterator = locationIterator.Current.Select("lng");
 if (lngIterator.MoveNext()) { resuts[1] = lngIterator.Current.Value; 
} 
}
 }
 }
 } 
return resuts; 
} catch (Exception ex) { 
//handle Error your way 
return resuts; 
} 
}
 }
 } 

Privacy Statement Terms Of Use Copyright 2011 by Rooznamechi