最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I change the Start and End marker image in Google Maps v3 API for Directions - Stack Overflow

programmeradmin2浏览0评论

I have a route plotted fine on using DirectionsRender but I cannot find out how to replace the generic Google markers with my own.

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

Michael

I have a route plotted fine on using DirectionsRender but I cannot find out how to replace the generic Google markers with my own.

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

Michael

Share Improve this question edited Nov 30, 2010 at 15:46 August Lilleaas 54.6k13 gold badges105 silver badges112 bronze badges asked Nov 30, 2010 at 10:59 Michael LawMichael Law 3072 gold badges6 silver badges12 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

The DirectionRender takes an option called markerOptions. Quoting from the API docs:

All markers rendered by the DirectionsRenderer will use these options.

So, if you want to set the markers use MarkerImage (as philar has indicated).

Your other option is to pass suppressMarkers: true to the DirectionRender's options and then simply use your own markers.

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
            });

First you need to add this on your 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!

发布评论

评论列表(0)

  1. 暂无评论