0

我知道这已经在网上被问了很多次了。但是我仍然无法解决这个问题,已经有几天了,但我仍然无法解决。我对这个谷歌地图 API 还是很陌生,也许有一些我混淆了。

以下是我当前的代码:

var places = [
              ['Car Park', 1.301526,103.849379, 4],
              ['Bus Stop', 1.301132,103.850077, 3],
              ['MRT', 1.298515,103.845785, 2],
              ['Success Human Resource Center', 1.301486,103.849712, 1]
            ];

            var companyImage0 = new google.maps.MarkerImage('images/map-icon.png',
                new google.maps.Size(54,62),
                new google.maps.Point(0,0),
                new google.maps.Point(50,50)
            );

            var companyImage1 = new google.maps.MarkerImage('images/MRT.png',
                new google.maps.Size(30,30),
                new google.maps.Point(0,0),
                new google.maps.Point(50,50)
            );

            var companyImage2 = new google.maps.MarkerImage('images/bus-stop.png',
                new google.maps.Size(30,33),
                new google.maps.Point(0,0),
                new google.maps.Point(50,50)
            );

            var companyImage3 = new google.maps.MarkerImage('images/car-park.png',
                new google.maps.Size(30,30),
                new google.maps.Point(0,0),
                new google.maps.Point(50,50)
            );

            var companyShadow = new google.maps.MarkerImage('images/logo_shadow.png',
                new google.maps.Size(54,62),
                new google.maps.Point(0,0),
                new google.maps.Point(65, 50)
            );

            /*var companyPos = new google.maps.LatLng(1.301486,103.849712);*/

            for (var i = 0; i < places.length; i++) {
            var place = places[i];
            var myLatLng = new google.maps.LatLng(place[1],place[2]);
            var marker = new google.maps.Marker({
                position: myLatLng,
                map: map,
                shadow: companyShadow,
                icon: companyImage[i],
                title: 'Singapore',
                zIndex: places[2][2]});
            }

除此之外,我只想在用户单击 SHRC 标记时显示信息窗口,单击标记时巴士站和 MRT 将一无所有。

感谢任何可以帮助我解决这个问题的好心人。提前致谢。

4

1 回答 1

1

我不太确定您要在这里做什么,因为您对收到的错误只字未提。但是,您的代码中有三个错误:

CompanyMarker尚未初始化,并且您为 z-index 使用了错误的变量名称和语法。请记住,数组是从零开始的索引,所以

places[2] // will retrieve the SHRC row data
places[2][2] //will retrive the Z-index number from the SHRC row

因此,请尝试以下更改:

var places = [
  ['Bus Stop', 1.301132,103.850077, 3],
  ['MRT', 1.298515,103.845785, 2],
  ['SHRC', 1.301486,103.849712, 1]
];

//change marker to companyMarker
var companyMarker = new google.maps.Marker({
  position: myLatLng,
  map: map,
  shadow: companyShadow,
  icon: companyImage,
  title: 'Singapore',
  zIndex: places[2][2]}); //<- change from place[3], get the z-index number
}

google.maps.event.addListener(companyMarker, 'click', function() {
  infowindow.open(map,companyMarker);
});

最后,如果您有其他用于 Bus Stop 和 MRT 标记的事件处理程序,那么您需要添加infowindow.close(map, someMarkerName)companyMarker侦听器以关闭相应的窗口。

于 2013-07-16T10:57:07.773 回答