0

私はSimpleGeo APIを使用しており、郵便番号または都市名で場所の緯度/経度を取得する必要があります。郵便番号で座標を取得することについて SO に多くの質問があることは知っていますが、それには多くの優れたデータベースがあります。しかし、それらは SimpleGeo のデータと密接に関連するものですか? 私のデータベースが SG のデータベースと異なるという理由だけで、SimpleGeo のデータベースに存在する正確な都市名/郵便番号を持っていて、どこにも行かないようにしたくありません。

または、そのようなデータが提供される場合は、 Foursquare APIに完全に移行できますが、これはあまり好ましくありません。

だから、私の質問は次のとおりです。

  1. SimpleGeo (または少なくとも Foursquare) を使用して、郵便番号または都市名で緯度/経度を取得できますか?
  2. そうでない場合、他のデータベース ( MaxMindGeonamesなど) を使用して同じ結果を得ることができますか? データベース間の違いを気にする必要がありますか?
4

2 に答える 2

1

Yes, you can use the SimpleGeo Context APIs to do this. Call the SimpleGeo context endpoint with the city name or zip code as the address parameter like this: http://api.simplegeo.com/1.0/context/address.json?address=98006

The response has the latitude and longitude embedded in the "query" part of the JSON blob returned:
{ "query":{ "latitude":40.745717, "longitude":-73.988121 ... }, ... }

Hope this helps.

Thanks,
-Nikhil

于 2011-06-13T20:05:20.590 に答える
0

それが役立つ場合... Googleのジオコーダーを使用して郵便番号の緯度/経度を取得する方法は次のとおりです。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 のジオコーダーは郵便番号をジオコーディングできますが、問題ありません。

于 2011-07-08T00:16:06.527 に答える