Google maps v3 draggable marker

前端 未结 4 511
忘了有多久
忘了有多久 2021-01-29 23:37

I\'m new in google maps, and I\'m trying to learn it.

marker = new google.maps.Marker(
{
     map:map,
     draggable:tru         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 00:01

    Set a Position on Map using lat/lang and make the marker draggable

    • Using lat/lang initially sets a marker at the given point on the map

    • The address variable is used for title purpose. It can be ignored.

    • draggable:true makes the marker draggable.

    • Use event listener google.maps.event.addListener(marker, 'dragend', function(marker) To listen for changes in the marker position

      function showMap(lat,lang,address) {
      var myLatLng = {lat: lat, lng: lang};
      
          var map = new google.maps.Map(document.getElementById('map_canvas'), {
            zoom: 17,
            center: myLatLng
          });
      
          var marker = new google.maps.Marker({
            position: myLatLng,
            map: map,
            title: address,
            draggable:true,
          });
      
          google.maps.event.addListener(marker, 'dragend', function(marker){
              var latLng = marker.latLng; 
              currentLatitude = latLng.lat();
              currentLongitude = latLng.lng();
              jQ("#latitude").val(currentLatitude);
              jQ("#longitude").val(currentLongitude);
           }); 
      }
      

提交回复
热议问题