質問

I am trying to get geocoding with googlemaps working. The problem I have is that the Geocoder in my code has not returned before centering the map again:

 GeoCoder.getLocations($scope.address).then(function(results) {
    var latLng = results[0].geometry.location;
    $scope.center = {
      lat: latLng.k,
      lng: latLng.A
    };
    $scope.zoom = 10;
  });

In my gmaps directive I set the center of the map to the new lat/lng that are returned from the GeoCoder (see above). What is a better solution to call the service and when it has returned to set the new location? Maybe an async call? This is the url plunkr:http://plnkr.co/edit/6Wg3UZNsJOHLQCmTJPCQ?p=preview

役に立ちましたか?

解決

What about just returning the center? Or chaining a promise for the center? You would resolve it when you are ready to center the map.

var centerPromise = GeoCoder.getLocations($scope.address).then(function(results) {
    var latLng = results[0].geometry.location;
    $scope.nextCenter = {
        lat: latLng.k,
        lng: latLng.A
    };
    return new $q.defer().promise;
 });
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top