angular-google-maps icon indicating copy to clipboard operation
angular-google-maps copied to clipboard

center map position

Open jlacroix12 opened this issue 11 years ago • 11 comments

I'm attempting to center the map on the current position and Im getting

undefined is not a function related to setCenter

the code is below

$scope.map = { center: { latitude: 45, longitude: -73 }, zoom: 8 }; $scope.options = {scrollwheel: false}; $scope.showWeather = true; $scope.showTraffic = true;

    $scope.positions = [{
      lat: 43.07493,
      lng: -89.381388
    }];

    $scope.$on('mapInitialized', function(event, map) {
      $scope.map = map;
    });

    $scope.centerOnMe = function() {
      if(!$scope.map) {
        return;
      }

      $scope.loading = $ionicLoading.show({
        content: 'Getting current location.',
        showBackdrop: false
      });

      navigator.geolocation.getCurrentPosition(function(pos) {
        $scope.map.setCenter(new google.maps.LatLng(pos.coords.latitude, pos.coords.longitude));
        $scope.loading.hide();
      }, function(error) {
        alert('Unable to get location: ' + error.message);
      });

Any thoughts?

jlacroix12 avatar Jan 15 '15 23:01 jlacroix12

You can just set the latitude and longitude properties of the center property of your map

$scope.map.center = {
   latitude:pos.coords.latitude,
   longitude: pos.coords.longitude
};

aplus01 avatar Jan 16 '15 02:01 aplus01

The map center object has been updated successfully but the map isn't updated to the new location

Any help?

iMomen avatar Aug 04 '15 08:08 iMomen

Same as iMomen here.

arathael avatar Nov 19 '15 00:11 arathael

plnkr?

nmccready avatar Nov 19 '15 00:11 nmccready

I have the same problem. Any solution on this? Can you run setCenter at all or do I have to instantiate a google map instance in my controller the old fashioned way var map = new google.map etc....

Loubot avatar Mar 25 '16 14:03 Loubot

+1. updating the center object does not recenter the map. Zoom works for me this way though.

mayson14 avatar Jul 14 '16 22:07 mayson14

Please use githubs thumbs up, no need for +1's anymore

nmccready avatar Sep 12 '16 13:09 nmccready

How are you guys updating the center object? This is why we need an example via plnkr!

Also the code for how the centering is updated is here . Walk through the debugger there to get an idea on what is causing to bail or not.

nmccready avatar Sep 12 '16 13:09 nmccready

Hey @nmccready Please have a look at the following plunkr http://plnkr.co/edit/M6wFwY8UW9ZCo9ABPzPx?p=preview

In the above try to move the map somewhere and click Recenter. The map is expected to center around the placed marker but it doesn't. But if you change the angular-google-maps version to 2.3.2 it works.

Further investigation

v2.1.5 with lodash 3.40 - works v2.1.6 - v2.3.1 - Not working v2.3.2 - working v2.3.3 & v2.3.4 - Not working

Hope this is helpful!

In all cases zoom worked as expected

youfacepalm avatar Sep 13 '16 15:09 youfacepalm

@youfacepalm thanks!

nmccready avatar Sep 13 '16 16:09 nmccready

For anyone else landing here with the same problem as @youfacepalm, you have to replace the center object itself, it is not sufficient to change it's latitude/longitude properties.

phaed-one avatar Jun 10 '17 07:06 phaed-one