在谷歌文档之后更改标记图标很容易。但我被卡住了,我不知道如何在点击标记时更改图像?
到目前为止我的代码:
<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>
答案 0 :(得分:33)
试试这个。
//open onclick
google.maps.event.addListener(marker, 'click', function() {
marker.setIcon("Your Image");
infowindow.open(map);
});
答案 1 :(得分:9)
您需要在点击事件中设置标记图标。
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
答案 2 :(得分:2)
我修改了之前的小提琴并启用了取消选择标记。
google.maps.event.addListener( map,'click', function () {
infoWindow.close();
//Change the marker icon
marker.setIcon('https://www.google.com/mapfiles/marker_black.png');
});