简体   繁体   English

如何动态设置谷歌地图标记大小

[英]How to set Google maps marker size dynamically

I am trying to size my marker by zooming, but it seems not to work here is the code for a better overview:我正在尝试通过缩放来调整标记的大小,但这里似乎不起作用,这是用于更好地概述的代码:

function Icon(url) {
    this.url = url;
    this.scaledSize = new google.maps.Size(30, 30); // scaled size
    //this.origin = new google.maps.Point(0,0); // origin
    //this.anchor = new google.maps.Point(0, 0); // anchor
}

var icon1 = new Icon('img/marker-test.png');

        map.addListener('zoom_changed', function() {
        if(map.zoom === 18){
            //marker.setIcon(test);
            console.log('check');
            marker.scaledSize = new google.maps.Size(300, 300);

        } else {
            //marker.setIcon(icon1);
        }
    });

Somebody knows how to fix this?有人知道如何解决这个问题吗?

Before I answer, a couple suggestions:在我回答之前,有几个建议:

  • You have used map.zoom === 18 which is not valid.您使用了map.zoom === 18 It should be map.getZoom() === 18它应该是map.getZoom() === 18
  • marker.scaledSize is invalid as scaledSize is the property of Icon object. marker.scaledSize无效,因为scaledSizeIcon对象的属性。 Ref: Icon Object Specification参考: 图标对象规范

    If you need to set the marker size dynamically, you need to specify the marker image(while creating marker) via icon property and not as a string or leave empty(to show default marker).如果您需要动态设置标记大小,则需要通过 icon 属性指定标记图像(在创建标记时),而不是作为字符串或留空(以显示默认标记)。

Scale wont work on default marker.比例不适用于默认标记。 So you need to provide your own custom image for the marker.因此,您需要为标记提供您自己的自定义图像。

Scale vs Size规模与大小

Scale transforms the marker image and size specifies the canvas size of the image.缩放转换标记图像,大小指定图像的画布大小。 Setting size property of a small marker image will not show you any zoom effect of the marker image.设置小标记图像的大小属性不会显示标记图像的任何缩放效果。 However if the marker image is large enough and that it was previously been drawn with downscaling, upscaling will work fine.但是,如果标记图像足够大并且之前是通过缩小比例绘制的,则放大效果会很好。

If you just scale the image and not set the size, the marker image will zoom in the prespecifed size.如果您只是缩放图像而不设置大小,则标记图像将放大预先指定的大小。 That is some portions of the marker image will not be visible.也就是说,标记图像的某些部分将不可见。

To put in easy words, here is a fiddle .简单地说,这是一个小提琴 Try changing zoom to 18 and over and then back to 15 and below.尝试将缩放更改为 18 及以上,然后再更改为 15 及以下。

Try with the correct getZoom map method.尝试使用正确的getZoom地图方法。

if (map.getZoom() === 18) {
    ...
}

https://developers.google.com/maps/documentation/javascript/reference#Map https://developers.google.com/maps/documentation/javascript/reference#Map

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

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