【问题标题】:JavaScripts to show multi locations on map用于在地图上显示多个位置的 JavaScript
【发布时间】:2011-11-28 09:08:17
【问题描述】:

我编写了这段代码来显示 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;

【问题讨论】:

  • RightMove 项目有机会吗?我遇到了谷歌地图的问题,它不会为每个标记提供正确的标签,它只会为每个标记提供相同的标签。有时地图很奇怪。
  • GUnload?? (在最后一行)- 这是 API v2!

标签: javascript google-maps-api-3


【解决方案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',

【讨论】:

    【解决方案2】:

    我建议和 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.

    【讨论】:

    • 非常感谢 Tomas.. 但我不知道为什么我看不到图标的阴影
    猜你喜欢
    • 1970-01-01
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-14
    • 1970-01-01
    相关资源
    最近更新 更多