简体   繁体   English

如何更改Google Maps v3 API for Directions中的开始和结束标记图像

[英]How do I change the Start and End marker image in Google Maps v3 API for Directions

I have a route plotted fine on using DirectionsRender but I cannot find out how to replace the generic Google markers with my own. 我使用DirectionsRender绘制的路线很好但我无法找到如何用我自己的标记替换通用的Google标记。

I know and use this in a normal Google Map situation but finding it difficult to do this with the directions markers for start and end. 我知道并在正常的谷歌地图情况下使用它,但发现使用开始和结束的方向标记很难做到这一点。

Thanks for any advice, pointers or gentle mocking if this is a silly question :D 如果这是一个愚蠢的问题,感谢任何建议,指点或温和的嘲笑:D

Michael 迈克尔

The DirectionRender takes an option called markerOptions . DirectionRender采用名为markerOptions的选项。 Quoting from the API docs: 引用API文档:

All markers rendered by the DirectionsRenderer will use these options. DirectionsRenderer呈现的所有标记都将使用这些选项。

So, if you want to set the markers use MarkerImage (as philar has indicated). 所以,如果你想设置的标志使用MarkerImage (如philar曾表示)。

Your other option is to pass suppressMarkers: true to the DirectionRender 's options and then simply use your own markers. 您的另一个选择是将suppressMarkers: true传递给DirectionRender的选项,然后只使用您自己的标记。

First you need to add this on your DirectionsRenderer 首先,您需要在DirectionsRenderer上添加此项

directionsDisplay = new google.maps.DirectionsRenderer({suppressMarkers: true}); 
//to hide the default icons

then after options etc... 然后选择等...

map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
//you set your custom image
var image = new google.maps.MarkerImage('images/image.png',
                    new google.maps.Size(129, 42),
                    new google.maps.Point(0,0),
                    new google.maps.Point(18, 42)
                );

//you set your icon for each of the direction points Origin
                var marker1 = new google.maps.Marker({
                    position: new google.maps.LatLng(19.432651,-99.133201),
                    map: map,
                    icon: image
                });
//you set your icon for each of the direction points Destination,
                var marker2 = new google.maps.Marker({
                    position: new google.maps.LatLng(45.508648,-73.55399),
                    map: map,
                    icon: image
                });

you can also add different icons for origin and destination. 您还可以为原点和目的地添加不同的图标。 Just play with the var image, it works for me! 只需使用var图像,它对我有用!

this is how you need to approach it 这就是你需要接近它的方法

Declare all your image icons as shown below 声明所有图像图标,如下所示

var movingIcon = new google.maps.MarkerImage('/img/icon_moving.jpg');
var startIcon = new google.maps.MarkerImage('/img/icon_start.png');

Then while creating the marker, use the icon option to set the specific image to that marker 然后在创建标记时,使用图标选项将特定图像设置为该标记

marker = new google.maps.Marker({
            position: point,
            map: map,
            icon: movingIcon
            });

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

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