简体   繁体   English

通过缩放调整自定义图像标记的大小-Google Maps API v3

[英]Resizing custom image marker with zoom - Google Maps API v3

I'm using a custom image as an icon/marker for a Google Map API. 我正在使用自定义图像作为Google Map API的图标/标记。 I would like the marker image to resize as users zoom in on the map. 我希望标记图像能够随着用户放大地图而调整大小。

I adopted some of the code from this question: Google Maps :: Changing icon based on zoom level 我从这个问题中采用了一些代码: Google Maps ::根据缩放级别更改图标

Unfortunately, this script only changes the last lat/long marker in my list. 不幸的是,该脚本仅更改了列表中的最后一个经/纬标记。 I'm not sure why, can anyone point out my mistake? 我不确定为什么,有人可以指出我的错误吗?

Here's the code: 这是代码:

var locations = [
 ['Aaron Baddeley (130)',-25.274398,133.775136],
 ['Adam Hadwin (176)',52.939916,-106.450864],
 ['Adam Scott (7)',-26.65,153.066667],
 ['Adilson da Silva (291)',-28.530554,30.895824],
 ['Alejandro Canizares (167)',40.416775,-3.70379],];

var infowindow = new google.maps.InfoWindow({}

var image = new google.maps.MarkerImage('images/marker11.gif',
  new google.maps.Size(9,9), //size
    null, //origin
    null, //anchor
    new google.maps.Size(9,9) //scale
);

var marker, i;
for (i = 0; i < locations.length; i++) {
  marker = new google.maps.Marker({
  position: new google.maps.LatLng(locations[i][1], locations[i][2]),
  map: map,
  icon: image,
});

//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 pixelSizeAtZoom4 = 9; //the size of the icon at zoom level 4
  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(pixelSizeAtZoom4*Math.pow(1.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;

  if(zoom < 4) //when zooming < 4, fix pixel size to 9
   relativePixelSize = 9;


//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
    )
);        
});

Add an event listener for zoom_changed events that trigger a change of the marker's icon zoom_changed事件添加事件侦听器,以触发标记图标的更改

map.addListener('zoom_changed', function() {
    for (var i=0, len = locations.length; i < len; i++) { 
       // set new icon depending on the value of map.getZoom()
       markers[i].setIcon(...) 
    }
});

You should save your markers in an array markers when you create them in the for loop, so you can reference them as markers[i] . 在for循环中创建markers时,应将标记保存在数组markers中,以便可以将其作为markers[i]引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM