简体   繁体   English

使用 Google API 获取多个地址的经纬度

[英]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.我有一个包含大约 16000 条记录的数据库,我喜欢遍历每个地址并获取它们的纬度和经度值。

I have tried exporting all records into excel sheet and then creat a macro to have a lat and lang values.我尝试将所有记录导出到 excel 工作表中,然后创建一个宏来获得 lat 和 lang 值。 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.它适用于 80% 的地址,但 google map api 会返回比 Bing map 更多的结果,因为我在 google map 中尝试了很少的地址(这些地址不能与 bing map 一起使用),而 google 正在返回准确的值。

I like to use Google Map API to get latitude and longitude values as it has a limit of 25k requests per day.我喜欢使用 Google Map API 来获取纬度和经度值,因为它每天的请求限制为 25k。

I have got this java script which works fine but i don't know how can i use it with multiple addresses?我有这个运行良好的 java 脚本,但我不知道如何将它与多个地址一起使用? 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?我可以遍历数据集,但不确定我是否需要在代码后面的页面中针对每个地址调用这个 java 脚本函数?

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.您需要为每个地址调用此函数,然后存储结果的 long 和 lat。 You would also need to throttle the requests (possibly using setTimeout()) as the API also has a rate limit .您还需要限制请求(可能使用 setTimeout()),因为 API 也有速率限制 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 .请记住,地理编码服务结果的存储和后续显示违反了 Google 的TOS You will be breaking the licence agreement without a paid subscription, under section 10.1.3 c.根据第 10.1.3 c 节,您将在没有付费订阅的情况下违反许可协议。

(c) No Mass Downloads or Bulk Feeds of Content. (c) 不得大量下载或批量提供内容。 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).例如,您不得提供使用 Maps API 中包含的内容的批量地理编码服务。

I think 16k coordinates will certainly count as bulk.我认为 16k 坐标肯定会算作批量。

Google Usage Limits for Geocoding is 2,500 (Not 25k as you stated). 谷歌地理编码的使用限制是 2,500(不是你所说的 25k)。

The Google Geocoding API has the following limits in place: 2,500 requests per 24 hour period. Google Geocoding API 有以下限制:每 24 小时 2,500 个请求。

Google Maps API for Business customers have higher limits: 100,000 requests per 24 hour period. Google Maps API for Business 客户有更高的限制:每 24 小时 100,000 个请求。

Here is my question regarding that in SO.这是关于SO的问题。

Your JavaScript code won't be counted in 2,500 limit, because it is run on client machine.您的 JavaScript 代码不会计入 2,500 个限制,因为它是在客户端计算机上运行的。

However, if you geocode using Geocoding API from code behind, then it counts in 2,500 limit.但是,如果您从后面的代码中使用地理编码 API 进行地理编码,则它计入 2,500 个限制。

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.我有一个包含大约 16000 条记录的数据库,我喜欢遍历每个地址并获取它们的纬度和经度值。

Easiest way will be to use WebClient, and Geocode 2,500/days and save them in database.最简单的方法是使用 WebClient 和 Geocode 2,500/days 并将它们保存在数据库中。

We can geocode the addresses directly in c# using the following code.我们可以使用以下代码直接在 c# 中对地址进行地理编码。 Loop through excel records and convert addresses to the following format "1600+Amphitheatre+Parkway,+Mountain+View,+CA".循环遍历excel记录并将地址转换为以下格式“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.注意: Google 对地理请求/天有限制。 Check this link for usage limits and billing - https://developers.google.com/maps/documentation/javascript/usage?hl=en .检查此链接以了解使用限制和计费 - 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.还要添加 1 或 2 毫秒的线程睡眠,以确保我们不会超载谷歌服务器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 解析解析以使用C#获取Google Geocoding API的纬度和经度 - Parse Parse to get Latitude and Longitude of the Google Geocoding API using C # Google API获取与C#中的纬度和经度相对应的地址 - Google API to get address corresponding to Latitude and Longitude in C# Google Geolocation API - 使用经度和纬度在文本框中获取地址? - Google Geolocation API - Use longitude and latitude to get address in textbox? 将整数经度和纬度转换为十进制(用于Google Maps API) - Convert integer latitude and longitude to decimal (using for google maps api) 问题使用Google API获取给定地址的经度和纬度 - Issue getting latitude and longitude of a given address using google api 如何在 c# 中动态地将多个纬度和经度传递给谷歌距离矩阵 api? - how to dynamically pass multiple latitude and longitude to google distance matrix api in c#? 从谷歌地图v3方向api的结果中获取经度和纬度? - Get Latitude and Longitude from result of google map v3 direction api? 如何在gmap中使用经度和纬度获取地址? - How to get the address using Latitude and Longitude with gmap? 使用经度和纬度获取地址和地名 - Get address and place name using latitude and longitude 使用NodaTime获取纬度和经度的国家/地区代码 - Get country code by latitude and longitude using NodaTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM