Cannot get location with Geolocation


#1

After i get platform.ready() in the constructor, i try to get the location when the native google maps is ready. As explaine here Geolocation Doc.

  // create a new map using element ID
  let map = new GoogleMap('map');

  map.one(GoogleMapsEvent.MAP_READY).then(() => {
    console.log('Map is ready!');

  //this is not working
  Geolocation.getCurrentPosition().then(pos => {
      console.log('lat: ' + pos.coords.latitude + ', lon: ' + pos.coords.longitude);
  });

I’m debuging the app on an android emulator, the maps renders properly, everything is fine except i can’t get an latlng… When wathing the android device monitor it tells me:

 No location to return for getLastLocation()

Sounds like it goes search for only the last known location but not a new one with the GPS. I need a way for the app to get the location with the GPS. I tried with watchPosition :

let watch = Geolocation.watchPosition().subscribe(pos => {
   console.log('lat: ' + pos.coords.latitude + ', lon: ' + pos.coords.longitude);
});

But Atoms (and the compiler) tells me that coords does not exist on type geoposition


#2

Hmm, well those types definitely are there.

https://github.com/driftyco/ionic-native/blob/master/src/plugins/geolocation.ts#L7

Can you log out pos first and see what that is returning?


#3

i’ll check this out tomorow.

Other than that, is there a way to ask the GPS for coords without getCurrentPosition()

or this last one is supposed to do so and I’m doing things wrong ?


#4

 map.one(GoogleMapsEvent.MAP_READY).then(() => {
    console.log('Map is ready!');
    console.log('----getCurrentPosition---');
    //ne focntionne pas
    Geolocation.getCurrentPosition().then(pos => {
      console.log('lat: ' + pos.coords.latitude + ', lon: ' + pos.coords.longitude);
    }, (err) => {
      console.log(err);
    });


    console.log('----getCurrentPosition---');
    let watch = Geolocation.watchPosition().subscribe(pos => {
      console.log('pos', pos);
    });

#5

I tried this code :

Geolocation.getCurrentPosition({enableHighAccuracy:true, timeout:5000, maximumAge:0}).then(position => {
    let latLng = new GoogleMapsLatLng(position.coords.latitude, position.coords.longitude);

    console.log(latLng);
  }, (err) => {
    console.log(err);
  });

But still problems, i guess it cannot get any position:


#6

OK for those who will end up in the same same situation as me (using the native google map)
Dont bother using this Geolocation that doesn’t work (I hate it so much).

Use map.getMyLocation :

this.map.getMyLocation().then(res => {
          console.log('Give it to me' +  res.latLng);

          let position: CameraPosition = {
            target: res.latLng,
            zoom: 18,
            tilt: 30
          };

          this.map.moveCamera(position);
        });

GitHub Doc

@CordovaInstance()
   getMyLocation(options?: MyLocationOptions): Promise<MyLocation> { return; }

export interface MyLocation {
 latLng?: GoogleMapsLatLng;
 speed?: number;
 time?: string;
 bearing?: number;
}