【问题标题】:Google Maps API - Offset for Marker Image - Replace Marker - Not at GeoPositionGoogle Maps API - 标记图像的偏移量 - 替换标记 - 不在 GeoPosition
【发布时间】:2012-08-19 18:22:10
【问题描述】:

我有一张图片(一个圆圈),应该表示一个特定的大都市区。我也有我所有相关领域的GeoPosition

如果我放置标记,则图像的底部中心位于标记的GeoPosition。但是因为它是鸟瞰图,所以我想给图像一个偏移量,所以我的圆形图像的 center 在那个 GeoPosition...

任何想法如何实现这一目标?

谢谢!

【问题讨论】:

    标签: android google-maps-api-3 google-maps-markers


    【解决方案1】:

    感谢 LeJared 我找到了解决方案。我的问题是我认为MarkerImage 取代了Marker。但是您必须先定义图像,然后将其插入Marker

    示例:

    首先定义图像:

    var image = new google.maps.MarkerImage("some/path/image.png",
            // This marker is 20 pixels wide by 32 pixels tall.
            null, 
            // The origin for this image is 0,0.
            new google.maps.Point(0,0),
            // The anchor for this image is the base of the flagpole at 0,32.
            new google.maps.Point(75, 35)
        );
    

    然后将其插入到常规标记中:

        var myMarker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            icon: image,
            title: "My Title",
        });
    

    【讨论】:

    • 正是我想要的。谢谢!
    • 谢谢!对于那些仍在寻找解决方案的人,请在下面查看我的答案,了解在新 API 中的解决方法。
    【解决方案2】:

    从 Google API 3.11 版开始,MarkerImage was replaced by Icon。所以这是将标记缩放到 50px 并将其定位以使其 center 位于地理位置上的新方法:

    var image = {
            url: '/some/path/image.png',
            origin: new google.maps.Point(0, 0),
            anchor: new google.maps.Point(25, 25),
            scaledSize: new google.maps.Size(50, 50)
        };
    
    var myMarker = new google.maps.Marker({
          position: position,
          icon: image,
          title: 'title',
          map: map
        });
    

    【讨论】:

      【解决方案3】:

      您必须指定 markerImage 中的锚点在哪里。请参阅文档:https://developers.google.com/maps/documentation/javascript/reference?hl=de#MarkerImage

      如果没有指定,谷歌假设你的 markerImage 底部边缘的中心作为锚点。

      这是 API 文档中的一个示例,如何使用正确的 markerImage 构建复杂的标记:https://google-developers.appspot.com/maps/documentation/javascript/examples/icon-complex?hl=de

      【讨论】:

      • 我读过这个,但anchor 似乎对我不起作用......这是我标记定义中的正确用法吗? anchor: new google.maps.Point(30,0)
      • 啊!我使用的是Marker,而不是MarkerImage。标记没有这样的属性......现在我只需要弄清楚如何让我的MarkerImage 工作:)
      • 嗯,上面的例子有你需要的一切,而且代码不多,所以你可以很容易地弄清楚,或者只是为你自己的项目采用代码。
      【解决方案4】:

      这是我的看法。

      new google.maps.Marker({
        position: pos,
        map: map,
        icon: {
          url: src,
          anchor: new google.maps.Point(25, 25),
          origin: new google.maps.Point(0, 0),
          scaledSize: new google.maps.Size(50, 50)
        }
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-22
        相关资源
        最近更新 更多