我在使用 google maps geocoding api 时遇到了一些问题。
我正在使用 Angular Google map 并尝试使用回调函数对地址进行地理编码:
.controller('myCtrl', ['$scope', '$rootScope', 'uiGmapGoogleMapApi', function ($scope, $rootScope, uiGmapGoogleMapApi) {
// To be set by previous step
$rootScope.chosenTown = "Roma"
// geocode the given address
var geocodeAddress = function(address, callback) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
callback(results[0].geometry.location);
} else {
console.log("Geocode was not successful for the following reason: " + status);
}
});
};
// google maps is ready
uiGmapGoogleMapApi.then(function(maps) {
// geocode chosen town
geocodeAddress($rootScope.chosenTown, function(latLng){
console.log("1 " + latLng.lat())
console.log("2 " + latLng.lng())
$scope.map = { center: { latitude: latLng.lat(), longitude: latLng.lng() }, zoom: 12, bounds: {}};
});
});
}])
和 html 代码:
<div class="col-lg-5 finalStepMap">
<ui-gmap-google-map center='map.center' zoom='map.zoom' draggable="true" options="options" bounds="map.bounds">
<ui-gmap-markers models="markers" coords="'self'" options="'options'"></ui-gmap-markers>
</ui-gmap-google-map>
</div>
map 没有显示。
但是,如果我调用:
$scope.map = { center: { latitude: 10, longitude: 40}, zoom: 12, bounds: {}};
});
在回调函数之外固定经纬度,一切正常。
有什么线索吗?
感谢您宝贵的帮助:)
最佳答案
经过多次谷歌搜索,我终于找到了答案!
你必须使用:
$scope.$apply(function() {
$scope.map = ...
});
关于javascript - Angular Google map 地理编码回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32678393/