2010-08-18 115 views
1

我目前正在創建一個房地產經紀人網站與搜索返回一組屬性存儲在數據庫中。谷歌地圖Geocoder請求

現在我知道谷歌每天有多少請求的限制,所以我試圖想到最好的方式來做這個過程。

基本上目前,第一步的搜索是輸入一個城鎮或郵政編碼,我將發送一個請求到谷歌地圖API獲取這些經緯度,並將它們存儲在數據庫中未來如果他們還沒有在那裏使用。

你認爲這樣可以嗎?或者有更好的解決方案呢?

謝謝。

+0

這是我的代碼: <?php $ code ='NE1 1RJ'; $ mapsApiKey ='your-google-maps-api-key'; $ query =「http://maps.google.co.uk/maps/geo?q=".urlencode($code)."&output=json&key=".$mapsApiKey; $ data = file_get_contents($ query); if($ data){data = json_decode($ data); $ long = $ data-> Placemark [0] - > Point-> coordinates [0]; $ lat = $ data->地標[0] - >點 - >座標[1]; echo $ long; $ lat; } else {return false; } ?> 我打算在數據庫中存儲經緯度和緯度以備將來使用... – Ashley 2010-08-18 09:12:55

回答

0

只需從用戶瀏覽器運行代碼 - API使用次數將計入用戶的IP地址。

+0

您確定這是否違反IP地址?我似乎記得我們碰到了API密鑰的這個限制* - 我們最初的實現是非常天真的,每個用戶的瀏覽器從他們自己的IP發出了大約10個請求。我們在幾個小時內開始擊中「OVER_QUERY_LIMIT」(我們通過短時間緩存結果解決了這個問題,大多數人都在請求相同的對象)。這是前段時間在v2和文檔模糊在這裏,請參閱http://code.google.com/apis/maps/documentation/geocoding/#Limits – Piskvor 2010-08-18 09:00:02

+0

嗨, 那麼我目前有這樣的: <? php $ code ='NE1 1RJ'; $ mapsApiKey ='your-google-maps-api-key'; $ query =「http://maps.google.co.uk/maps/geo?q=".urlencode($code)."&output=json&key=」。$ mapsApiKey; $ data = file_get_contents($ query); if($ data){//將其轉換爲可讀格式 $ data = json_decode($ data); $ long = $ data-> Placemark [0] - > Point-> coordinates [0]; $ lat = $ data->地標[0] - >點 - >座標[1]; echo $ long; echo $ lat; } else { return false; } ?> 這樣會好嗎?謝謝。 – Ashley 2010-08-18 09:01:01

+0

@Piskvor:常見問題解答如此:http://code.google.com/intl/zh-CN/apis/maps/faq.html – 2010-08-18 14:14:36

3

看看這個網站。它的工作非常適合我: http://www.geonames.org/export/ws-overview.html

我只用函數 「findNearbyStreets」(http://www.geonames.org/maps/us-reverse-geocoder.html#findNearbyStreets),這是我的代碼:

<?php 
$sFile = "list.csv"; 
$sContent = file_get_contents($sFile); 

$aRows = explode("\n", $sContent); 
$sEcho = ""; 
$i  = 1; 

echo "Total rows: " . count($aRows) . "\n===============\n\n"; 

foreach ($aRows as $sRow) 
{ 
    $aCols = explode(",", $sRow); 

    $sLongitude = $aCols[0]; 
    $sLatitude = $aCols[1]; 
    $sInfo = ucfirst($aCols[2]); 
    $sInfo = str_replace('"', '', $sInfo); 

    $url = "http://ws.geonames.org/findNearbyStreetsOSMJSON?lat=$sLatitude&lng=$sLongitude"; 
    $json = file_get_contents($url); 
    $data = json_decode($json, true); 

    $street = $data['streetSegment'][0]['name']; 

    $sEcho .= "Some text"; 

    echo "Done with row: $i/".count($aRows)." \n"; 
    $i++; 
} 

$rFile = fopen("newFile.txt", "w+"); 
fwrite($rFile, $sEcho); 
fclose($rFile); 
?> 

的問候,保羅

+0

只是爲了澄清:我寫的腳本是爲shell使用而編寫的,但也可以用於webusage(只是刪除echo的;)) – 2010-08-18 09:22:19

1

你的方法看起來不錯 - 在我認爲;)

你甚至可以存儲每個屬性的座標,例如「Barb Foo街123 Foo街是35.7,-111.0」,只有在數據庫中沒有他們時纔可以在地理編碼器中查找它們。

+0

是的,這就是我要做的事情,所以加班時間會少一些,減少了對谷歌的要求,但我仍然關注最初的發佈...... – Ashley 2010-08-18 09:10:13

+0

好吧,如果你在正式發佈前擁有數據,你可以與他們建立數據庫 - 查找最可能的城市,以防萬一以後超過極限,只提供城市位置。 – Piskvor 2010-08-18 09:25:25