2012-06-21 7 views
16

मुझे आश्चर्य है कि इस स्निपेट का उपयोग करके या जियोकोडिंग अनुरोधों का उपयोग करके लैट और एलएनजी के बजाय ज़िपकोड का उपयोग करने का कोई तरीका है या नहीं।Google मानचित्र में लाइट और एलएनजी के बजाय ज़िप कोड का उपयोग कैसे करें 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 GeoCode API का उपयोग कर सकते हैं और वहां से जा सकते हैं।

एपीआई अनुरोध URL ऐसा दिखाई देगा अगर आप या इस सीधे करना चाहता था कुछ अन्य मध्यम

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

कहाँ 50,323 अपना पिन कोड है के माध्यम से होगा।

या आप jQuery जावास्क्रिप्ट एपीआई का उपयोग 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 का प्रयास कर रहा है, बेल्जियम से डाक कोड), और यदि मैं ज़िपक को पूर्णांक के रूप में पास करता हूं @ मोहम्मद के रूप में एक ही त्रुटि देता है। उमर कहते हैं – AlvaroAV

संबंधित मुद्दे