在Google地图中添加多个标记

时间:2016-08-03 11:31:47

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

我正在将谷歌地图添加到我的网站,我可以获取我当前的位置,但现在我想在地图上添加标记到不同的地方。我能做到这一点吗?我获取当前位置的代码是:

<div id="map"></div>
<script>

  function initMap() {
    var myLatlng = {lat: 40.59726025535419, lng: 80.02503488467874};

    var map = new google.maps.Map(document.getElementById('map'),{
      center:myLatlng,


    var infoWindow = new google.maps.InfoWindow({map: map});



  // Try HTML5 geolocation.
    if (navigator.geolocation) {
      navigator.geolocation.getCurrentPosition(function(position) {
        var pos = {
          lat: position.coords.latitude,
          lng: position.coords.longitude
        };

        infoWindow.setPosition(pos);
        infoWindow.setContent('Your Location.');
        map.setCenter(pos);
      }, function() {
        handleLocationError(true, infoWindow, map.getCenter());
      });
    } else {
      // Browser doesn't support Geolocation
      handleLocationError(false, infoWindow, map.getCenter());
    }
  }

  function handleLocationError(browserHasGeolocation, infoWindow, pos) {
         infoWindow.setPosition(pos);
    infoWindow.setContent(browserHasGeolocation ?
                          'Error: The Geolocation service failed.' :
                          'Error: Your browser doesn\'t support geolocation.');
  }



</script>
<script async defer
    src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBn7svjyYLYuP-EzakUjobLmpPi41l1_hw&callback=initMap">

</script>

3 个答案:

答案 0 :(得分:2)

创建新的google.maps.Map后(在您的情况下,变量 地图

    var marker = new google.maps.Marker({
      position: {lat: 40.59726025535419, lng: 80.02503488467874},
      map: map,
      title: 'Hello World!'
    });
    var marker2 = new google.maps.Marker({
      position: {lat: 40.59726025535419, lng: 80.02503488467874},
      map: map,
      title: 'Hello World!'
    });

来源:https://developers.google.com/maps/documentation/javascript/examples/marker-simple#try-it-yourself

演示 - 更新。 https://jsbin.com/bazoco/edit?html,console,output

尝试此功能伙伴 - 更新v3

  function appendMarker(map, latitude, longitude, text) {
    var pos = {lat: latitude, lng: longitude};
    var markerOption = {
      position: pos,
      map: map,
      title: text || 'Hello World!'
    };
    return new google.maps.Marker(markerOption);
  }
//Try HTML5 geolocation.
if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(function(position) {
    var pos = {
      lat: position.coords.latitude,
      lng: position.coords.longitude
    };

    infoWindow.setPosition(pos);
    infoWindow.setContent('Your Location.');
    map.setCenter(pos);

    //could you add this code below and test ?
    var markerOp = appendMarker(map, position.coords.latitude, position.coords.longitude, 'Hello');
    var markerOp2 = appendMarker(map, position.coords.latitude + 0.005, position.coords.longitude + 0.005, 'Hello');
    //could you add this code above and test ?

  }, function() {
    handleLocationError(true, infoWindow, map.getCenter());
  });

答案 1 :(得分:0)

您可以创建标记对象并将latlng对象传递给它

首先制作谷歌地图LatLng对象

var latlng = new google.maps.LatLng(latitude, longitude);

然后将此latlng对象用作标记对象

中的位置
var marker = new google.maps.Marker({position: latlng,map: map,})
marker.setMap(map);

答案 2 :(得分:0)

var myLatlng = {lat: 40.59726025535419, lng: 80.02503488467874};

var marker = new google.maps.Marker({
    position: myLatLng,
    map: map,
    title: 'Your LatLng!'
});

var marker2 = new google.maps.Marker({
    position: {lat: 40.60, lng: 80.1},
    map: map,
    title: 'Your second marker!'
});

var marker3 = new google.maps.Marker({
    position: {lat: 40.62, lng: 80.5},
    map: map,
    title: 'Your third marker!'
});

下次首先阅读文档:-)

工作小提琴:https://jsfiddle.net/kLndyr00/1/