Javascript 在谷歌地图中移动标记和更新位置
我使用的是谷歌地图,你可以在文本字段中键入一个地点,然后你会在谷歌地图上看到一个带有坐标的标记。您还可以移动制造者,然后更新信息框中的坐标。但是如何更新文本字段中的地名呢?多谢各位 这是jquery脚本:Javascript 在谷歌地图中移动标记和更新位置,javascript,jquery,google-maps,google-maps-api-3,Javascript,Jquery,Google Maps,Google Maps Api 3,我使用的是谷歌地图,你可以在文本字段中键入一个地点,然后你会在谷歌地图上看到一个带有坐标的标记。您还可以移动制造者,然后更新信息框中的坐标。但是如何更新文本字段中的地名呢?多谢各位 这是jquery脚本: var map; function initMap() { var geocoder = new google.maps.Geocoder(); var startaddress = $('#form_inp19').val(); geocoder.geoco
var map;
function initMap() {
var geocoder = new google.maps.Geocoder();
var startaddress = $('#form_inp19').val();
geocoder.geocode({ 'address': startaddress }, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
startLocationMap = results[0].geometry.location;
map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: startLocationMap
});
geocoder = new google.maps.Geocoder();
document.getElementById('submit').addEventListener('click', function () {
geocodeAddress(geocoder, map);
});
} else {
alert('Place doesnt exist on the map: ' + status);
}
if (typeof google.maps == 'undefined') {
/* custom functions to alert the user to the error */
return 0;
}
});
}//end function initMap
$(document).ready(function () {
if (typeof google.map == 'undefined') {
return 0;
}
map = new google.maps.Map(document.getElementById('map'),
mapOptions);
});
function toggleBounce() {
if (marker.getAnimation() !== null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
}
var marker;
var infowindow;
function geocodeAddress(geocoder, resultsMap) {
if (typeof infowindow != 'undefined') {
infowindow.close();
}
if (typeof marker != 'undefined') {
marker.setMap(null);
}
var address = document.getElementById('address').value;
geocoder.geocode({ 'address': address }, function (results, status) {
if (status === google.maps.GeocoderStatus.OK) {
resultsMap.setCenter(results[0].geometry.location);
marker = new google.maps.Marker({
map: resultsMap,
draggable: true,
animation: google.maps.Animation.DROP,
position: results[0].geometry.location,
title: "Drag me!"
});
} else {
alert('Place doesnt exist on the map: ' + status);
}
infowindow = new google.maps.InfoWindow({
content: '<p>Marker Location:'
+ 'lat: ' + marker.getPosition().lat().toFixed(6)
+ ', '
+ 'lng: ' + marker.getPosition().lng().toFixed(6)
+ '</p>'
});
$(".geolocation_lat ").val(marker.getPosition().lat().toFixed(6)) //= marker.getPosition().lat().toFixed(6);
$(".geolocation_long ").val(marker.getPosition().lng().toFixed(6))
google.maps.event.addListener(marker, 'dragend', function (event) {
if (typeof infowindow != 'undefined') {
infowindow.close();
}
infowindow = new google.maps.InfoWindow({
content: '<p>Marker Location:'
+ 'lat: ' + event.latLng.lat().toFixed(6)
+ ', '
+ 'lng: ' + event.latLng.lng().toFixed(6)
+ '</p>'
});
$(".geolocation_lat ").val(event.latLng.lat().toFixed(6)); //= marker.getPosition().lat().toFixed(6);
$(".geolocation_long ").val(event.latLng.lng().toFixed(6));
infowindow.open(map, marker);
});
infowindow.open(map, marker);
google.maps.event.addListener(marker, 'click', function (event) {
if (typeof infowindow != 'undefined') {
infowindow.open(map, marker);
}
});
});
}
我看到地址:responses[0]。格式化地址“Belle van Zuylenlaan 23-242642 Pijnacker,Nederland”
但是如何在文本字段中只获取地名呢
谢谢你的dragend listener
google.maps.event.addListener(marker, 'dragend', function() {
geocodePosition(marker.getPosition());
});
及
oh put geocoder=new google.maps.geocoder();在geocoder.geocode({u missed.value,即document.getElementById('address').value=”“是的,谢谢。它现在可以工作($(“#address”).val(responses[3]。格式化的#address);)。但是如何只获取地名,而不获取地址和邮政编码呢?谢谢
document.getElementById('address') = responses[0].formatted_address;
google.maps.event.addListener(marker, 'dragend', function() {
geocodePosition(marker.getPosition());
});
function geocodePosition(pos) {
geocoder = new google.maps.Geocoder();
geocoder.geocode({
latLng: pos
}, function(responses) {
if (responses && responses.length > 0) {
document.getElementById('yourTextBoxId').value=responses[0].formatted_address;
} else {
document.getElementById('yourTextBoxId').value='Cannot determine address at this location.';
}
});
}