简体   繁体   English

Google Maps API:标记图像定位

[英]Google maps API: Marker image positioning

I have changed the image that I use for a marker on Google maps. 我已经更改了用于Google地图上marker的图像。 The new image is much wider than the old one and I have noticed that the marker is aligned with the lat and lng so that the marker rests with it's horizontal midpoint over the lat and lng . 新图像比旧图像宽得多,我注意到标记与latlng对齐,因此标记位于latlng的水平中点。 This is not what I want, I want to have the lat and lng aligned with the markers left hand side - I want to offset the marker from the default position by about 80px to the right. 这不是我想要的,我想让latlng与标记的左侧对齐-我想将marker从默认位置向右偏移80像素。

Try this on for size. 试试看它的大小。

var markerImage = new google.maps.MarkerImage('/img/icon/here.png',
    new google.maps.Size(80, 80), //size
    new google.maps.Point(0, 0), //origin point
    new google.maps.Point(0, 80)); // offset point
marker[stamp] = new google.maps.Marker({
    position: loc, // loc is a variable with my lngLat object
    title: params.name,
    map: window.gMaps[params.map],
    icon: markerImage
});

You want the 'anchor' property of MarkerImage . 您需要MarkerImage的'anchor'属性。 Note that the origin property comes before anchor in the constructor call. 请注意,origin属性在构造函数调用的锚点之前。 If you're not using sprites, you can just send null for the optional parameters. 如果您不使用精灵,则可以为可选参数发送null。

If your marker is 80x60 and you want the anchor in the middle of the left side, create it like this: 如果您的标记是80x60,并且您希望锚点位于左侧的中间,请按以下方式创建它:

var markerImage = new google.maps.MarkerImage('customIcon.png', new google.maps.Size(80, 60), null, new google.maps.Point(0, 30));

See also Google's example . 另请参阅Google的示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM