Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Google maps geocoding callback

I have some issues with google maps geocoding api.

I am using Angular Google Maps and trying to geocode an address with a callback function :

.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: {}};
        });

    });
}])

And the html code :

<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>

The map is not displayed.

However, if I call :

$scope.map = { center: { latitude: 10, longitude: 40}, zoom: 12, bounds: {}};
        });

With fixed lat and lng outside the callback function, everything works fine.

Any clue?

Thanks for your precious help :)

like image 434
Maxime VAST Avatar asked Oct 19 '22 02:10

Maxime VAST


1 Answers

after more googling, i finally found the answer !

You have to use :

$scope.$apply(function() { 
    $scope.map = ...
});
like image 91
Maxime VAST Avatar answered Oct 29 '22 19:10

Maxime VAST