2

I have a database of around 16000 records and i like to go through each address and get their latitude and longitude values.

I have tried exporting all records into excel sheet and then creat a macro to have a lat and lang values. It works for 80% of addresses, but google map api would return more results than Bing map as i have tried few addresses (which were not working with bing map) in google map and google is returning accurate values.

I like to use Google Map API to get latitude and longitude values as it has a limit of 25k requests per day.

I have got this java script which works fine but i don't know how can i use it with multiple addresses? I can loop through dataset but not sure if i need to call this java script function in the code behind page against every single address?

What is the best way to do this?

<script type="text/javascript">
<!--
    function GetLocation() {
        var geocoder = new google.maps.Geocoder();
        var address = document.getElementById("txtAddress").value;
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                alert("Latitude: " + latitude + "\nLongitude: " + longitude);
            } else {
                alert("Request failed.")
            }
        });
    };
    //-->
</script>
user1263981
  • 2,953
  • 8
  • 57
  • 98

3 Answers3

0

You would need to call this function for each address and then store the resultant long and lat. You would also need to throttle the requests (possibly using setTimeout()) as the API also has a rate limit. You can only make so many requests per second.

Bear in mind though that the storing and subsequent display of results from the geocoding service is against Google's TOS. You will be breaking the licence agreement without a paid subscription, under section 10.1.3 c.

(c) No Mass Downloads or Bulk Feeds of Content. You must not use the Service in a manner that gives you or any other person access to mass downloads or bulk feeds of any Content, including but not limited to numerical latitude or longitude coordinates, imagery, visible map data, or places data (including business listings). For example, you are not permitted to offer a batch geocoding service that uses Content contained in the Maps API(s).

I think 16k coordinates will certainly count as bulk.

Community
  • 1
  • 1
ChrisSwires
  • 2,713
  • 1
  • 15
  • 28
0

Google Usage Limits for Geocoding is 2,500 (Not 25k as you stated).

The Google Geocoding API has the following limits in place: 2,500 requests per 24 hour period.

Google Maps API for Business customers have higher limits: 100,000 requests per 24 hour period.

Here is my question regarding that in SO.

Your JavaScript code won't be counted in 2,500 limit, because it is run on client machine.

However, if you geocode using Geocoding API from code behind, then it counts in 2,500 limit.

protected void Button1_Click(object sender, EventArgs e)
{
    var uri = "http://maps.googleapis.com/maps/api/geocode/json?address=dallas&sensor=false";
    var result = new WebClient().DownloadString(uri);
}

Answer to your question

I have a database of around 16000 records and i like to go through each address and get their latitude and longitude values.

Easiest way will be to use WebClient, and Geocode 2,500/days and save them in database.

Community
  • 1
  • 1
Win
  • 61,100
  • 13
  • 102
  • 181
  • •Google Geocoding Service: 2,500 requests per day •Google Maps Javascript API : up to 25,000 map loads per day for each service. – user1263981 Jan 03 '14 at 15:40
  • So you're saying if you execute all of your geocoding requests in javascript within a browser then there is effectively no API limitation on number (only frequency)? Or that it's 2.5k per client? If the latter then I don't think it makes I difference to the question. I believe the intention was to execute from a single client regardless. – ChrisSwires Jan 03 '14 at 15:42
  • ***go through each address and get their latitude and longitude values*** is geoencoding (it is not map loading). You want to use **WebClient** and geoencode those 16000 address via **GeoEncode API** from code behind. – Win Jan 03 '14 at 15:44
  • @user1263981 We spoke with Google on the phone already. Business license costs ***$17,000 per year***. Here is my update for the [my question](http://stackoverflow.com/a/18410658/296861). You do not have a lot of records, so just run 2,500 per day for 7 days and you'll be fine. – Win Jan 03 '14 at 15:52
  • @Win Again. Just to clarify. This is against the TOS of the API and they may ban you. As is publishing the price (something which Google made us aware of us when we paid for a clients licence). – ChrisSwires Jan 03 '14 at 15:56
  • According to **[Before You Begin](https://developers.google.com/maps/documentation/geocoding/#BYB)**, Google even suggests to ***pre-geocode known addresses*** and ***store your results***. It basically let you store the encoded latitute and longitude for your addresses in advance. However, 2,500 is not a lot if your website is really busy one; Google'll block your IP address if you reach cap for the day. We reached the cap few times, so we end up creating our own GoeEncoding API (which is out of the scope of OP). – Win Jan 03 '14 at 16:11
  • @Win, can you suggest any other API's close to google map in terms of accuracy. Yahoo API limit is 5k per day. – user1263981 Jan 03 '14 at 16:17
  • @user1263981 Sorry, I'm not familiar with other APIs. According to your scenario, Google's GeoEncoding API should be fine for your need, since you only have 16,000 records. *Please do not forget to upvote answers; they are very informative and deserve a round of applause.* – Win Jan 03 '14 at 16:25
-1

We can geocode the addresses directly in c# using the following code. Loop through excel records and convert addresses to the following format "1600+Amphitheatre+Parkway,+Mountain+View,+CA".

Sample Code: Change this code as per your needs

string url = "http://maps.googleapis.com/maps/api/geocode/" + "xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false";

WebResponse response = null;
bool is_geocoded = true;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
response = request.GetResponse();
string lat = "";
string lng = "";
string loc_type = "";
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")
        {
            is_geocoded = false;
        }
    }

    // get results
    if (is_geocoded)
    {
        XPathNodeIterator resultIterator = navigator.Select("/GeocodeResponse/result");
        while (resultIterator.MoveNext())
        {


            XPathNodeIterator geometryIterator = resultIterator.Current.Select("geometry");
            while (geometryIterator.MoveNext())
            {
                XPathNodeIterator locationIterator = geometryIterator.Current.Select("location");
                while (locationIterator.MoveNext())
                {
                    XPathNodeIterator latIterator = locationIterator.Current.Select("lat");
                    while (latIterator.MoveNext())
                    {
                        lat = latIterator.Current.Value;
                    }

                    XPathNodeIterator lngIterator = locationIterator.Current.Select("lng");
                    while (lngIterator.MoveNext())
                    {
                        lng = lngIterator.Current.Value;

                    }
                    XPathNodeIterator locationTypeIterator = geometryIterator.Current.Select("location_type");
                    while (locationTypeIterator.MoveNext())
                    {
                        loc_type = locationTypeIterator.Current.Value;
                    }
                }

            }
        }
    }
}

Note: Google has limitations on Geo requests/Day. Check this link for usage limits and billing - https://developers.google.com/maps/documentation/javascript/usage?hl=en . Contact google based on your needs.

Also add Thread sleep for 1 or 2 ms just to make sure we are not overloading the google servers.

i3arnon
  • 113,022
  • 33
  • 324
  • 344
Jasti
  • 927
  • 5
  • 14
  • Thanks for the example. I know how to do it from code behind page but i thought can make 25k request in one day just to validate old addresses. Now as it is clear that limit is only 2500 per day, i think we will look into business license. We just need to validate 16k addresses for one time, after that 2500 would be more then enough for us. – user1263981 Jan 03 '14 at 15:50