2010-07-19 4 views
9

मेरे पास v3 एपीआई पर एक Google मानचित्र चल रहा है, मैंने कुछ कस्टम मार्कर जोड़े हैं, क्या यह मानचित्र के ज़ूम स्तर के आधार पर उन्हें स्केल करना संभव है? मैंने संदर्भ खोजने की कोशिश की लेकिन मार्कर इमेज का आकार बदलने के लिए कोई भी तरीका नहीं दिख रहा है।ज़ूम के आधार पर मार्कर का आकार बदलें Google मानचित्र v3

शायद मुझे मार्करों को ज़ूम करने और विभिन्न आकारों में नए मार्कर बनाने के लिए मार्करों को हटाना होगा?

उत्तर

7

दुर्भाग्य से, आपको हर बार आइकन सेट करना होगा। हालांकि, आप उन्हें पूर्व-परिभाषित कर सकते हैं, और फिर उन्हें मार्कर पर लागू कर सकते हैं।

zoomIcons = [null, icon1, icon2]; // No such thing as zoom level 0. A global variable or define within object. 
marker.setIcon(zoomIcons[map.getZoom()]); 
+2

आप भी इस तरह के रूप में एक zoom_changed घटना को यह हुक कर सकते हैं,: google.maps.event.addListener (मानचित्र, "zoom_changed", समारोह() {var ज़ूम = map.getZoom(); // करना वर्तमान ज़ूम के आधार पर कुछ}}; – Dr1Ku

24

यह कोड हर बार मानचित्र ज़ूम होने पर आकार बदलता है, इसलिए यह हमेशा उसी भौगोलिक क्षेत्र को कवर करता है।

//create a marker image with the path to your graphic and the size of your graphic 
var markerImage = new google.maps.MarkerImage(
    'myIcon.png', 
    new google.maps.Size(8,8), //size 
    null, //origin 
    null, //anchor 
    new google.maps.Size(8,8) //scale 
); 

var marker = new google.maps.Marker({ 
    position: new google.maps.LatLng(38, -98), 
    map: map, 
    icon: markerImage //set the markers icon to the MarkerImage 
}); 

//when the map zoom changes, resize the icon based on the zoom level so the marker covers the same geographic area 
google.maps.event.addListener(map, 'zoom_changed', function() { 

    var pixelSizeAtZoom0 = 8; //the size of the icon at zoom level 0 
    var maxPixelSize = 350; //restricts the maximum size of the icon, otherwise the browser will choke at higher zoom levels trying to scale an image to millions of pixels 

    var zoom = map.getZoom(); 
    var relativePixelSize = Math.round(pixelSizeAtZoom0*Math.pow(2,zoom)); // use 2 to the power of current zoom to calculate relative pixel size. Base of exponent is 2 because relative size should double every time you zoom in 

    if(relativePixelSize > maxPixelSize) //restrict the maximum size of the icon 
     relativePixelSize = maxPixelSize; 

    //change the size of the icon 
    marker.setIcon(
     new google.maps.MarkerImage(
      marker.getIcon().url, //marker's same icon graphic 
      null,//size 
      null,//origin 
      null, //anchor 
      new google.maps.Size(relativePixelSize, relativePixelSize) //changes the scale 
     ) 
    );   
}); 
+0

धन्यवाद यह काम किया! मुझे भौगोलिक भाग को कवर करने के लिए स्केलिंग के अतिरिक्त चरण भी पसंद हैं। हालांकि मेरे लिए मार्कर प्रतीकात्मक हैं, इसलिए मैंने उचित रूप से उन्हें स्केल करने के लिए map.getZoom() पर तुलना सशर्त का उपयोग किया। – efwjames

+1

आपके कोड के लिए धन्यवाद। जाहिर है मार्कर इमेज को हटा दिया गया और हटा दिया गया। अब इसका उपयोग करें -> https://developers.google.com/maps/documentation/javascript/markers#convertingtoicon –