0
我有一個簡單的谷歌地圖拖動事件和地址查找。 在拖動事件上,我想更新緯度/經度輸入字段(工作),但也要更新地址(不工作),。Javascript - 谷歌地圖,簡單的反向地理編碼 - 總是爲零結果
我可能是太累了,而且很可能只是缺少一些微小的明顯啄這裏,但我似乎無法得到它的工作(總是返回零次的結果)..
見行動:http://jsfiddle.net/obmerk99/Ss4tp/
JS代碼:
// <![CDATA[
var map;
var markersArray = []; // to be used later to clear overlay array
var marker;
var address;
jQuery(document).ready(function() {
var lat = parseFloat(jQuery('#LatTxt').val());
var lon = parseFloat(jQuery('#LonTxt').val());
var myLatlng = new google.maps.LatLng(lat, lon);
var myOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(jQuery('[id^=k99_geolocation_]')[0], myOptions);
placeMarker(myLatlng);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
// Find Adress and set zoom when found
jQuery('#k99_geo_address').click(function() { codeAddress() });
function codeAddress() {
geocoder = new google.maps.Geocoder();
var address = document.getElementById("address").value;
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
map.setCenter(results[0].geometry.location);
map.setZoom(11); // how to do that according to the level ?? (country ,city, street, house ??)
if (marker) {
marker.setPosition(results[0].geometry.location);
}
} else {
alert("Geocode was not successful for the following reason: " + status);
}
}
);
}
//Function to extract longitude
function placeMarker(location) {
if (marker) {
marker.setPosition(location);
} else {
marker = new google.maps.Marker({
position: location,
draggable: true,
title: 'Drag me',
map: map
});
markersArray.push(marker); // to be used later to clear overlay array
}
}
// adding drag event
google.maps.event.addListener(marker, 'dragend', function(event) { updatelonlat(); });
// Removes the overlays from the map, but keeps them in the array
function clearOverlays() {
if (markersArray) {
for (var i = 0, length = markersArray.length; i < length; i++) {
markersArray[i].setMap(null);
}
}
}
//Jquery update HTML input boxes
function updatelonlat() {
jQuery('#LatTxt').val(marker.getPosition().lat());
jQuery('#LonTxt').val(marker.getPosition().lng());
geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
jQuery('#address').val(results[1].formatted_address);
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
// add event click
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
//document.getElementById("#LatTxt").value = event.latLng.lat();
//down lan lon values from http://tech.cibul.net/geocode-with-google-maps-api-v3/
updatelonlat();
});
});
// ]]>
權。我在那裏,並在一次迭代中刪除它。謝謝 !! –