javascript - Angular Google map 地理编码回调

标签 javascript angularjs google-maps callback google-geocoding-api

我在使用 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/

相关文章:

javascript - 如何在js中连接字符串和数字

ios - 在我们之前尚未在 GMSPlacePickerViewController 实例上设置委托(delegate)

javascript - Facebook API 请求对话框不适用于 Safari

javascript - HTML/Javascript 按钮点击计数器

c# - 我如何每 x 秒使用 javascript 调用一个 C# 函数?

javascript - 为什么不能在正则表达式中定义自己的自定义字符类?

html - 从下拉列表中进行选择后如何在下拉 block 中显示不同的值

javascript - Angular,创建工厂服务来获取本地 JSON 数据

android - 从 google map api v2 中删除默认对象

flutter - Android模拟器谷歌地图显示错误的位置