简体   繁体   English

更改谷歌地图方向api V3中的单个标记

[英]Change individual markers in google maps directions api V3

I'm looking to change the marker icons when using the DirectionsRender within a google map. 我想在谷歌地图中使用DirectionsRender时更改标记图标。 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? 有任何想法吗?

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. 我知道如何为两者改变它,但是使用不同的标记图标证明是困难的。

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的路线,所以只需要抓住第一条腿,然后获得标记需要去的位置,并为这些点创建标记。

Do as they say on that page you linked to: 按照他们在您关联的页面上说的那样:

  • Set the suppressMarkers option to true to prevent the default start and end markers from showing suppressMarkers选项设置为true可防止显示默认的开始和结束标记
  • Create the images for the two new markers 为两个新标记创建图像
  • Create the markers with the position set to the start and end positions, and the icon set to the ones you created 创建位置设置为开始和结束位置的标记 ,并将图标设置为您创建的标记

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

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