2010-04-12 48 views
10

我有我的大的數據庫,包含了像SL5 9JH值郵政編碼傳遞到郵政編碼maps.google.com,LU1 3TQ取緯度經度通過使用Javascript

現在,當我在上面粘貼到郵政編碼maps.google.com它指向一個完美的位置..

我的要求就像我想通過郵政編碼maps.google.com,它應該返回相關的緯度和經度的指出位置,我想存儲在我的數據庫中。

所以,最有可能應該有一些JavaScript爲...如果任何人有關於該請提供它的另一個想法..

在此先感謝...

回答

10

您描述的過程的技術術語稱爲reverse geocoding。 Google提供 The Google Geocoding Web Service New working Google Geocoding Link,您可以在服務器端進行反向地理編碼,而不是在客戶端使用JavaScript。

例如,如果你嘗試在瀏覽器下面的網址,你會回來的q參數傳遞的郵政編碼的經度和緯度,以CSV格式:

http://maps.google.com/maps/geo?q=SL59JH,+UK&output=csv&sensor=false 

http://maps.google.com/maps/geo?q=LU13TQ,+UK&output=csv&sensor=false 

這是你會怎樣能夠反向地理您的郵政編碼在PHP,例如:

$url = 'http://maps.google.com/maps/geo?q=SL59JH,+UK&output=csv&sensor=false'; 

$data = @file_get_contents($url); 

$result = explode(",", $data); 

echo $result[0]; // status code 
echo $result[1]; // accuracy 
echo $result[2]; // latitude 
echo $result[3]; // longitude 

注意,作爲Pekka suggested in another answerGoogle Maps API Terms of Use似乎prohibit the storage of the results,除非店裏充當數據的高速緩存將ü sed在谷歌地圖。您可能希望與Google取得聯繫,並詢問Google Maps API Premier有更靈活的地理編碼要求條款。

+0

@Daniel ...非常感謝您的反饋..我會解決你的給定解決方案,並會回覆你... – Nirmal 2010-04-12 12:26:47

+0

這是仍然工作,因爲在我的瀏覽器中使用上述示例我回到610,0,0,0。無論我的郵政編碼樣本是什麼,總是610,0,0,0? – QFDev 2013-10-10 08:39:34

+0

我認爲這是因爲這個答案中的例子使用了舊版本的maps API。一個新的例子會是這樣的:http://maps.googleapis.com/maps/api/geocode/json?address=SL59JH,+UK&sensor=false。顯然,提取lat/lng的查詢將需要更改,因爲輸出現在返回JSON而不是CSV。 – Dan 2014-03-13 10:59:28

2

Google Geocoding API這樣做,雖然如果我沒有記錯,他們的服務條款禁止本地存儲地理編碼結果。

+0

我做了一個基於這個應用程序,我不記得有這個限制。我認爲反對邏輯不是爲了保存結果,因爲它們不會在時間上發生變化,從而爲Google保留未來的ping。 – 2010-04-15 09:44:00

+0

@Elzo你是對的,如果在谷歌地圖應用中使用它,它可以「暫時」緩存結果:10.3 http://code.google.com/intl/zh-CN/apis/maps/terms.html – 2010-04-15 09:46:07

+0

有沒有人知道如果這是一個https版本,通過郵政編碼獲取長期和格子? – AngularM 2016-01-11 13:39:31

9

軍械測量局在知識共享許可證(CC-BY v3,IIRC)上有released the postcode locations。相反,使用它會少很多麻煩(合法性更好)。

甚至還有用WGS84(又名GPS)版本協調由mySociety

+0

+1如果你有方便的話,一個到軍械調查數據轉儲的鏈接也會很好。 – 2010-04-12 20:21:36

+0

它鏈接後的鏈接,不能看到它在第一頁上承諾沒有20mb zip文件。 5分鐘後,我放棄了。 – brux 2014-05-01 15:12:31

+0

@brux從我給出的鏈接中,勾選「Code-Point®Open」產品的下載框,轉到下一步,填寫表單,然後等待帶有下載鏈接的電子郵件。就那麼簡單。 – 2014-05-01 16:11:28

1

鏡像我知道這是一個老問題,但只是小打小鬧在這裏與我如何設法達到同樣的事情(在PHP中,但應相當簡單):

  1. 我有一個數以千計不同格式的郵政編碼的數據庫。我打掃他們的了每一個,要均勻,用這個功能和批量更新:

    function clean_postcode($postcode) 
    { 
        $out = preg_replace("/[^a-zA-Z0-9]/", '',strtoupper($postcode)); 
    
        if(strlen($out)>3) 
        { 
         $out = substr($out, 0, (strlen($out) -3)).' '.substr($out, -3); 
        } 
    
        return $out; 
    } 
    
  2. 現在所有郵政編碼是統一格式,我下載並導入地形測量局的代碼點數據組(免費) - https://www.ordnancesurvey.co.uk/opendatadownload/products.html

  3. 我在一個單獨的codepoint表中將其所有CSV導入到我的數據庫中。從每個CSV導入郵政編碼,以及Eastings和Northings值。

  4. 我在我的數據庫中的所有Ordnance Survey數據上批量再次運行​​函數。大約50%的郵政編碼有空格,50%的編碼沒有 - 之後,統一設置。

  5. 我跑了每個每個郵編在codepoint表下面的PHP腳本,並拯救了緯度和經度值返回到這個表:http://bramp.net/blog/os-easting-northing-to-lat-long

  6. 全部完成!您現在可以匹配並根據格式良好的郵政編碼拉取Lat/Lon值。

延伸閱讀:http://www.deepbluesky.com/blog/-/converting-os-coodinates-into-longitude-latitude_7/

13

快速注意那些找到這個蘇答案。 Daniel Vassallo的答案使用了現在已被棄用的Google Geocoding API V2。新V3 API使用像這樣的請求格式:

http://maps.googleapis.com/maps/api/geocode/output?parameters 

用於郵政編碼查詢的例子,JSON格式返回的數據是:

http://maps.googleapis.com/maps/api/geocode/json?address=SL59JH,+UK&sensor=false 

這將返回一個JSON陣列,其包括lat和長在結果>的幾何性質 - >位置 - > lat和結果>的幾何性質 - >位置 - > LNG

實施例響應:

{ 
"results" : [ 
    { 
    "address_components" : [ 
     { 
      "long_name" : "SL5 9JH", 
      "short_name" : "SL5 9JH", 
      "types" : [ "postal_code" ] 
     }, 
     { 
      "long_name" : "Windsor and Maidenhead", 
      "short_name" : "Windsor and Maidenhead", 
      "types" : [ "administrative_area_level_2", "political" ] 
     }, 
     { 
      "long_name" : "United Kingdom", 
      "short_name" : "GB", 
      "types" : [ "country", "political" ] 
     }, 
     { 
      "long_name" : "Ascot", 
      "short_name" : "Ascot", 
      "types" : [ "postal_town" ] 
     } 
    ], 
    "formatted_address" : "Ascot, Windsor and Maidenhead SL5 9JH, UK", 
    "geometry" : { 
     "bounds" : { 
      "northeast" : { 
       "lat" : 51.39655490000001, 
       "lng" : -0.66024660 
      }, 
      "southwest" : { 
       "lat" : 51.39457330, 
       "lng" : -0.6624574999999999 
      } 
     }, 
     "location" : { 
      "lat" : 51.39539040, 
      "lng" : -0.66096740 
     }, 
     "location_type" : "APPROXIMATE", 
     "viewport" : { 
      "northeast" : { 
       "lat" : 51.39691308029150, 
       "lng" : -0.6600030697084980 
      }, 
      "southwest" : { 
       "lat" : 51.39421511970851, 
       "lng" : -0.6627010302915021 
      } 
     } 
    }, 
    "types" : [ "postal_code" ] 
    } 
], 
"status" : "OK" 
} 

API規範可在這裏:https://developers.google.com/maps/documentation/geocoding/

0

退房ALgolia地方here。超快速,開放源代碼和麻省理工學院