问题描述
我希望在谷歌地图中使用 DirectionsRender 时更改标记图标.我已经从 此处 如何将两个标记更改为相同的图标,但我正在寻找起点和终点的自定义图标.有什么想法吗?
I'm looking to change the marker icons when using the DirectionsRender within a google map. I've figured out from here how 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?
我正在寻找如何为开始和结束标记分配单独的图标.我知道如何为两者更改它,但事实证明使用不同的标记图标很困难.
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.
推荐答案
对于那些像我一样需要示例的人,这里有一个基本的:
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
});
}
来自路线请求的响应会根据您路线上的停靠点数量返回一个或多个航段.我只做 A 到 B 的路线,所以只走第一条路,然后找到标记需要去的位置,然后为这些点创建标记.
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.
这篇关于更改谷歌地图方向 api V3 中的单个标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!