Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

How to drag marker and click to set at the same time in Google Maps API v3?

I'm new in Google Maps API v3, I'm trying to add click event listener to re-set the marker, but I want have the posibility to drag the marker too.

Actually I only can drag the marker, this is my code:

var lat = null;
var lng = null;
var map = null;
var geocoder = null;
var marker = null;

jQuery(document).ready(function(){
     lat = jQuery('#lat').val();
     lng = jQuery('#long').val();
     jQuery('#pasar').click(function(){
        codeAddress();
        return false;
     });
    initialize();
});

    function initialize() {

      geocoder = new google.maps.Geocoder();

       if(lat !='' && lng != '')
      {
         var latLng = new google.maps.LatLng(lat,lng);
      }
      else
      {
         var latLng = new google.maps.LatLng(11.027113, -63.862023);
      }
       var myOptions = {
          center: latLng,
          zoom: 15,
          mapTypeId: google.maps.MapTypeId.ROADMAP 
        };
        map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

        marker = new google.maps.Marker({
            map: map,
            position: latLng,
            draggable: true 
        });

        updatePosition(latLng);


    }

    function codeAddress() {

        var address = document.getElementById("direccion").value;
        geocoder.geocode( { 'address': address}, function(results, status) {

        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            marker.setPosition(results[0].geometry.location);
            updatePosition(results[0].geometry.location);

            google.maps.event.addListener(marker, 'dragend', function(){
                updatePosition(marker.getPosition());
            });
      } else {
          alert("No podemos encontrar la direccion, error: " + status);
      }
    });
  }

  function updatePosition(latLng)
  {

       jQuery('#lat').val(latLng.lat());
       jQuery('#long').val(latLng.lng());

  }

I tried to add the following code:

myListener = google.maps.event.addListener(map, 'click', function(event) {
        if(marker){marker.setMap(null)}
        placeMarker(event.latLng);
        google.maps.event.removeListener(myListener);
    });

But it does not work. I want to place the marker with both options, dragging and click. How I can achieve that in my code? Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is no placeMarker function in your code, and not sure why you only want the marker placed on the first click on the map (you are removing the listener after that). This is what I think you want:

var lat = null;
var lng = null;
var map = null;
var geocoder = null;
var marker = null;
var myListener = null;

jQuery(document).ready(function() {
  lat = jQuery('#lat').val();
  lng = jQuery('#long').val();
  jQuery('#pasar').click(function() {
    codeAddress();
    return false;
  });
  initialize();
});

function initialize() {

  geocoder = new google.maps.Geocoder();

  if (lat != '' && lng != '') {
    var latLng = new google.maps.LatLng(lat, lng);
  } else {
    var latLng = new google.maps.LatLng(11.027113, -63.862023);
  }
  var myOptions = {
    center: latLng,
    zoom: 15,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

  marker = new google.maps.Marker({
    map: map,
    position: latLng,
    draggable: true
  });
  google.maps.event.addListener(marker, 'dragend', function() {
    updatePosition(marker.getPosition());
  });
  updatePosition(latLng);
  google.maps.event.addListener(map, 'click', function(event) {
    if (marker) {
      marker.setPosition(event.latLng)
    } else {
      marker = new google.maps.Marker({
        map: map,
        position: event.latLng,
        draggable: true
      });
    }
    updatePosition(event.latLng);
  });

}

function codeAddress() {

  var address = document.getElementById("direccion").value;
  geocoder.geocode({
    'address': address
  }, function(results, status) {

    if (status == google.maps.GeocoderStatus.OK) {
      map.setCenter(results[0].geometry.location);
      marker.setPosition(results[0].geometry.location);
      updatePosition(results[0].geometry.location);

      google.maps.event.addListener(marker, 'dragend', function() {
        updatePosition(marker.getPosition());
      });
    } else {
      alert("No podemos encontrar la direccion, error: " + status);
    }
  });
}

function updatePosition(latLng) {

  jQuery('#lat').val(latLng.lat());
  jQuery('#long').val(latLng.lng());

}
html,
body,
#map_canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js"></script>
<input id="lat" value="42" />
<input id="long" value="-85" />
<input id="pasar" type="button" value="geocode" />
<input id="direccion" value="New York, NY" />
<div id="map_canvas" style="border: 2px solid #3872ac;"></div>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...