Javascript 更改谷歌地图方向 api V3 中的单个标记
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4813728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change individual markers in google maps directions api V3
提问by Corey Hart
I'm looking to change the marker icons when using the DirectionsRender within a google map. I've figured out from herehow to change both the markers to the same icon, but I am looking for custom icons on both the start and end points. Any ideas?
我希望在谷歌地图中使用 DirectionsRender 时更改标记图标。我从这里想出了如何将两个标记更改为相同的图标,但我正在寻找起点和终点的自定义图标。有任何想法吗?
Edit: I'm looking for how to assign separate icons to the start and end markers. I know how to change it for both, but having different marker icons is proving difficult.
编辑:我正在寻找如何为开始和结束标记分配单独的图标。我知道如何为两者更改它,但事实证明使用不同的标记图标很困难。
回答by Corey Hart
For those that need an example like I did, here's a basic one:
对于那些像我一样需要示例的人,这是一个基本示例:
// Map and directions objects
var map = new google.maps.Map( element, options );
var service = new google.maps.DirectionsService();
var directions = new google.maps.DirectionsRenderer({suppressMarkers: true});
// Start/Finish icons
var icons = {
start: new google.maps.MarkerImage(
// URL
'start.png',
// (width,height)
new google.maps.Size( 44, 32 ),
// The origin point (x,y)
new google.maps.Point( 0, 0 ),
// The anchor point (x,y)
new google.maps.Point( 22, 32 )
),
end: new google.maps.MarkerImage(
// URL
'end.png',
// (width,height)
new google.maps.Size( 44, 32 ),
// The origin point (x,y)
new google.maps.Point( 0, 0 ),
// The anchor point (x,y)
new google.maps.Point( 22, 32 )
)
};
service.route( { origin: origin, destination: destination }, function( response, status ) {
if ( status == google.maps.DirectionsStatus.OK ) {
display.setDirections( response );
var leg = response.routes[ 0 ].legs[ 0 ];
makeMarker( leg.start_location, icons.start, "title" );
makeMarker( leg.end_location, icons.end, 'title' );
}
});
function makeMarker( position, icon, title ) {
new google.maps.Marker({
position: position,
map: map,
icon: icon,
title: title
});
}
The response from a route request returns a leg(s) depending on the number of stops on your route. I am only doing a A to B route, so just take the first leg, and get the position of where the markers need to go, and create markers for those spots.
来自路线请求的响应根据您路线上的停靠点数量返回一个或多个航段。我只做 A 到 B 的路线,所以只需走第一条路,并获得标记需要去的位置,并为这些点创建标记。
回答by aniri
Do as they say on that page you linked to:
按照他们在您链接到的那个页面上说的做:
- Set the suppressMarkersoption to true to prevent the default start and end markers from showing
- Create the imagesfor the two new markers
- Create the markerswith the position set to the start and end positions, and the icon set to the ones you created
- 将suppressMarkers选项设置为true 以防止显示默认的开始和结束标记
- 为两个新标记创建图像
- 创建标记,位置设置为开始和结束位置,图标设置为您创建的标记