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 :)
after more googling, i finally found the answer !
You have to use :
$scope.$apply(function() {
$scope.map = ...
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With