2011-07-20 9 views
9

私はlatとlonにGoogle geocoderを使用しています。私の質問には、緯度と経度の郵便番号を見つける方法がありますか?緯度と経度は郵便番号がありますか?

+0

この質問に対する回答は古くなっています。 http://stackoverflow.com/a/17933106/4526479 –

答えて

8

私は何を探していることはresults配列のaddress_components[]だと思います。たぶんこのようなものがうまくいくかもしれません。ちょうど以下に入力すればエラーになるかもしれませんが、あなたはそのアイデアを得るでしょう。

http://code.google.com/apis/maps/documentation/geocoding/#Results

function (request, response) { 
    geocoder.geocode({ 'address': request.term, 'latLng': centLatLng, 'region': 'US' }, function (results, status) { 
    response($.map(results, function (item) { 
     return { 
     item.address_components.postal_code;//This is what you want to look at 
     } 
} 
+0

ノートに基づいて郵便番号を取得することができます方法はあり明らかにAPIがあるためこの回答への点で変更されていない最新の回答(のために、以下の答え2013):http://stackoverflow.com/a/17933106/4526479 –

1

はそれはとても思われます:

出典:Google Maps API Service

ジオコーディングを使用して、マーカーまたは位置を配置する を使用することができ(緯度37.423021、経度-122.083739など) 地理座標に(「1600 アンフィシアター・パークウェイ、マウンテンビュー、CA」のような)アドレスを変換する処理であります地図。 Google Geocoding API は、HTTPリクエストを介してジオコーダに直接アクセスする方法を提供します。 さらに、サービスでは、逆の操作 (座標をアドレスに変換)を実行できます。このプロセスは 「逆ジオコーディング」と呼ばれます。

また、いくつかのサンプルコードを持っているこのドキュメントをチェックアウトする必要があります: Reverse Geocoding

+0

ヨーダ:どうやらここで、2013年以来変わっていないGoogleマップAPIの現在のバージョンを、アドレス解決策があり、私は何のようにアドレスを変換していますあなたは上記の投稿が、私の質問はもっとある「私は緯度と経度 –

8

ここではGoogleのJSをスキップして、JSON(jqueryの&のCoffeeScript)として結果を取得する私のソリューションです:

navigator.geolocation.getCurrentPosition (pos) -> 
    coords = pos.coords 
    url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=#{ coords.latitude },#{ coords.longitude }&sensor=true&callback=zipmap" 
    $.ajax({ 
     url: url, 
     dataType: 'json', 
     cache: true, 
    }).success (data) -> 
     for c in data.results[0].address_components 
     if c.types[0] == 'postal_code' 
      alert c.short_name 

https://developers.google.com/maps/documentation/geocoding/

私は、これは動作しませんでした推測長い間、私はそれが一点であったことを誓うが。私はgoogle .jsの一部である秘密の握手を逃しているに違いない。私はopenstreetmap.orgに切り替え:

getZip = function(cb) { 
    if (document.location.protocol === 'http:' && (navigator.geolocation != null)) { 
    return navigator.geolocation.getCurrentPosition(function(pos) { 
     var coords, url; 
     coords = pos.coords; 
     url = "http://nominatim.openstreetmap.org/reverse?format=json&lat=" + coords.latitude + "&lon=" + coords.longitude + "&addressdetails=1"; 
     return $.ajax({ 
     url: url, 
     dataType: 'jsonp', 
     jsonp: 'json_callback', 
     cache: true 
     }).success(function(data) { 
     return cb(data.address.postcode); 
     }); 
    }); 
    } 
}; 

はこのようにそれを使用します:

getZip(function(zipcode){ console.log("zip code found:" + zipcode); }); 
+1

はないのCoffeeScript – Zone6

+0

にZone6 @ナッジのためのおかげで、ここでのJSのバージョンであることが好ましいだろう – Julian

+0

おかげで、これは私を助け、オープンストリートマップはwaaayyy簡単でした。郵便番号を取得するには、Googleのバージョンに与えるもの – garek007

6

それはGoogleマップの新バージョン以降を持っていることに注意することは良いことだ

getZip = (cb) -> 
    # try to populate zip from geolocation/google geocode api 
    if document.location.protocol == 'http:' && navigator.geolocation? 
    navigator.geolocation.getCurrentPosition (pos) -> 
     coords = pos.coords 
     url = "http://nominatim.openstreetmap.org/reverse?format=json&lat=#{ coords.latitude }&lon=#{ coords.longitude }&addressdetails=1" 
     $.ajax({ 
     url: url, 
     dataType: 'jsonp', 
     jsonp: 'json_callback', 
     cache: true, 
     }).success (data) -> 
     cb(data.address.postcode) 

ここでコンパイルされたJavaScriptですこの解決策が提示された。

参考:

https://developers.google.com/maps/documentation/geocoding/?csw=1#ReverseGeocodingはここでGoogleマップv3の更新例です。上記のJIssakに記載されているアドレスコンポーネントを利用しています。フォールバックはないことに注意してください。郵便番号を見つけられなかった場合は、何もしません。これはあなたのスクリプトにとって重要かもしれません。

var latlng = new google.maps.LatLng(p.coords.latitude, p.coords.longitude); 
geocoder = new google.maps.Geocoder(); 

    geocoder.geocode({'latLng': latlng}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
      if (results[0]) { 
       for (j = 0; j < results[0].address_components.length; j++) { 
        if (results[0].address_components[j].types[0] == 'postal_code') 
         alert("Zip Code: " + results[0].address_components[j].short_name); 
       } 
      } 
     } else { 
      alert("Geocoder failed due to: " + status); 
     } 
    }); 
関連する問題