0

I'm trying to center the users current location on a map.

But I have a hard time figuring out why getCurrentPosition is just skipped. When i debug it just skip the line.

What am i doing wrong?

initMap: function() {
    var latLng;

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            function(position) {
                latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
            }, function() {
                latLng = new google.maps.LatLng(37.4, -122.1)
            }, {
                timeout: 10000
            }
        );
    };

    var mapOptions = {
        center: latLng,
        zoom: 12,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    this.oMap = new google.maps.Map(this.getView().byId("map_canvas").getDomRef(), mapOptions);
    this.geocoder = new google.maps.Geocoder();
    this.directionsService = new google.maps.DirectionsService();
    this.directionsDisplay = new google.maps.DirectionsRenderer({
        draggable: true,
        map: this.oMap,
        suppressMarkers: true
    });

    this.createCurrentLocationMarker();
    this.loadDataFromModel();
}
HenrikD
  • 3
  • 2
  • 1
    Maybe `navigator.geolocation` is undefined on your browser? – Frédéric Hamidi Nov 20 '15 at 14:53
  • Hm, ' if (navigator.geolocation)' is called fine and I get to ' navigator.geolocation.getCurrentPosition'. If 'navigator' is undefined then i should not get to ' navigator.geolocation.getCurrentPosition'. – HenrikD Nov 20 '15 at 14:55
  • If `navigator.geolocation` is null, it would skip the next code. What is `navigator.geolocation`? – Karl Gjertsen Nov 20 '15 at 14:56
  • @Henrik, if you get to `navigator.geolocation.getCurrentPosition()`, why do you say *it just skip the line*? – Frédéric Hamidi Nov 20 '15 at 14:56
  • 4
    Or is the fact that `getCurrentPosition()` may return asynchronously, so when `latLng` is used to initialize `mapOptions`, it may not be set yet? – Paul Roub Nov 20 '15 at 14:59
  • @Frédéric, When i debug i can step down to ' navigator.geolocation.getCurrentPosition' without any problems, but when i expect to step into the function it just jumps over the statement and latLng are still null – HenrikD Nov 20 '15 at 15:00

1 Answers1

1

As Frédéric Hamidi, mentioned in the comment, navigator.geolocation might be not available in the browser you are testing.

But there are some more things that needs to be done. You cannot access latlang before it populated. So, you have to change your code to something like below, even if you are testing in a browser which has navigator.geolocation.

initMap: function() {
    var latLng;

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(
            function(position) {
                latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
                var mapOptions = {
                      center: latLng,
                      zoom: 12,
                      mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                this.oMap = new google.maps.Map(this.getView().byId("map_canvas").getDomRef(), mapOptions);
                this.geocoder = new google.maps.Geocoder();
                this.directionsService = new google.maps.DirectionsService();
                this.directionsDisplay = new google.maps.DirectionsRenderer({
                     draggable: true,
                     map: this.oMap,
                     suppressMarkers: true
                 });

                 this.createCurrentLocationMarker();
                 this.loadDataFromModel();
            }.bind(this), function() {
                latLng = new google.maps.LatLng(37.4, -122.1)
            }, {
                timeout: 10000
            }
        );
    }; 
}

Now, your code will run after latlang is available and also notice .bind(this) to make sure, this same in the callback.