2010-02-15 10 views
35

क्या देश के आकार के आधार पर ज़ूम स्तर स्वचालित रूप से स्थापित करने का था, जिस पर नक्शा केंद्रित है?किसी देश के लिए सही ज़ूम स्तर सेट करने के लिए Google मानचित्र API कैसे प्राप्त करें?

maps.google.com मुझे वही करता है जो मुझे चाहिए, उदाहरण के लिए, यदि मैं रूस की खोज करता हूं तो मुझे ज़ूम स्तर मिलता है जैसे कि रूस बस स्क्रीन पर फिट बैठता है, और जब मैं क्यूबा की खोज करता हूं तो मुझे उच्च ज़ूम स्तर मिलता है ताकि क्यूबा बस फिट बैठ सके।

क्या मानचित्र एपी को देश का स्थान देने और उपयुक्त ज़ूम स्तर प्राप्त करने का कोई तरीका है।

यदि नहीं, तो मुझे लगता है कि मुझे मैन्युअल रूप से (ugh!) इस जानकारी के लिए अपनी तालिका बनाना होगा। या यह जानकारी कहीं भी मुफ्त में उपलब्ध है?

उत्तर

45

एपीआई v3 के लिए नीचे दिए गए उत्तर की जांच करें।

आप देश के सीमांकन बॉक्स प्राप्त करने के लिए निम्न उदाहरण में गूगल मैप्स Client-side Geocoder उपयोग कर सकते हैं: जब रूस के लिए खोज

// API version 2 
var geocoder = new GClientGeocoder(); 

geocoder.getLocations("Russia", function (locations) { 

    var north = locations.Placemark[0].ExtendedData.LatLonBox.north; 
    var south = locations.Placemark[0].ExtendedData.LatLonBox.south; 
    var east = locations.Placemark[0].ExtendedData.LatLonBox.east; 
    var west = locations.Placemark[0].ExtendedData.LatLonBox.west; 

    var bounds = new GLatLngBounds(new GLatLng(south, west), 
            new GLatLng(north, east)); 

    map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds)); 
}); 

// API version 3 
// ... set north, south, east and west ... 
var bounds = new google.maps.LatLngBounds(new google.maps.LatLng(south, west), 
              new google.maps.LatLng(north, east)); 
map.fitBounds(bounds); 

नीचे दिए गए स्क्रीनशॉट ऊपर तकनीक के परिणाम दिखाने के और क्यूबा:

Zoom on Russia and Cuba

+2

बहुत बढ़िया,:

यहाँ jQuery के getJSON समारोह और गूगल मैप्स एपीआई v3 का उपयोग कर एक कोड उदाहरण है। मैं जीडब्ल्यूटी में काम कर रहा हूं और यह अभी तक विस्तारित डेटा तक नहीं पहुंच सकता है, इसलिए मुझे ऐसा करने के लिए कुछ जेएसएनआई विधियां बनाना पड़ा। एक चीज जो मुझे थोड़ी देर के लिए मिली: सुनिश्चित करें कि GetBoundsZoomLevel को कॉल करते समय नक्शा दिखाई देता है, अन्यथा यह शून्य का ज़ूम स्तर देता है। – krishnaz

+0

@ कृष्णाज़: अच्छा काम। 'GetBoundsZoomLevel()' टिप साझा करने के लिए धन्यवाद। –

+2

+1 हम किस उम्र में रहते हैं ........ –

3

आप गूगल मैप्स एपीआई का उपयोग नहीं करते और सिर्फ अपने जियोकोडिंग का उपयोग आप इस सूत्र का उपयोग कर सकते हैं:

var url = 'http://maps.google.com/maps/geo?q=YOUR_QUERY&output=json&oe=utf8&sensor=false&key=YOUR_KEYback=geoCodeDone'; 
jQuery.getScript(url); 


function geoCodeDone(data) 
{ 
    if (data.Status.code == 200) 
    { 
     lng = data.Placemark[0].Point.coordinates[0]; 
     lat = data.Placemark[0].Point.coordinates[1]; 

     var east = data.Placemark[0].ExtendedData.LatLonBox.east; 
     var west = data.Placemark[0].ExtendedData.LatLonBox.west; 

     var zoom = 11 - Math.round(Math.log(Math.abs(west-east))/Math.log(2)); 
    } 
} 
49

वी 3 के लिए इस कोड को मेरे लिए काम किया:

var geocoder = new google.maps.Geocoder(); 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     map.fitBounds(results[0].geometry.viewport); 
     } 
    }); 
+2

पूरी तरह से काम किया। वी 3 के लिए अद्यतन संस्करण जोड़ने के लिए धन्यवाद। बहुत सराहना की। –

+2

ग्रेट। किसी भी जगह के साथ काम करता है। शहर, प्रांत, देश इत्यादि – Phius

+0

क्या 'सेट सेंटर' और 'फिटबाउंड' के लिए तर्क सही हैं? मुझे यह काम नहीं मिल रहा है, एक जेएस त्रुटि मिली है "बहुत अधिक रिकर्सन" - 'map.setCenter (lat, lon); 'काम करता है। –

1

आप क्योंकि उपयोग सीमाओं की, गूगल से जियोकोडर ग्राहक का उपयोग करने के लिए आप अपनी स्वयं की सूची इस्तेमाल कर सकते हैं नहीं करना चाहते हैं। आप इस github repo से एक प्राप्त कर सकते हैं। कि सही धन्यवाद था

function initialize() { 
     // read the list of countries 
    $.getJSON('countries.json', function (countries) { 

     // will use the country with index 40 (Cyprus) 
     var index_country = 40; 
     var myOptions = { 
      center: new google.maps.LatLng(
       countries[index_country].center_lat, 
       countries[index_country].center_lng), 
     } 
     var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 

     // set the bounds of the map 
     var bounds = new google.maps.LatLngBounds(
      new google.maps.LatLng(countries[index_country].sw_lat, countries[index_country].sw_lng), 
      new google.maps.LatLng(countries[index_country].ne_lat, countries[index_country].ne_lng)); 

     map.fitBounds(bounds); 
    }); 
} 
संबंधित मुद्दे