【问题标题】:Scaling marker size with marker icons from a sprite in Google Maps API v3使用 Google Maps API v3 中精灵的标记图标缩放标记大小
【发布时间】:2011-10-12 06:48:24
【问题描述】:

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

图标本身位于一个包含三个不同圆形标记的精灵中,每个标记为 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
       )
    });

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

我做错了什么?

【问题讨论】:

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


【解决方案1】:

经过反复试验,我已经弄清楚了它应该如何工作(文档是骗人的),感谢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
       )
    });

希望这可以帮助某人!

【讨论】:

  • 对于那些在家关注的人,链接博客文章的重点是:“要显示缩放图像或缩放 prite 图像,只需提供 size、origin、anchor 和 scaledSize 参数应用了比例因子。”
  • 谢谢,仅供参考 google.maps.MarkerImage 在 v3.10+ 中被 google.maps.Icon 取代
  • 我使用 scaledSize 来缩小用于标记的图标。这对我行得通。 var icon = { url: imageName, scaledSize: new google.maps.Size(8, 12) };
猜你喜欢
  • 2016-12-28
  • 1970-01-01
  • 2012-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-18
  • 1970-01-01
  • 2011-03-17
相关资源
最近更新 更多