使用Google Maps API v3中精灵的标记图标缩放标记大小

Max*_*sky 14 javascript gis google-maps image google-maps-api-3

我正在玩Google Maps API(v3),我遇到了标记图标的问题.我正在尝试根据各自的数据属性改变标记的大小.

图标本身是一个精灵,包含三个不同的圆形标记,每个16px乘16px.我正在尝试扩展单个图标,但到目前为止还没有成功.

这是我的代码:

var offset = Math.floor(Math.random() * 3) * 16; // pick one of the three icons in the sprite

// Calculate desired pixel-size of the marker
var size = Math.floor(4*(count-1) + 8);

// Create custom marker
var marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lng),
    icon: new google.maps.MarkerImage(
        'img/dot.png', // my sprite with 3 circular icons
        new google.maps.Size(16, 16), // 16x16 is the original size of each individual icon
        new google.maps.Point(0, offset), // picking one of the three icons in the sprite
        new google.maps.Point(Math.floor(size/2), Math.floor(size/2)), // setting correct anchor for the marker
        new google.maps.Size(size, size) // trying to scale the marker
       )
    });
Run Code Online (Sandbox Code Playgroud)

问题似乎是在最后一行,我正在尝试将标记图标缩放到所需的大小.而不是它正确缩放,我得到一个奇怪的,扭曲的椭圆形图标.

我究竟做错了什么?

Max*_*sky 22

经过一些试验和错误,我已经弄清楚这是如何工作的(文档是欺骗性的),感谢这篇博客文章.

这是我的新代码:

var offset = Math.floor(Math.random() * 3) * 16; // pick one of the three icons in the sprite

// Calculate desired pixel-size of the marker
var size = Math.floor(4*(count-1) + 8);
var scaleFactor = size/16;

// Create custom marker
var marker = new google.maps.Marker({
    position: new google.maps.LatLng(lat, lng),
    icon: new google.maps.MarkerImage(
        'img/dot.png', // my 16x48 sprite with 3 circular icons
        new google.maps.Size(16*scaleFactor, 16*scaleFactor), // desired size
        new google.maps.Point(0, offset*scaleFactor), // offset within the scaled sprite
        new google.maps.Point(size/2, size/2), // anchor point is half of the desired size
        new google.maps.Size(16*scaleFactor, 48*scaleFactor) // scaled size of the entire sprite
       )
    });
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助有人下线!

  • 对于那些一起在家里之后,从链接博客文章中最关键的一点是:"要显示的缩放图像或缩放图像prite,只是应用的比例因子提供尺寸,产地,锚和scaledSize参数." (5认同)