Google地图Api标记不适用于多个位置

时间:2016-12-15 02:14:17

标签: javascript html google-maps google-maps-api-3

我有一个谷歌地图api,我在地图上放了一些多个位置但不幸的是我的位置标记没有出现在地图上,我不知道我的阵列或对象上是否存在问题我宣布,我需要的是在两个位置放置一些多个标记,这里是示例代码



#

.......................*#################
.........................................
.........................................
.........................................
.........................................
*.....................*..................
.*.......................................
..*......................................
.........................................
...*.................*...................
.........................................
....*....................................
.....*..............*....................
.........................................
......*............*.....................
.......*.................................
........*.........*......................
.........*.......*.......................
..........*..............................
...........*...**........................
............***..........................




1 个答案:

答案 0 :(得分:1)

您的标记创建代码中有拼写错误:

marker = new google.maps.Marker({
  position: new google.maps.LatLng(locations[i][1]),
  map: map

});

position google.maps.LatLng对象有两个参数,你只给它一个。应该是:

  position: new google.maps.LatLng(locations[i][0],locations[i][1]),

代码段

html,
body,
#hplus-map {
  height: 100%;
  width: 100%;
}
<div id="hplus-map"></div>
<script>
  var map;

  function initMap() {
    var manilaMain = {
      info: "<strong>Manila Main</strong><br>5224 N Broadway St<br> Chicago, IL 60640<br>Get Directions</a>",
      lat: 14.5995124,
      lng: 120.9842195
    };

    var manilaHospital = {
      info: "<strong>Manila Hospital</strong><br>5224 N Broadway St<br> Chicago, IL 60640<br>",
      lat: 14.609189,
      lng: 120.966466
    };

    var locations = [
      [manilaMain.lat, manilaMain.lng, manilaMain.info, 0],
      [manilaHospital.lat, manilaHospital.lng, manilaHospital.info, 1],
    ];

    var newCent = new google.maps.LatLng(14.5995124, 120.9842195);

    map = new google.maps.Map(document.getElementById('hplus-map'), {
      center: newCent,
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      draggable: false,
      scrollwheel: false
    });

    var infowindow = new google.maps.InfoWindow({
      // content: contentString,
      // maxWidth: 600
    });

    var marker, i;

    for (i = 0; i < locations.length; i++) {
      marker = new google.maps.Marker({
        position: new google.maps.LatLng(locations[i][0], locations[i][1]),
        map: map
      });

      google.maps.event.addListener(marker, 'click', (function(marker, i) {
        return function() {
          infowindow.setContent(locations[i][2]);
          infowindow.open(map, marker);
        }
      })(marker, i));
    }
  }
</script>
<script src="https://maps.googleapis.com/maps/api/js?callback=initMap" async defer></script>