2012-06-22 11 views
20

मैं शहर के नाम के साथ एपीआई प्रदान करके शहर के अक्षांश और देशांतर प्राप्त करना चाहता हूं। यह शहर को इनपुट करने के तरीके के बावजूद अधिकांश शहरों के लिए काम करना चाहिए।Google मानचित्र अक्षांश और देशांतर वाले शहर का नाम प्राप्त करते हैं?

उदाहरण के लिए:

सिटी 'मियामी, अमेरिका' हो सकता है या शहर हो सकता है 'मियामी, संयुक्त राज्य अमेरिका'

मैं कैसे इसके अक्षांश प्रिंट करूं?

उत्तर

45

आप कोड यहाँ jsfiddled पा सकते हैं: http://jsfiddle.net/YphZw/ या नीचे:

$("#btn").click(function(){ 
 
      var geocoder = new google.maps.Geocoder(); 
 
    geocoder.geocode({ 'address': 'miami, us'}, function(results, status) { 
 
      if (status == google.maps.GeocoderStatus.OK) { 
 
      alert("location : " + results[0].geometry.location.lat() + " " +results[0].geometry.location.lng()); 
 
      } else { 
 
      alert("Something got wrong " + status); 
 
      } 
 
     }); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<html> 
 
<head> 
 
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> 
 
</head> 
 
<body> 
 
    <input id="btn" type="button" value="search for miami coordinates" /> 
 
</body> 
 
</html>

आप अधिक उदाहरण चाहते हैं जावास्क्रिप्ट एपीआई के लिए, इस लिंक को आजमाएं: https://developers.google.com/maps/documentation/javascript/examples/

मैंने जो कोड लिखा है वह geocoding-simple नमूना से प्रेरित है।

सम्मान।

संपादित करें 1:

आप इसे एक गैर-सरकारी पीएचपी लाइब्रेरी का उपयोग कर प्राप्त कर सकते हैं। इस उदाहरण की जाँच करें: http://www.bradwedell.com/phpgooglemapapi/demos/geocoding.php (कोड नीचे स्थित है =

+0

क्या PHP के साथ ऐसा करने का कोई तरीका है? धन्यवाद – lisovaccaro

12

आप Google की जियोकोडिंग सेवा, उदाहरण के लिए,

http://maps.googleapis.com/maps/api/geocode/xml?address=Miami+FL&sensor=false

है कि आप प्रारूपों (JSON, एक्सएमएल, आदि) की एक किस्म में वापस भू-संदर्भित डेटा देता है का उपयोग कर सकते हैं। किसी भी घटना में, स्थान निश्चित रूप से लौटाए गए डेटा ब्लॉक में है। पर

एपीआई डॉक्स कर रहे हैं:

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

+0

आपके द्वारा ऊपर दिए गए एपीआई डॉक्स का लिंक और आपके द्वारा वर्णित विधि Google मानचित्र API वेब सेवाओं के बारे में है, न कि जावास्क्रिप्ट का। – Zakaria

+0

दोह! क्षमा करें - जेएस एपीआई (बहुत सिमलर) का लिंक उस पृष्ठ पर है हालांकि। – debracey

2

यह लगता है बेकार में जटिल यहाँ एक उदाहरण है "आस-पास की घटनाओं" नक्शा यह City, State रों ले जाएगा, उन्हें latLng coords में बदलने का है, और एक पर मार्कर डाल दिया।। नक्शा:।

// Nearby Events with Google Maps 
window.nearbyEventsMap =() => { 
    const centerOfUS = { 
     lat: 37.09024, 
     lng: -95.712891 
    } 

    // Create a map object and specify the DOM element for display. 
    const map = new google.maps.Map(document.querySelector('#nearby_events_map'), { 
     center: centerOfUS, 
     scrollwheel: false, 
     zoom: 4 
    }) 

    // Create a marker and set its position. 
    const geocoder = new google.maps.Geocoder() 

    // Filter out duplicate cityStates 
    let cityStates = {} 
    document.querySelectorAll('.nearby_event_city_state').forEach(event => { 
     cityStates[event] = event.innerText 
    }) 

    // `cityState` is in the format of "City, State". It's not picky about state being a word or the abbreviation. 
    for (const cityState in cityStates) { 
     const location = cityStates[cityState] 

     geocoder.geocode({ 
      address: location 
     }, function (results, status) { 
      if (status === 'OK') { 
       const result = results[0].geometry.location 
       const lat = result.lat() 
       const lng = result.lng() 
       const latLng = { 
        lat, 
        lng 
       } 

       return new google.maps.Marker({ 
        map: map, 
        position: latLng 
       }) 
      } 
     }) 
    } 
} 
// /Nearby Events with Google Maps 

अपने <script> टैग शामिल करना सुनिश्चित करें

<script src="/dist/js/main.js"></script> 

<!-- We're loading this after the main.js script so the callback from main.js will be available to it. --> 
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=nearbyEventsMap"></script> 

StackOverflow बाकी और जी हर किसी को शामिल होने के लिए कृपया सिंटैक्स हाइलाइटिंग के साथ et GitHub मार्कडाउन ...

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