如何在每个纬度/长点上添加带有标记的2个谷歌地图?

时间:2017-11-03 18:31:32

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

I'm pretty new to Javascript and I am not able to add a marker to these google maps API's. I was able to add one map with a marker using W3schools tutorial, but when I try to add multiple maps with one marker on each one, this is where I am running into problems. I have searched for an answer on here already, and I was not able to find a working solution. In the picture I have posted, I have not yet added the "var marker" constructor because I am not sure where to put it?  Can someone kindly point me in the right direction, and tell me where I am going wrong?

有问题的Javascript:

var md     = window.markdownit();
var result = md.parse( document.querySelector('#markdown-content').textContent, {} );

我认为我必须添加,但不确定在哪里,甚至如何:

function myMap() {
  var mapOptions1 = {
    center: new google.maps.LatLng(42.9814362, -81.2267205),
    zoom:15,
    mapTypeId: google.maps.MapTypeId.ROADMAP

};
  var mapOptions2 = {
    center: new google.maps.LatLng(42.9421287, -81.2284421),
    zoom:15,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map1 = new google.maps.Map(document.getElementById("googleMap1"),mapOptions1);
  var map2 = new google.maps.Map(document.getElementById("googleMap2"),mapOptions2);
}

2 个答案:

答案 0 :(得分:1)

您应为每张地图上的每个标记分配一个有效的中心

  function myMap() {
    var mapOptions1 = {
      center: new google.maps.LatLng(42.9814362, -81.2267205),
      zoom:15,
      mapTypeId: google.maps.MapTypeId.ROADMAP

  };
    var mapOptions2 = {
      center: new google.maps.LatLng(42.9421287, -81.2284421),
      zoom:15,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map1 = new google.maps.Map(document.getElementById("googleMap1"),mapOptions1);
    var map2 = new google.maps.Map(document.getElementById("googleMap2"),mapOptions2);

    var marker1 = new google.maps.Marker({
              position: new google.maps.LatLng(42.9814362, -81.2267205)
    });

    marker1.setMap(map1); 

    var marker2 = new google.maps.Marker({
              position: new google.maps.LatLng(42.9421287, -81.2284421)
    });

    marker2.setMap(map2); 

  }

答案 1 :(得分:0)

为了将来参考,我想出来了。

  function myMap() {
  var mapOptions1 = {
    center: new google.maps.LatLng(42.9814362, -81.2267205),
    zoom:15,
    mapTypeId: google.maps.MapTypeId.ROADMAP

};
  var mapOptions2 = {
    center: new google.maps.LatLng(42.9421287, -81.2284421),
    zoom:15,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map1 = new google.maps.Map(document.getElementById("googleMap1"),mapOptions1);
  var latlong = {lat: , lng: }; // Insert your map marker lat/long here
  var marker = new google.maps.Marker({
    position: latlong,
    map: map1,
    title: 'Hello World'
  });
  var map2 = new google.maps.Map(document.getElementById("googleMap2"),mapOptions2);
  var latlong = {lat: , lng: }; // Insert your map marker lat/long here
  var marker = new google.maps.Marker({
    position: latlong,
    map: map2,
    title: 'Hello World'
  });
}