saudblaze
saudblaze

Reputation: 150

ionic 3 geo-location not working for the second time

I have a problem using Geolocation on Android device.

I create an Ionic 3 application. I use Geolocation ( [https://ionicframework.com/docs/native/geolocation/ 13]). I use the sample code provided there. The problem is that, the code works in Browser but whenever I upload it on an Android device, it does not work.

When I start the app on device the alert for location permission is prompted. I press Allow. Then in the android tray an icon appears that says “Searching for GPS”. After timeout expires I got the error for timeout and no location.

The strange think is this: If I close my app, then open google maps ( or other app using gps position ) and reopen my app works as it should.

I don’t know what to do.

The permissions are fine :

and everything was installed properly.

Can anyone help please?

Upvotes: 1

Views: 468

Answers (3)

saudblaze
saudblaze

Reputation: 150

i got it actually i have to pass apikey with that like

<script src="http://maps.googleapis.com/maps/api/js?key=YOUR_KEY"></script>

in the index page

without this key it sometime works but not sure everytime.

Note : this key is only generated when you have a valid gmail a/c (by providing DOB/CreditCard nos) although its free .

Upvotes: 1

Franco Coronel
Franco Coronel

Reputation: 730

import { LocationAccuracy } from '@ionic-native/location-accuracy';

declare var navigator Above @Component

watchPosition() {

     this.locationAccuracy.request(this.locationAccuracy.REQUEST_PRIORITY_HIGH_ACCURACY).then(
        () => {
                var options = {
                    timeout: 500,
                    maximumAge: Infinity
                };

                this.watch = navigator.geolocation.watchPosition((data) => {
                    let latlng: LatLng = new LatLng(data.coords.latitude, data.coords.longitude);


                }, (error) => {
                    console.log("error watch", error)
                }, options);

            }
        }, error => {
            alert('Error requesting location permissions' + JSON.stringify(error))
        }); 
}

}

Upvotes: 0

AbdulAzeem
AbdulAzeem

Reputation: 529

try this, it should work

ionic cordova run android --prod

--prod makes production build apk

Upvotes: 0

Related Questions