Google 地图 v3 自定义标记图标无法保持其在地图上的位置

发布于 2025-01-06 09:50:35 字数 422 浏览 4 评论 0原文

使用 Google 地图 v3 进行开发。

由于某种原因,我的自定义标记图标“更改”了它在放大时的位置。看起来它有某种“填充”属性,不会随着缩放而改变。

这意味着它在最大缩放(18)上的位置是正确的,但是如果我更改缩放值,它会“移动”一点到顶部,并且在较小的缩放值上会出现问题,因为它看起来与这是。

标记定义为:

var image = new google.maps.MarkerImage('img/antennas/img.png',new google.maps.Size(100, 100));

这可能会有所帮助:标记图标是方形的,100x100px,它的中心位于图像的中间,而不是像“正常”标记那样位于底部。

更新:我是否必须对锚点属性执行某些操作?

Developing with Google Maps v3.

For some sort of reason, my custom marker icon "change" it's position on zoom in-out. It looks like it have some sort of "padding" property, that not changes together with zoom.

It means that it position is correct on maximum zoom (18), but if I change zoom value, it "moves" a bit to top, and it makes problem on smaller zoom values, because it looks like it is not on same position as it is.

Marker is defined as:

var image = new google.maps.MarkerImage('img/antennas/img.png',new google.maps.Size(100, 100));

This maybe can help: marker icon is squared shape, 100x100px, and it's center is in middle of the image, not on the bottom like "normal" markers.

UPDATE: do I have to do something with anchor property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

王权女流氓 2025-01-13 09:50:35

您必须设置标记的锚点。默认为中心底部。

请参阅 http://code.google.com/apis/maps/文档/javascript/reference.html#MarkerImage

You have to set the anchor of the marker. The default is center bottom.

See http://code.google.com/apis/maps/documentation/javascript/reference.html#MarkerImage

梦旅人picnic 2025-01-13 09:50:35

除了使用 Marker 之外,还可以使用 MarkerImage 作为标记。

在此示例中,我使用的标记是一个中间有一个点的圆,因此我始终希望它居中。

例子

 var marker_image = new google.maps.MarkerImage(
        '../Media/icon_maps_marker.png',
         null,
         // The origin for my image is 0,0.
         new google.maps.Point(0, 0),
         // The center of the image is 50,50 (my image is a circle with 100,100)
         new google.maps.Point(50, 50)
     );
    var marker = new google.maps.Marker({
        clickable: true,
        map: map,
        position: center_location,
        icon: marker_image,
    });

Instead of using only a Marker, use also a MarkerImage, to be used as the marker.

In this example I use a mark that is a circle with a point in the middle, so I always want it centered.

Example

 var marker_image = new google.maps.MarkerImage(
        '../Media/icon_maps_marker.png',
         null,
         // The origin for my image is 0,0.
         new google.maps.Point(0, 0),
         // The center of the image is 50,50 (my image is a circle with 100,100)
         new google.maps.Point(50, 50)
     );
    var marker = new google.maps.Marker({
        clickable: true,
        map: map,
        position: center_location,
        icon: marker_image,
    });
梦毁影碎の 2025-01-13 09:50:35

根据您对“填充”的描述,这听起来像是您的 MarkerImage 的定位问题。尝试调整 MarkerImage 的锚点属性。默认情况下,锚点位于图像的底部中心。如果您希望图像居中,则必须将锚点向下移动图像大小的一半以使其居中。

请参阅 http://code.google.com/ intl/no/apis/maps/documentation/javascript/reference.html#MarkerImage 供参考。

Based on your description of "padding", this sounds like it is an issue with the positioning of your MarkerImage. Try tweaking the anchor property of the MarkerImage. By default, the anchor is at the bottom center of your image. If you want the image to be centered, you will have to move the anchor down half the size of the image to center it.

See http://code.google.com/intl/no/apis/maps/documentation/javascript/reference.html#MarkerImage for reference.

瑾夏年华 2025-01-13 09:50:35

查看图标中的anchor属性:anchor: new google.maps.Point(0, 0)

var icon = {
    url: image,
    anchor: new google.maps.Point(0, 0)
  };

  marker = new google.maps.Marker({
    position: Latlng,
    map: map,
    icon: icon,
    optimized: false
  });

See the anchor property in icon: anchor: new google.maps.Point(0, 0)

var icon = {
    url: image,
    anchor: new google.maps.Point(0, 0)
  };

  marker = new google.maps.Marker({
    position: Latlng,
    map: map,
    icon: icon,
    optimized: false
  });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文