20

按照谷歌文档更改标记图标很容易。但是我被卡住了,我不知道单击标记时如何更改图像?

到目前为止我的代码:

<script>
  function initialize() {

    var latlngPos = new google.maps.LatLng(<?php echo $event_google_map_coordinates; ?>);

    var mapOptions = {
      zoom: 16,
      center: latlngPos,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    var map = new google.maps.Map(document.getElementById('googlemap_event'),
      mapOptions);

    var marker = new google.maps.Marker({
      position: latlngPos,
      map: map,
      //animation: google.maps.Animation.BOUNCE,
      icon: '<?= get_bloginfo("template_url"); ?>/images/marker.png'
    });

    var infowindow = new google.maps.InfoWindow({
      position: latlngPos,
      maxWidth: 200,
      content: "<h3><?php the_title(); ?></h3><?php if ($event_address_meta) {_e($event_address_meta);} ?>"
    });

    //open onclick
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map);
    });

    //open infowindow onload
    //infowindow.open(map);

  }

function loadScript() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'http://maps.googleapis.com/maps/api/js?key=AIzaSyCK3XUMWOH0GIiuj3VeprakKZXoo_nDV08&sensor=false&' +
    'callback=initialize';
  document.body.appendChild(script);
}

window.onload = loadScript; < /script>
<div id="googlemap_event"></div>
4

3 回答 3

43

试试这个。

//open onclick
   google.maps.event.addListener(marker, 'click', function() {
    marker.setIcon("Your Image");                                    
    infowindow.open(map);
    });
于 2013-03-20T12:27:00.203 回答
10

您需要在点击事件中设置标记图标。

google.maps.event.addListener(marker, 'click', function() {
          infowindow.open(map);
          //Change the marker icon
          marker.setIcon('https://www.google.com/mapfiles/marker_green.png');
     });

这是一个例子:jsfiddle

于 2013-03-20T12:38:53.320 回答
6

我已经修改了以前的小提琴并启用取消选择标记。

http://jsfiddle.net/zsw76pq9/

google.maps.event.addListener( map,'click', function () {
    infoWindow.close();

    //Change the marker icon
    marker.setIcon('https://www.google.com/mapfiles/marker_black.png');
});
于 2017-12-21T17:01:45.743 回答