2012-06-21 2 views
16

このスニペットを使用するか、ジオコーディングリクエストを使用してLatとLngの代わりに郵便番号を使用する方法があるのだろうかと思います。代わりに郵便番号を使用する方法GoogleマップのLatとLng API

<script type="text/javascript"> 
     function initialize() { 
      var latlng = new google.maps.LatLng(22.1482635,-100.9100755); 
      // var latlang = new google.maps.zipcode(7845); <- Is there a way to do that? 
      var myOptions = { 
       zoom: 8, 
       center: latlng, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 

      var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
      var marker = new google.maps.Marker({ 
       position: latlng, 
       map: map, 
       title:"zipcode" 
      }); 

     } 
    </script> 

答えて

25

GoogleジオコードAPIを使用すると、郵便番号の緯度と経度をリクエストしてそこから移動することができます。あなたが直接、またはいくつかの他の媒体

50323はあなたの郵便番号である

http://maps.googleapis.com/maps/api/geocode/json?address=50323&sensor=false

を経由してこれをやってみたかった場合

APIリクエストURLは次のようになります。

また、Google Javascript APIを使用してjQuery経由でこれを行うこともできます。

var geocoder; //To use later 
var map; //Your map 
function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    //Default setup 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var myOptions = { 
    zoom: 8, 
    center: latlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
} 

//Call this wherever needed to actually handle the display 
function codeAddress(zipCode) { 
    geocoder.geocode({ 'address': zipCode}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     //Got result, center the map and put it out there 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 
     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
    } 
+0

実例をありがとう。 –

+1

エラーが発生します。プロパティアドレス:文字列ではありません。 –

+2

郵便番号を文字列として渡すと南アフリカに行きます(ベルギーの郵便番号8790を試しています)。郵便番号を整数として渡すと@ Mohd.Umarと同じエラーを返します。 – AlvaroAV

関連する問題