angularjs ng地图将自动聚焦于我的位置,似乎没有任何原因
我正在我的web应用程序中使用angular ng map。我构建了一个函数,它将获取所选用户的地址,在那里放置一个标记,然后放大它。但是,如果我不使用setTimeout来关注我的标记,地图将自动捕捉到我的地理位置,尽管我在应用程序中的任何位置都不使用地理位置。有人知道这是怎么发生的吗 控制器:angularjs ng地图将自动聚焦于我的位置,似乎没有任何原因,angularjs,google-maps,ng-map,Angularjs,Google Maps,Ng Map,我正在我的web应用程序中使用angular ng map。我构建了一个函数,它将获取所选用户的地址,在那里放置一个标记,然后放大它。但是,如果我不使用setTimeout来关注我的标记,地图将自动捕捉到我的地理位置,尽管我在应用程序中的任何位置都不使用地理位置。有人知道这是怎么发生的吗 控制器: NgMap.getMap({id:'map'}).then(function(map) { console.log('map', map); vm.map =
NgMap.getMap({id:'map'}).then(function(map) {
console.log('map', map);
vm.map = map;
//GEOCODER
var patientLat;
var patientLon;
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { "address": vm.myData.addressHome + ' ' + vm.myData.addressCity + ' ' + vm.myData.state + ' ' + 'USA'}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK && results.length > 0) {
var location = results[0].geometry.location;
patientLat = location.lat();
patientLon = location.lng();
console.log("the patient is located at " + patientLat + " " + patientLon);
vm.patientLocation = [
{
"patientLat" : patientLat
, "patientLon" : patientLon
}
]
setTimeout(function(){
patientPos(patientLat, patientLon);
}, 2000) <---------- here is the timeout i need
}
});
});
function patientPos(patientLat, patientLon) {
console.log(patientLat);
console.log(patientLon);
var bounds2 = new google.maps.LatLngBounds();
var latLng2 = new google.maps.LatLng(patientLat, patientLon);
bounds2.extend(latLng2);
vm.map.fitBounds(bounds2);
vm.map.setZoom(10);
}
HTML
您正试图在控制器完成之前设置一个位置,以便值在$digest周期内不会更新。基本上,您设置这些值太快了。要在使用$scope.$$postdigest方法后立即渲染它,可以使用$scope.$$postdigest方法 您可以执行以下操作,而不是超时:
$scope.$$postDigest(function(){
patientPos(patientLat, patientLon);
})
或者使用$timeout方法:
假设控制器中已经包含$scope或$timeout。结果是我有一个形状,其中有一个当前位置
当我移除它时,一切都解决了
$scope.$$postDigest(function(){
patientPos(patientLat, patientLon);
})
$timeout(function(){
patientPos(patientLat, patientLon);
},0,false);
//Run immediately after digest (0 ms after) and do not trigger another digest cycle
<shape id="circle" name="circle" centered="true"
stroke-color='#bc1212' stroke-opacity="0.8"stroke-weight="2"
center="current-position" radius={{vm.selectedRadius}} editable="false" ></shape>