【问题标题】:Does SimpleGeo provide ZIP code to latitude/longitude mapping?SimpleGeo 是否提供邮政编码到纬度/经度映射?
【发布时间】:2011-05-30 19:38:25
【问题描述】:

我正在与 SimpleGeo API 合作,我需要通过邮政编码或城市名称获取地点的纬度/经度。我知道SO中有很多关于通过邮政编码获取坐标的问题,并且有很多很好的数据库。但它们是与 SimpleGeo 的数据密切相关的东西吗?我不想仅仅因为我的数据库与 SG 的数据库不同,而在 SimpleGeo 的数据库中存在精确的城市名称/邮政编码。

或者,如果 Foursquare API 提供此类数据,我可以完全转移到它,但这不太可取。

所以,我的问题是:

  1. 我可以通过 SimpleGeo(或至少 Foursquare)通过邮政编码或城市名称获取纬度/经度吗?
  2. 如果不是,我可以使用其他数据库(例如MaxMindGeonames)来获得相同的结果吗?我应该关心数据库之间的差异吗?

【问题讨论】:

    标签: geolocation foursquare zipcode simplegeo


    【解决方案1】:

    是的,您可以使用 SimpleGeo 上下文 API 来执行此操作。 使用城市名称或邮政编码作为地址参数调用 SimpleGeo 上下文端点,如下所示: http://api.simplegeo.com/1.0/context/address.json?address=98006

    响应在返回的 JSON blob 的“查询”部分中嵌入了纬度和经度:
    { “询问”:{ “纬度”:40.745717, “经度”:-73.988121 ... }, ... }

    希望这会有所帮助。

    谢谢,
    -尼基尔

    【讨论】:

      【解决方案2】:

      如果有帮助... 以下是如何使用谷歌的地理编码器获取邮政编码的纬度/经度。假设您有一个带有 id 'location' 的 HTML 输入(使用 jqueryui 自动完成小部件):

      // 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"
                };
              }));
              }
            }
          }
      }
      

      google 的地理编码器可以对邮政编码进行地理编码,没问题。

      【讨论】:

      • 请务必注意,如果不附带 GMAP,则不能使用 Google 的地理编码器(这违反了 TOS)。不,您不能只在地图的 div 上使用 display="none"...
      猜你喜欢
      • 2012-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-17
      • 1970-01-01
      相关资源
      最近更新 更多