JavaScripts在地图上显示多个位置

时间:2011-11-28 09:08:17

标签: javascript google-maps-api-3

我已编写此代码以在d地图上显示多个位置。但是我希望标记在location[i].visit_status为0时为红色,如果location[i].visit_status为1则为绿色(我的机器中有绿色和红色图标)所以我写了这个代码但是有一个原因我不知道,它不起作用。有人可以帮我吗?

以下是代码:

var locations=[{"id_retailer":"3","retailer_name":"ret1","retailer_latitude":"3.083826","retailer_longitute":"101.731689","visit_date":"2011-11-11","visit_status":"0"}];

var map = new google.maps.Map(document.getElementById('map'), {

  zoom: 8,

  center: new google.maps.LatLng(2.67968661580376,102.23876953125),

  mapTypeId: google.maps.MapTypeId.ROADMAP

});


var infowindow = new google.maps.InfoWindow();
var marker, i;

for (i = 0; i < locations.length; i++) {

  marker = new google.maps.Marker({

    position: new google.maps.LatLng(locations[i].retailer_latitude, locations[i].retailer_longitute),

    map: map,
    if(locations[i].visit_status)
      icon: 'images/1.png';
    else
      icon: 'images/0.png';
    title: locations[i].retailer_name

  });

  google.maps.event.addListener(marker, 'click', (function(marker, i) {

    return function() {
      infowindow.setContent("Name: "+locations[i].retailer_name);
      infowindow.open(map, marker);
    }

  })(marker, i));

}

window.onunload=GUnload;

2 个答案:

答案 0 :(得分:1)

更改此语法

 map: map,
    if(locations[i].visit_status)
      icon: 'images/1.png';
    else
      icon: 'images/0.png';

另一个人:

 map: map,
 icon: locations[i].visit_status ? 'images/1.png':'images/0.png',

答案 1 :(得分:0)

我建议和Adilson一样,而且,不要忘记设置图标阴影 - 这是标准图标阴影的示例(如果您使用与标准阴影具有相同形状的图标):

icon: locations[i].visit_status ? 'images/1.png' : 'images/0.png',
shadow: new google.maps.MarkerImage(
    'http://maps.gstatic.com/mapfiles/shadow50.png',
    null,
    null,
    new google.maps.Point(10, 34)
)

我想知道一些更好的方法来指定默认阴影,但是I don't know - see this question.