我正在尝试的是在地图上移动标记时动态获取纬度和经度。我找到了这个解决方案:
google.maps.event.addListener(marker, 'dragend', function(evt){
window.alert(evt.latLng.lat()+"---"+evt.latLng.lng());
});
但它没有给我返回lat和lng的新值
这是我给出的代码:
$scope.$on('mapInitialized', function(event, map) {
var uluru;
$scope.postGoogle = function(google){
$http.get("https://maps.googleapis.com/maps/api/geocode/json?address="+google.address+google.city+google.state+google.zip+google.country+"&key="+google.apikey).
then(function(response) {
$scope.latitute = response.data.results[0].geometry.location.lat;
$scope.longitude = response.data.results[0].geometry.location.lng;
uluru = {lat: response.data.results[0].geometry.location.lat, lng: response.data.results[0].geometry.location.lng};
map.setCenter({lat: response.data.results[0].geometry.location.lat, lng: response.data.results[0].geometry.location.lng});
var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">latitude and longitude</h1>'+
'<div id="bodyContent">'+
'<Span>'+map.getCenter()+'</span>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});
var marker = new google.maps.Marker({
position: uluru,
map: map,
draggable:true,
title: response.data.results[0].formatted_address
});
/* marker.addListener('click', function() {
infowindow.open(map, marker);
});*/
google.maps.event.addListener(marker, 'dragend', function(evt){
/* $scope.latitute = evt.latLng.lat().toFixed(3);
$scope.longitude = evt.latLng.lng().toFixed(3);*/
window.alert(evt.latLng.lat()+"---"+evt.latLng.lng());
//window.alert(this.getPosition().lat());
});
/*google.maps.event.addListener(marker, 'dragstart', function(evt){
//$scope.latitute = '<p>Currently dragging marker...</p>';
window.alert(evt.latLng.lat());
});
*/map.setCenter(marker.position);
marker.setMap(map);
}, function(response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
});
//window.alert(google.address+"------"+google.apikey);
};
var numTiles = 1 << map.getZoom();
var projection = new MercatorProjection();
$scope.chicago = map.getCenter();
});
答案 0 :(得分:0)
以下是我使用的一些JavaScript代码,在移动标记时动态更改lat和lng:
google.maps.event.addListener(marker, 'position_changed', function() {
lat = marker.getPosition().lat();
lng = marker.getPosition().lng();
$('#lat').val(lat);
$('#lng').val(lng);
});
和底部的2行为html输入分配值,以查看值是否已更改
答案 1 :(得分:0)
这就是我猜你正在寻找的东西:
google.maps.event.addListener(marker, 'dragend', function() {
geocoder.geocode({'latLng': marker.getPosition()}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
$('#address').val(results[0].formatted_address);
$('#latitude').val(marker.getPosition().lat());
$('#longitude').val(marker.getPosition().lng());
infowindow.setContent(results[0].formatted_address);
infowindow.open(map, marker);
}
}
});
});