[英]Parse Parse to get Latitude and Longitude of the Google Geocoding API using C #
[英]Get the latitude and longitude of multiple addresses using Google API
我有一個包含大約 16000 條記錄的數據庫,我喜歡遍歷每個地址並獲取它們的緯度和經度值。
我嘗試將所有記錄導出到 excel 工作表中,然后創建一個宏來獲得 lat 和 lang 值。 它適用於 80% 的地址,但 google map api 會返回比 Bing map 更多的結果,因為我在 google map 中嘗試了很少的地址(這些地址不能與 bing map 一起使用),而 google 正在返回准確的值。
我喜歡使用 Google Map API 來獲取緯度和經度值,因為它每天的請求限制為 25k。
我有這個運行良好的 java 腳本,但我不知道如何將它與多個地址一起使用? 我可以遍歷數據集,但不確定我是否需要在代碼后面的頁面中針對每個地址調用這個 java 腳本函數?
做這個的最好方式是什么?
<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>
您需要為每個地址調用此函數,然后存儲結果的 long 和 lat。 您還需要限制請求(可能使用 setTimeout()),因為 API 也有速率限制。 您每秒只能發出這么多請求。
請記住,地理編碼服務結果的存儲和后續顯示違反了 Google 的TOS 。 根據第 10.1.3 c 節,您將在沒有付費訂閱的情況下違反許可協議。
(c) 不得大量下載或批量提供內容。 您不得以使您或任何其他人訪問任何內容的大量下載或批量提要的方式使用服務,包括但不限於數字緯度或經度坐標、圖像、可見地圖數據或地點數據(包括商業清單)。 例如,您不得提供使用 Maps API 中包含的內容的批量地理編碼服務。
我認為 16k 坐標肯定會算作批量。
谷歌地理編碼的使用限制是 2,500(不是你所說的 25k)。
Google Geocoding API 有以下限制:每 24 小時 2,500 個請求。
Google Maps API for Business 客戶有更高的限制:每 24 小時 100,000 個請求。
這是我關於SO的問題。
您的 JavaScript 代碼不會計入 2,500 個限制,因為它是在客戶端計算機上運行的。
但是,如果您從后面的代碼中使用地理編碼 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);
}
我有一個包含大約 16000 條記錄的數據庫,我喜歡遍歷每個地址並獲取它們的緯度和經度值。
最簡單的方法是使用 WebClient 和 Geocode 2,500/days 並將它們保存在數據庫中。
我們可以使用以下代碼直接在 c# 中對地址進行地理編碼。 循環遍歷excel記錄並將地址轉換為以下格式“1600+Amphitheatre+Parkway,+Mountain+View,+CA”。
示例代碼:根據您的需要更改此代碼
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;
}
}
}
}
}
}
注意: Google 對地理請求/天有限制。 檢查此鏈接以了解使用限制和計費 - https://developers.google.com/maps/documentation/javascript/usage?hl=en 。 根據您的需要聯系谷歌。
還要添加 1 或 2 毫秒的線程睡眠,以確保我們不會超載谷歌服務器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.