2013-05-30 80 views
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); 
} 
+0

它不起作用?你是否在回覆中獲得了OVER_QUERY_LIMIT狀態? (看起來好像你正在檢查狀態) – geocodezip

+0

你是什麼意思標記狀態?它只是返回「無法確定此地點的地址」。 @geocodezip – CoffeeTime

+0

不是「標記狀態」,地理編碼操作的狀態。 [geocoder](https://developers.google.com/maps/documentation/javascript/reference#Geocoder)回調函數簽名是geocode(request:GeocoderRequest,callback:function(Array。,GeocoderStatus))' 。你沒有看GeocoderStatus。 – geocodezip

回答

0

「dragend」事件的觸發方式比人們預期的要多。我建議你扼殺這些要求。

  1. 添加一個標誌,以便一次只能向服務器發送一個請求。
  2. 如果自身不(它可能不會)工作,加入

連續請求之間的延遲或者可能添加一個檢查,看看用戶是否已經完成了拖,然後做請求到反向地理編碼器。

+0

再次感謝,我已更新我的代碼,嘗試解決我在SO上找到的使用setInterval但仍不起作用的解決方案。有什麼機會可以幫我解決問題嗎? @geocodezip – CoffeeTime