Skip to content
Advertisement

Google Maps Marker doesn’t remain draggable after setting a Custom Place using Autocomplete

So I’m trying to make a Custom Google Map where the user can either use the input (with autocomplete suggestions for places) to select the position in the map or drag the marker to select the position.

When I create a map without the autocomplete feature, the marker on the map remains draggable. But once I add the autocomplete listener, on using the autocomplete feature once, the marker no longer remains draggable.

Here’s the JS code that I’m using:

defaultLatLong = {lat: xxxx lng: xxxx};

var map = new google.maps.Map(document.getElementById('map'), {
  center: defaultLatLong,
  zoom: 13,
  mapTypeId: 'roadmap'
});

var input = document.getElementById('pac-input');

var autocomplete = new google.maps.places.Autocomplete(input);

autocomplete.bindTo('bounds',map);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

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

google.maps.event.addListener(marker, 'dragend', function(marker){
    var latLng = marker.latLng;
    currentLatitude = latLng.lat();
    currentLongitude = latLng.lng();
    var latlng = {lat: currentLatitude, lng: currentLongitude};
    var geocoder = new google.maps.Geocoder;
    geocoder.geocode({'location': latlng}, function(results, status) {
          if (status === 'OK') {
            if (results[0]) {
              input.value = results[0].formatted_address;
            } else {
              window.alert('No results found');
            }
          } else {
            window.alert('Geocoder failed due to: ' + status);
          }
        });
});

autocomplete.addListener('place_changed', function() {
    var place = autocomplete.getPlace();
    if (!place.geometry) {
      return;
    }
    if (place.geometry.viewport) {
      map.fitBounds(place.geometry.viewport);
    } else {
      map.setCenter(place.geometry.location);
    }

    marker.setPlace({
      placeId: place.place_id,
      location: place.geometry.location
    });

    currentLatitude = place.geometry.location.lat();
    currentLongitude = place.geometry.location.lng();

  });

Any solutions to this issue?

Advertisement

Answer

It looks like if you use the .setPlace() method of the marker it isn’t draggable.

Use the .setPosition() method instead.

replace:

marker.setPlace({
  placeId: place.place_id,
  location: place.geometry.location
});

with:

marker.setPosition(place.geometry.location);

proof of concept fiddle

code snippet:

defaultLatLong = {
  lat: 40.7127753,
  lng: -74.0059728
};

var map = new google.maps.Map(document.getElementById('map'), {
  center: defaultLatLong,
  zoom: 13,
  mapTypeId: 'roadmap'
});

var input = document.getElementById('pac-input');

var autocomplete = new google.maps.places.Autocomplete(input);

autocomplete.bindTo('bounds', map);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);

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

google.maps.event.addListener(marker, 'dragend', function(marker) {
  var latLng = marker.latLng;
  currentLatitude = latLng.lat();
  currentLongitude = latLng.lng();
  var latlng = {
    lat: currentLatitude,
    lng: currentLongitude
  };
  var geocoder = new google.maps.Geocoder;
  geocoder.geocode({
    'location': latlng
  }, function(results, status) {
    if (status === 'OK') {
      if (results[0]) {
        input.value = results[0].formatted_address;
      } else {
        window.alert('No results found');
      }
    } else {
      window.alert('Geocoder failed due to: ' + status);
    }
  });
});

autocomplete.addListener('place_changed', function() {
  var place = autocomplete.getPlace();
  if (!place.geometry) {
    return;
  }
  if (place.geometry.viewport) {
    map.fitBounds(place.geometry.viewport);
  } else {
    map.setCenter(place.geometry.location);
  }

  marker.setPosition(place.geometry.location);

  currentLatitude = place.geometry.location.lat();
  currentLongitude = place.geometry.location.lng();

});
html,
body,
#map {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<input id="pac-input" />
<div id="map"></div>
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement