简体   繁体   中英

Add multiple markers in google maps

I am adding google maps to my website and I am able to get my current location but now I want to add markers to differnet places on map.How I can do that?My code to get current location is as:

<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>

after you create new google.maps.Map (in your case, variable 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!'
    });

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

Demo - updated. https://jsbin.com/bazoco/edit?html,console,output

Try this function buddy - updates 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());
  });

you can create marker object and pass latlng object to it

first make google map LatLng Object

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

then use this latlng object as position in marker object

var marker = new google.maps.Marker({position: latlng,map: map,})
marker.setMap(map);
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!'
});

Next time read the documentation first :-)

Working fiddle: https://jsfiddle.net/kLndyr00/1/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM