Google map V3 user adding markers

前端 未结 2 1958
野的像风
野的像风 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:36

    Or, use MarkerManager for maps API v3 and rid yourself of any kind of other little mistake you could make implementing this (like moving markers etc)

    0 讨论(0)
  • 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);
           }
        });
      }
    0 讨论(0)
提交回复
热议问题