使用Google maps API v3在地图上放置不同颜色的标记

Drop different coloured markers on map using Google maps API v3

本文关键字:颜色 maps Google API v3 地图 使用      更新时间:2023-09-26

代码中的标记代表车辆,现在我想做的是通过标记的颜色(分别为绿色,红色,黄色)来检查车辆的状态(移动,静止,停用)。我得到的是,当我点击按钮看到所有的标记,所有的标记出现,是相同的颜色(红色),当我把鼠标悬停在这些标记,他们显示"5",这是阵列中的最后一个标记。我知道我正在做一些蹩脚的错误,可以有人请看看我的代码,告诉我我错过了什么?我的for循环有一些错误。

https://jsfiddle.net/va87foja/1/

代码片段(来自jsfiddle):

var map;
var all = [
	{"name": '0', "lat": 24.92650881416285, "lng": 67.10869789123535, "status": 'Deactivated'},
	{"name": '1', "lat": 24.845937391711946, "lng": 66.73162758350372, "status": 'Stationary'},
	{"name": '2', "lat": 24.89372598975854, "lng": 67.08657503128052, "status": 'Deactivated'},
	{"name": '3', "lat": 24.803024353889768, "lng": 67.02959954738617, "status": 'Moving'},
	{"name": '4', "lat": 25.032825073841558, "lng": 66.8778133392334, "status": 'Moving'},
	{"name": '5', "lat": 24.91757686884615, "lng": 67.19997346401215, "status": 'Stationary'}
];
var markers = [];
function initialize() {
	  map = new google.maps.Map(document.getElementById("map"), {
	  center: new google.maps.LatLng(24.89372598975854,67.08657503128052),
	  zoom: 11,
	  mapTypeId: google.maps.MapTypeId.TERRAIN
	});
}
function drop(){
	//end.setVisible(false);
	map.setZoom(10);
	clearMarkers();			
	for (var j = 0; j < all.length; j++) {
  		addMarker(all[j], j * 200);
	}
//	stop();
}
function clearMarkers(){
	for (var j = 0; j < markers.length; j++) {
 		markers[j].setMap(null);
  	}
  	markers = [];
}
function addMarker(position, timeout){
	var icon="";
	for(var k=0; k<all.length; k++)
	{
		var data = all[k];
		switch(data.status){
			case "Stationary":
			  icon = "red";
			  break;
			case "Moving":
			  icon = "green";
			  break;
			case "Deactivated":
			  icon = "yellow";
			  break;
		}
	}
	icon="http://maps.google.com/mapfiles/ms/micons/" + icon + ".png";
	window.setTimeout(function() {
  		markers.push(new google.maps.Marker({
    		position: position,
    		map: map,
			title: data.name,
    		animation: google.maps.Animation.DROP,
			icon: new google.maps.MarkerImage(icon)
  		}));
  	}, timeout);
}
google.maps.event.addDomListener(window, 'load', initialize);
<script src="https://maps.googleapis.com/maps/api/js"></script>
<center>
	<br><br>
	<input onclick="drop();" type=button value="Show All">
	<br><br>
	<div id="map" style="width:1000px;height:460px;"></div>
</center>

也许你想要这样的东西:

   var map;
        var all = [
            { "name": '0', "lat": 24.92650881416285, "lng": 67.10869789123535, "status": 'Deactivated' },
            { "name": '1', "lat": 24.845937391711946, "lng": 66.73162758350372, "status": 'Stationary' },
            { "name": '2', "lat": 24.89372598975854, "lng": 67.08657503128052, "status": 'Deactivated' },
            { "name": '3', "lat": 24.803024353889768, "lng": 67.02959954738617, "status": 'Moving' },
            { "name": '4', "lat": 25.032825073841558, "lng": 66.8778133392334, "status": 'Moving' },
            { "name": '5', "lat": 24.91757686884615, "lng": 67.19997346401215, "status": 'Stationary' }
        ];
        var markers = [];
        function initialize() {
            map = new google.maps.Map(document.getElementById("map"), {
                center: new google.maps.LatLng(24.89372598975854, 67.08657503128052),
                zoom: 11,
                mapTypeId: google.maps.MapTypeId.TERRAIN
            });
        }
        function drop() {
            //end.setVisible(false);
            map.setZoom(10);
            clearMarkers();
            for (var j = 0; j < all.length; j++) {
                addMarker(all[j], j * 200);
            }
            //	stop();
        }
        function clearMarkers() {
            for (var j = 0; j < markers.length; j++) {
                markers[j].setMap(null);
            }
            markers = [];
        }
        function addMarker(data, timeout) {
            var icon = "";
            switch (data.status) {
            case "Stationary":
                icon = "red";
                break;
            case "Moving":
                icon = "green";
                break;
            case "Deactivated":
                icon = "yellow";
                break;
            }
            icon = "http://maps.google.com/mapfiles/ms/micons/" + icon + ".png";
            window.setTimeout(function() {
                markers.push(new google.maps.Marker({
                    position: { 'lat': data.lat, 'lng': data.lng },
                    map: map,
                    title: data.name,
                    animation: google.maps.Animation.DROP,
                    icon: new google.maps.MarkerImage(icon)
                }));
            }, timeout);
        }
        google.maps.event.addDomListener(window, 'load', initialize);
html, body {
            height: 100%;
            margin: 0;
            padding: 0;
}
#map {
            height: 100%;
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<input onclick="drop();" type=button value="Show All">
<div id="map" style="width:1000px;height:460px;"></div>

好的,我做到了。当我将@Vadim Gremyachev提供的代码与我的原始代码集成时,出现了一些问题,但现在它可以工作了。

var map;
var all = [
	{"name": '0', "lat": 24.92650881416285, "lng": 67.10869789123535, "status": 'Deactivated'},
	{"name": '1', "lat": 24.845937391711946, "lng": 66.73162758350372, "status": 'Stationary'},
	{"name": '2', "lat": 24.89372598975854, "lng": 67.08657503128052, "status": 'Deactivated'},
	{"name": '3', "lat": 24.803024353889768, "lng": 67.02959954738617, "status": 'Moving'},
	{"name": '4', "lat": 25.032825073841558, "lng": 66.8778133392334, "status": 'Moving'},
	{"name": '5', "lat": 24.91757686884615, "lng": 67.19997346401215, "status": 'Stationary'}
];
var markers = [];
function initialize() {
	  map = new google.maps.Map(document.getElementById("map"), {
	  center: new google.maps.LatLng(24.89372598975854,67.08657503128052),
	  zoom: 11,
	  mapTypeId: google.maps.MapTypeId.TERRAIN
	});
}
function drop(){
	//end.setVisible(false);
	map.setZoom(10);
	clearMarkers();			
	for (var j = 0; j < all.length; j++) {
  		addMarker(all[j], j * 200);
	}
//	stop();
}
function clearMarkers(){
	for (var j = 0; j < markers.length; j++) {
 		markers[j].setMap(null);
  	}
  	markers = [];
}
function addMarker(position, timeout){
	var icon="";
	var data = position;
    switch(data.status){
		case "Stationary":
		  icon = "red";
		  break;
		case "Moving":
		  icon = "green";
		  break;
		case "Deactivated":
		  icon = "yellow";
		  break;
	}
	icon="http://maps.google.com/mapfiles/ms/micons/" + icon + ".png";
	window.setTimeout(function() {
  		markers.push(new google.maps.Marker({
    		position: position,
    		map: map,
			title: data.name,
    		animation: google.maps.Animation.DROP,
			icon: new google.maps.MarkerImage(icon)
  		}));
  	}, timeout);
}
google.maps.event.addDomListener(window, 'load', initialize);
    <script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<center>
	<br><br>
	<input onclick="drop();" type=button value="Show All">
	<br><br>
	<div id="map" style="width:1000px;height:460px;"></div>
</center>

https://jsfiddle.net/va87foja/2/