【问题标题】:Google maps API - multiple locations on multiple maps in one pageGoogle 地图 API - 一页中的多个地图上的多个位置
【发布时间】:2017-04-19 06:28:03
【问题描述】:

我们必须使用谷歌地图来获取地图位置。对于一页中具有多个位置的不同地图。有可能吗?

实际上我们想要多张地图,但在 google api 代码中,他们为我们提供了一张地图用于一页,我们如何更改和制作多张地图。

谷歌 API 链接:- https://developers.google.com/maps/documentation/javascript/examples/icon-complex

代码:-

// google map section
function initMap() {
 var map = new google.maps.Map(document.getElementById('map'), {
 zoom: 15,
 center: {lat: 35.9099, lng: 14.4498}
});
setMarkers(map);
}

// Data for the markers consisting of a name, a LatLng and a zIndex for the
// order in which these markers should display on top of each other.
var beaches = [
  ['Property 3', 35.9065598, 14.4502898, 3],
  ['Bondi Beach', 35.9099206, 14.4498855, 2],
  ['Coogee Beach', 35.9159643, 14.4484053, 1]
];

请查看代码链接:-Jsfiddle link

第一个是显示多个位置,第二个地图不显示任何位置。

【问题讨论】:

  • 使用尽可能多的元素并创建地图,它的不是地图对应于容器document.getElementById('map')添加更多容器添加更多地图
  • 我试试这个,但不能为一页工作。
  • 你能分享那段代码吗?也许将它嵌入到 jsfiddle 中。
  • 向我们展示您尝试的代码,每个映射一个新实例?
  • @BharatNegi 在下面查看我的解决方案。

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


【解决方案1】:

您需要将两个变量传递给您的setMarkers 函数,因为您正在创建两个映射,您需要初始化两个变量mapmap1,然后调用setMarkers(map, map1);

setMarkers 函数中,您现在需要设置两个标记,第二个标记需要将map 选项设置为map1

var marker1 = new google.maps.Marker({
      position: {lat: beach[1], lng: beach[2]},
      map: map1,
      icon: image,
      shape: shape,
      title: beach[0],
      zIndex: beach[3]
    });

更新答案:

#map, #mapTwo{width: 300px; height: 500px; }
#map {float:left;}
#mapTwo {float:right;}
<div id="map"></div>

<div id="mapTwo"></div>

<script>
// google map section
function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 15,
    center: {lat: 35.9099, lng: 14.4498}
  });
  
  
  var map1 = new google.maps.Map(document.getElementById('mapTwo'), {
    zoom: 15,
    center: {lat: 35.9099, lng: 14.4498}
  });

  // modified to pass two variables, both map instances. 
  setMarkers(map, map1);
}

// Data for the markers consisting of a name, a LatLng and a zIndex for the
// order in which these markers should display on top of each other.
var beaches = [
  ['Property 3', 35.9065598, 14.4502898, 3],
  ['Bondi Beach', 35.9099206, 14.4498855, 2],
  ['Coogee Beach', 35.9159643, 14.4484053, 1]
];


// SECOND MAP LOCATIONS
var beaches2 = [
  ['Property - MAP 2', 35.9065598, 14.4512898, 3],
  ['Bondi Beach - MAP 2', 35.9099206, 14.4598855, 2],
  ['Coogee Beach - MAP 2', 35.9159643, 14.5484053, 1]
];

function setMarkers(map, map1) {
  // Adds markers to the map.

  // Marker sizes are expressed as a Size of X,Y where the origin of the image
  // (0,0) is located in the top left of the image.

  // Origins, anchor positions and coordinates of the marker increase in the X
  // direction to the right and in the Y direction down.
  var image = {
    url: 'https://developers.google.com/maps/documentation/javascript/examples/full/images/beachflag.png',
    // This marker is 20 pixels wide by 32 pixels high.
    size: new google.maps.Size(20, 32),
    // The origin for this image is (0, 0).
    origin: new google.maps.Point(0, 0),
    // The anchor for this image is the base of the flagpole at (0, 32).
    anchor: new google.maps.Point(0, 32)
  };
  // Shapes define the clickable region of the icon. The type defines an HTML
  // <area> element 'poly' which traces out a polygon as a series of X,Y points.
  // The final coordinate closes the poly by connecting to the first coordinate.
  var shape = {
    coords: [1, 1, 1, 20, 18, 20, 18, 1],
    type: 'poly'
  };
  for (var i = 0; i < beaches.length; i++) {
    var beach = beaches[i];
    var marker = new google.maps.Marker({
      position: {lat: beach[1], lng: beach[2]},
      map: map,
      icon: image,
      shape: shape,
      title: beach[0],
      zIndex: beach[3]
    });
  } 
  
  // second map markers
  for (var i = 0; i < beaches2.length; i++) { 
    var beach = beaches2[i];
    // markers for second map
    var marker1 = new google.maps.Marker({
      position: {lat: beach[1], lng: beach[2]},
      map: map1,  // make sure to point to your second map
      icon: image,
      shape: shape,
      title: beach[0],
      zIndex: beach[3]
    });
  }
} 
</script>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAWAoxTVd_zUi11jJe9TEvu6MUoOfzTerE&callback=initMap"></script>

【讨论】:

  • 感谢您的回复,但我们希望两张地图上的不同位置不一样。
  • @BharatNegi 创建另一个位置数组并将其传递给marker1。目前,您的代码甚至没有在第二张地图上显示标记。给我第二张地图的位置,我会更新我的代码。
  • @BharatNegi 运行 jsFiddle,现在您将在两张地图上看到不同的位置。我输入了随机位置。您需要缩小第二张地图才能看到所有三个位置。
猜你喜欢
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 2012-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多