Google map V3 user adding markers

前端 未结 2 1960
野的像风
野的像风 2021-02-04 19:01

I need some code that allows users to add their own markers to my map. Does anybody have an example?

Thanks!

var initialLocation;
var siberia = new goog         


        
2条回答
  •  滥情空心
    2021-02-04 19:58

    It's not a hard job:

    • Set a click event on the map

      google.maps.event.addListener(map, 'click', function(event) {
        placeMarker(event.latLng);
      });
    • Place the marker and make an AJAX call to the server to save the location in the database:

      function placeMarker(location) {
        var marker = new google.maps.Marker({
            position: location, 
            map: map
        });
      
      
        map.setCenter(location);
      
      
        $.ajax({
           url: 'myPHP',
           data: location,
           succes: function(){
             alert('marker was added');
           },
           error: function(){
             alert('marker wasn't added');
             marker.setMap(null);
           }
        });
      }

提交回复
热议问题