javascript - 使用 Google Maps API v3 中的 Sprite 标记图标缩放标记大小

标签 javascript image google-maps google-maps-api-3 gis

我在使用 Google Maps API (v3) 时遇到了标记图标的问题。我正在尝试根据标记的各个数据属性来改变标记的大小。

图标本身位于包含三个不同圆形标记的 Sprite 中,每个标记为 16 像素 x 16 像素。我正在尝试缩放单个图标,但到目前为止没有成功。

这是我的代码:

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

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

我做错了什么?

最佳答案

经过反复试验,我已经弄清楚这是如何工作的(文档是骗人的),感谢 this blog post .

这是我的新代码:

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

希望这对下线的人有帮助!

关于javascript - 使用 Google Maps API v3 中的 Sprite 标记图标缩放标记大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6811313/

相关文章:

javascript - 如何在同一个 Visual Studio 项目中混合使用 Node.js 和 Typescript?

javascript - 我陷入了 Javascript 变量递增教程

javascript - 如何获取 queryselectorall 的克隆元素并附加到另一个 div

image - 如何使用spring mvc显示mysql数据库中的图像

image - 使用 Matlab 自动测量管边缘距离

javascript - 为什么 $.when() 不等待函数完成?

image - OpenGL:渲染图像 3D 点云

Android 谷歌地图 java.lang.NoClassDefFoundError : Failed resolution of: Lorg/apache/http/ProtocolVersion

javascript - 多边形内的点——json还是kml?

jquery - jQuery 和 Google Maps API v3 的最佳插件?