【发布时间】:2015-12-20 15:59:17
【问题描述】:
我正在尝试在地图上移动标记时动态获取纬度和经度。我找到了这个解决方案:
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();
});
【问题讨论】:
标签: angularjs google-maps