2011-05-30 112 views
0

我正在與SimpleGeo API和我需要通過郵編或城市名稱獲取地點的經度/緯度。我知道有很多關於通過郵政編碼獲得座標的問題,並且有很多很好的數據庫。但是他們是否與SimpleGeo的數據密切相關?我不想讓SimpleGeo的數據庫中存在精確的城市名稱/郵政編碼,因爲我的數據庫與SG的數據庫不同而無法到達目的地。SimpleGeo是否提供郵政編碼給緯度/經度映射?

或者,我可以完全移動到Foursquare API,如果它提供了這樣的數據,但這是不太可取的。

所以,我的問題是:

  1. 我可以通過郵政編碼或城市名通過SimpleGeo(或至少四方)獲得緯度/經度?
  2. 如果不是,我可以使用其他數據庫(例如MaxMindGeonames)來獲得相同的結果嗎?我應該關心數據庫之間的差異麼?

回答

1

是的,您可以使用SimpleGeo上下文API來執行此操作。 調用SimpleGeo背景下端點的城市名或者郵政編碼爲這樣的地址參數: http://api.simplegeo.com/1.0/context/address.json?address=98006

的響應具有緯度和經度嵌入JSON Blob的「查詢」部分返回:
{ 「查詢」:{ 「緯度」:40.745717, 「東經」: - 73.988121 ... }, ... }

希望這有助於。

感謝,
-Nikhil

0

在情況下,它可以幫助... 這裏是你如何使用谷歌的地理編碼器,以獲得郵編緯度/經度。假設你有id爲「位置」(使用jQueryUI的自動完成構件)的HTML輸入:

// insert a div for autocomplete results 
$("#location").after('<div id="location-results"></div>'); 

$("#location").autocomplete({ 
    appendTo:$("#location-results"), 
    minLength: 3, 
    open: acOpen, 
    select: acSelect, 
    close: acClose, 
    source: acSource 
}); 

function acOpen(event, ui){ 
// no op, but put code here to handle results list open if you need it 
} 

function acSelect(event, ui){ 
    // fired when user selects a result from the autocomplete list 
    // assuming you are using the json2.js library for json parsing/decoding 
    alert(JSON.stringify(ui.item)); 
    // the lat/lng is in ui.item.latLng.lat() and ui.item.latLng.lng() 
    // ui.item.latLng is of type google.maps.LatLng (see v3 api) 
} 

function function acClose(event, ui){ 
// no op, but put code here to handle results list close if you need it 
} 

function acSource(request, response) { 
    // implements the autocomplete source for the widget 
    geocoder.geocode({'address': request.term }, function(results, status) { 
     if(undefined!==results){ 
      var postal_code=''; 
      var city = ''; 
      var state = ''; 
      var country = ''; 
      response($.map(results, function(item) { 
       $.each(item.address_components, function(item_i, item_v){ 
       $.each(item_v.types, function(type_i, type_v){ 
        switch(type_v){ 
        case "locality": case "administrative_area_level_3": 
         city = item_v.long_name; 
        break; 
        case "administrative_area_level_2": 
        break; 
        case "street_number": 
        break; 
        case "route": 
        break; 
        case "administrative_area_level_1": 
        state = item_v.long_name; 
       break; 
       case "country": 
        country = item_v.long_name; 
       break; 
       case "postal_code": 
        postal_code = item_v.long_name; 
       break; 
       } 
     }); 
      }); 
       // the object that gets returned and is ui.item on select event 
      return { 
      label : item.formatted_address, 
      value : item.formatted_address, 
      latLng : item.geometry.location, 
      country  : country, 
       state  : state, 
       city  : city, 
       postal_code : postal_code, 
      source : "google" 
      }; 
     })); 
     } 
     } 
    } 
} 

谷歌的地理編碼器可以進行地址解析郵編,沒有問題。

+0

重要的是要注意,谷歌的地理編碼器不能在沒有包括GMAP的情況下使用(這是違反TOS的)。不,你不能在地圖上使用'display =「none」'... – tylerl 2011-08-28 05:05:25

相關問題