Changing Google Maps V3 Maker Icon the correct way?

别来无恙 提交于 2019-12-25 02:16:07

问题


The code in example 1 works. I would like to understand the marker.setIcon(); function more (new to JavaScript also).

My question is. In the documentation for Google maps you see something like this for changing the marker.

MarkerImage(url:string, size?:Size, origin?:Point, anchor?:Point, scaledSize?:Size)

How does this relate to what I have done in example 1 for setting up marker Icon, shoud I have done somethign like this instead?

marker = google.maps.MarkerImage({
        url: "newIcon.png"
});

marker.setIcon(marker);

and would that have worked?

here is my example

Example 1

function initialize(){
//MAP

  var latlng = new google.maps.LatLng('xxx','xxx');
  var options = {
    zoom: 16,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.SATELLITE
  };

  map = new google.maps.Map(document.getElementById("map_canvas"), options);

  //GEOCODER
  geocoder = new google.maps.Geocoder();

  marker = new google.maps.Marker({
    map: map,
    draggable: true
  });     

  marker.setPosition(latlng);
  marker.setIcon("newIcon.png");
  map.setCenter(latlng);

}

回答1:


You're giving a V2 answer for a V3 question.

There is no GIcon in V3.

var image = new google.maps.MarkerImage("newIcon.png");

Can be used inside your marker as the icon.

var marker = new google.maps.Marker({
        position: new google.maps.LatLng(lat,lng),
        icon:image
    });


来源:https://stackoverflow.com/questions/7965288/changing-google-maps-v3-maker-icon-the-correct-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!