2010-10-13 8 views
40

यदि कोई उपयोगकर्ता कोई पता इनपुट करता है, तो मैं समतुल्य LatLng में कनवर्ट करना चाहता हूं।Google मानचित्र API v3 का उपयोग करके मैं किसी दिए गए पते के साथ LatLng कैसे प्राप्त करूं?

मैंने प्रलेखन पढ़ा है, और मुझे लगता है कि मैं ऐसा करने के लिए जियोकोडर कक्षा का उपयोग कर सकता हूं, लेकिन यह समझ नहीं सकता कि इसे कैसे कार्यान्वित किया जाए।

किसी भी मदद के लिए धन्यवाद!

उत्तर

85

https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

पर एक बहुत अच्छा उदाहरण इसे छोटा अप करने के लिए नहीं है एक छोटे से:

geocoder = new google.maps.Geocoder(); 

function codeAddress() { 

    //In this case it gets the address from an element on the page, but obviously you could just pass it to the method instead 
    var address = document.getElementById('address').value; 

    geocoder.geocode({ 'address' : address }, function(results, status) { 
     if(status == google.maps.GeocoderStatus.OK) { 

      //In this case it creates a marker, but you can get the lat and lng from the location.LatLng 
      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); 
     } 
    }); 
} 
+6

क्या हम नहीं चाहते कि सभी दस्तावेज jQuery के जितना अच्छा हो। मुझे Google के कुछ दस्तावेज़ों के साथ कठिन समय है। – b729sefc

22

मैं location.LatLng काम कर रहा है नहीं लगता है, लेकिन इस काम करता है:

results[0].geometry.location.lat(), results[0].geometry.location.lng() 

Get Lat Lon स्रोत कोड की खोज करते समय इसे मिला।

+0

इसके लिए धन्यवाद! – cpcdev

16

आप बैकएंड आप निम्न URL संरचना का उपयोग कर सकते हैं पर यह करने के लिए की जरूरत है: कर्ल का उपयोग कर

https://maps.googleapis.com/maps/api/geocode/json?address=STREET_ADDRESS 

नमूना PHP कोड:

$curl = curl_init(); 

curl_setopt($curl, CURLOPT_URL, 'https://maps.googleapis.com/maps/api/geocode/json?address=' . rawurlencode($address)); 

curl_setopt ($curl, CURLOPT_RETURNTRANSFER, 1); 

$json = curl_exec($curl); 

curl_close ($curl); 

अधिक जानकारी के लिए अतिरिक्त documentation देखें।

दस्तावेज़ नमूना आउटपुट प्रदान करते हैं और Google मानचित्र जियोकोडिंग API पर अनुरोध करने में सक्षम होने के लिए आपको अपना खुद का API key प्राप्त करने में सहायता करेंगे।

+0

क्या आप जानते हैं कि इसके लिए सीमा क्या है। मेरा मतलब है कि मैं एक घंटे या दिन में कितनी कॉल कर सकता हूं? –

+2

निशुल्क एपीआई सीमा 24 घंटे की अवधि के लिए 2500 अनुरोध हैं, प्रति सेकंड 5 अनुरोध ... https://developers.google.com/maps/documentation/geocoding/intro#Limits – farinspace

+1

यहां देखें। PHP उपयोगकर्ता डेटा तक पहुंचने के लिए '$ arr = json_decode ($ json, true);' का उपयोग करना चाह सकते हैं। –

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