dylanfprice / angular-gm

AngularJS Google Maps Directives
MIT License
197 stars 47 forks source link

unable to fit bounds Bounds #37

Closed rahulchavan30 closed 10 years ago

rahulchavan30 commented 10 years ago

Hi i am having trouble in setting the bounds, to cut short things heres the code snippet

//HTML Markup

//Controller $scope.bounds = new google.maps.LatLngBounds(); var myLatLng=new google.maps.LatLng(elements.practice.latitude,elements.practice.longitude)

$scope.bounds.extend(myLatLng);

//and i need to set the center from this $scope.bounds.getCenter();

$scope.center=$scope.bounds.getCenter();

for some reason its taking the default values.

i need to do something like this map.fitBounds(bounds); map.setCenter(bounds.getCenter()); how do i acheive this

Please help !!

dylanfprice commented 10 years ago

Hey sorry just noticed this. If you provide a jsfiddle or plunker that helps a lot.

If the code you listed for your controller is being run when the controller is instantiated then those values will get overwritten when the gm-map directive is instantiated. Hmm maybe gm-map needs to fire an event when it is initialized. In the meantime you could do something like:

var mapInitialized = $q.defer();
$scope.$watch('bounds', function(bounds) {
  if (bounds) {
    mapInitialized.resolve();
  }
});

mapInitialized.promise.then(function() {
  var myLatLng=new google.maps.LatLng(elements.practice.latitude,elements.practice.longitude)

  $scope.bounds.extend(myLatLng);

  //and i need to set the center from this
  $scope.bounds.getCenter();

  $scope.center = $scope.bounds.getCenter();
});

It's hacky but it will work.