简体   繁体   中英

Get the latitude and longitude of multiple addresses using Google API

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>

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM