0

ユーザーが地図上でマーカーをドラッグすると、場所を取得できません。ユーザーがマップをクリックしてマーカーを配置した場合、コードは正常に機能しますが、ドラッグされた場合は機能しません。なぜ機能しないのかよくわかりませんか?どんな助けでも感謝します。私のコードはこちら

function initialize() {
  var latLng = new google.maps.LatLng(53.34235267846957, -6.264953253906242);
  map = new google.maps.Map(document.getElementById('map-canvas'), {
  zoom: 7,
  center: latLng,
  mapTypeId: google.maps.MapTypeId.ROADMAP
  });
  marker = new google.maps.Marker({
  map: map,
  draggable: true
 });

  google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
updateMarkerPosition(marker.getPosition());
geocodePosition(event.latLng);
 });

google.maps.event.addListener(marker, 'position_changed', function() {
updateMarkerPosition(marker.getPosition());
geocodePosition(latLng);
 });

google.maps.event.addListener(marker, 'dragend', function() {
geocodePosition(marker.getPosition());
 });
} 
function placeMarker(location) {
 if ( marker ) {
  marker.setPosition(location);
map.setCenter(location);
} else {
 marker = new google.maps.Marker({
  position: location,
  map: center
 });
}
 }

var geocoder = new google.maps.Geocoder();
var geoTimer = setInterval(geocoder, 200);
var geoIndex = 0;
function geocodePosition(pos) {
if (geoIndex < 10) {
      geoIndex++;
  geocoder.geocode({ latLng: pos }, function(responses, status) {
  if (status == google.maps.GeocoderStatus.OK) {
    if (responses && responses.length > 0) {
      updateMarkerAddress(responses[0].formatted_address);
    } else {
      updateMarkerAddress('Cannot determine address at this location.');
    }
    }else{ alert('Cannot determine address at this location.'+status)}
  });
  }
else {
    clearInterval(geoTimer);
 }
 }

 function updateMarkerPosition(latLng) {
  document.getElementById('info').innerHTML = [
   latLng.lat(),
  latLng.lng()
  ].join(', ');
}

function updateMarkerAddress(str) {
document.getElementById('matchAdd').style.display="block";
  document.getElementById('address1').value = str;
  document.getElementById('address').innerHTML = str;

}
// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);
}
4

1 に答える 1