Aessandro
Aessandro

Reputation: 5771

Geolocation not working correctly

The following code returns in the console:

Cannot call method 'setCenter' of undefined

this.locateUser = function() {
    if(navigator && navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {
                var latitude = position.coords.latitude,
                    longitude = position.coords.longitude;
                var coords = google.maps.LatLng(latitude, longitude);
                self.map.setCenter(coords);
            });
    }

It looks like there is sth wrong with the coords

Upvotes: 0

Views: 144

Answers (2)

Jörn Zaefferer
Jörn Zaefferer

Reputation: 5705

Your code block is incomplete, so I have to guess a lot here: You're accessing self.map.setCenter. The error message says "Cannot call method 'setCenter' of undefined", so while self is defined, self.map isn't. Your code doesn't include a definition of self or self.map, so the issue is likely elsewhere.

Additionally, you shouldn't declare and use self as a variable in your code, since self is never undefined (it refers to window if not declared locally). Use var that = this; instead.

Upvotes: 1

Norbert Pisz
Norbert Pisz

Reputation: 3440

Try like this:

 map.setCenter(new GLatLng(latitude, -longitude), 13);

Upvotes: 0

Related Questions