MapStyle更改时,GoogleMaps会更改图标的标记

Dio*_*ong 7 javascript google-maps google-maps-api-3

我正在我的html/jsp页面中使用Google地图进行Web动态项目.

由于(lat,lng,map)我创建了一个创建标记的函数,并在标记的参数中使用特殊的image.png作为图标.

在我的地图中,我制作了两种不同的风格(地图的颜色......):"日"和"夜".

我想知道当用户点击Night改变样式时如何更改标记的图标.实际上,标记的颜色对于这种风格的地图并不好......

我尝试使用相同的名称初始化不同样式的var image = /.../...png,所以我可以在地图代码中使用var,但它不起作用.我也试过了

if(map.mapTypeControlOptions.mapTypeIds.equals(Day)){
    var image=...png
} else {
    var image=...png
}...

customMapTypeIdJour<div id="map"></div>

<script>
function initMap() {
    var customMapTypeNuit = new google.maps.StyledMapType([
        {
            "featureType": "landscape.man_made", "elementType": "geometry.fill", "stylers": [ { "color": "#2b3f57" } ]
        },                                                    
        //  ... the style of map
        {  
            name: 'Nuit'
        }
    );

    var customMapTypeJour = new google.maps.StyledMapType([
        //  a style of map
        {  
            name: 'Jour'
        }
    );

    var customMapTypeIdJour = 'Jour';
    var customMapTypeIdNuit = 'Nuit';
    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 43.6666, lng: 1.43333},
        zoom: 17,
        streetViewControl: false,
        zoomControl: false,
        mapTypeControlOptions: {
            mapTypeIds: [customMapTypeIdJour, customMapTypeIdNuit]
        }
    });
    map.mapTypes.set(customMapTypeIdNuit, customMapTypeNuit);
    map.mapTypes.set(customMapTypeIdJour, customMapTypeJour);
    map.setMapTypeId(customMapTypeIdJour);
    var infoWindow = new google.maps.InfoWindow({map: map});

    // Try HTML5 geolocation.
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            var pos = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
            };

            infoWindow.setPosition(pos);
            infoWindow.setContent('Vous êtes ici.');

            createMarqueur(lat, lng, map);    // create a special marker with a special image as icon

            map.setCenter(pos);
        });
    } else {
        // Browser doesn't support Geolocation
        handleLocationError(false, infoWindow, map.getCenter());
    }
}

function handleLocationError(browserHasGeolocation, infoWindow, pos) {
    infoWindow.setPosition(pos);
    infoWindow.setContent(browserHasGeolocation ?
        'Error: The Geolocation service failed.' :
        'Error: Your browser doesn\'t support geolocation.');
}
</script>
Run Code Online (Sandbox Code Playgroud)

Buz*_*age 7

我开始赏金,但最后我得到了答案.根据谷歌的文档,getMapTypeId()返回一个MapTypeId字符串(如'Jour'或'Nuit'(salut compatriote!)).

所以,如果你持有,markerList你可以这样做:

google.maps.event.addListener( map, 'maptypeid_changed', function() {
    if(map.getMapTypeId() == "Nuit"){
      for(var i=0;i<markerList.length;i++){
        markerList[i].setIcon("/resources/img/nuit.png");
      }
    }
    else{
      for(var i=0;i<markerList.length;i++){
        markerList[i].setIcon("/resources/img/jour.png");
      }
    }
} );
Run Code Online (Sandbox Code Playgroud)

根据这个答案,maptypeid_changedmapTypeId财产变化时发出的信号.


Jak*_*uda 5

你尝试过使用svg图形吗?您可以使用javascript轻松更改其外观.

您必须使用Corel Draw,Adobe Illustrator或任何其他免费程序等矢量编辑器软件制作标记的svg.在文本编辑器中打开生成的文件,您会发现类似的内容.

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 25000 6000">
    <path d=" <!--svg path here--> ">
</svg>
Run Code Online (Sandbox Code Playgroud)

你申请css:

svg path{fill:red} /*any color here*/ 
Run Code Online (Sandbox Code Playgroud)

您希望使用javascript更改颜色所做的任何更改都可以使用上述css的简单更改来完成.我希望我能告诉你如何改变标记.