NativeScript / nativescript-geolocation

Geolocation plugin to use for getting current location, monitor movement, etc
Apache License 2.0
139 stars 76 forks source link

Error while getting current location #210

Closed anaszgh closed 5 years ago

anaszgh commented 5 years ago

Platform: Android Type: Device - Huawei P10

Please, provide the following version numbers that your issue occurs with:

"dependencies": {
        "@angular/animations": "~7.1.0",
        "@angular/common": "~7.1.0",
        "@angular/compiler": "~7.1.0",
        "@angular/core": "~7.1.0",
        "@angular/forms": "~7.1.0",
        "@angular/http": "~7.1.0",
        "@angular/platform-browser": "~7.1.0",
        "@angular/platform-browser-dynamic": "~7.1.0",
        "@angular/router": "~7.1.0",
        "nativescript-angular": "~7.1.0",
        "nativescript-directions": "^1.3.0",
        "nativescript-geolocation": "^5.0.0",
        "nativescript-gradient": "^2.0.1",
        "nativescript-image": "^2.1.0",
        "nativescript-localize": "^4.1.1",
        "nativescript-snackbar": "^4.1.0",
        "nativescript-theme-core": "~1.0.4",
        "reflect-metadata": "~0.1.12",
        "rxjs": "~6.3.0",
        "tns-core-modules": "~5.1.0",
        "zone.js": "~0.8.26"
    },
    "devDependencies": {
        "@angular/compiler-cli": "^7.2.12",
        "@nativescript/schematics": "~0.5.0",
        "@ngtools/webpack": "~7.1.0",
        "nativescript-dev-typescript": "~0.7.0",
        "nativescript-dev-webpack": "~0.19.0"
    },

I have went through the documentation step by step, i have added google play services in app.gradle

android {  
  defaultConfig {  
    generatedDensities = []
  }  
  aaptOptions {  
    additionalParameters "--no-version-vectors"  
  }  
  project.ext {
    googlePlayServicesVersion = "11.2.+"
  }
} 

I'm suing the plugin along with the directions plugin, here is the code below

 private grantPermission(): Promise<boolean> {
        return new Promise<boolean>((resolve, reject) => {
            geolocation.enableLocationRequest()
                .then((result) => {
                    resolve(true);
                })
                .catch((reason) => {
                    resolve(false);
                })
        });
    }
    public getCurrentLocation(): Promise<geolocation.Location> {
        return new Promise<geolocation.Location>((resolve, reject) => {
            geolocation.getCurrentLocation({
                desiredAccuracy: Accuracy.high,

            })
                .then((result) => {
                    resolve(result);
                })
                .catch((reason) => {
                    reject(reason);
                });
        })
    }
    public getDirections(lng: string, lat: string) {
        this.getCurrentLocation()
            .then((result) => {
                let directions: Directions = new Directions();
                directions.available().then(avail => {
                    if (avail) {
                        directions.navigate({
                            from: { // optional, default 'current location'
                                lat: parseFloat(lat),
                                lng: parseFloat(lng)
                            },
                            to: [{
                                lat: result.latitude,
                                lng: result.longitude
                            }],
                            type: "driving", // optional, can be: driving, transit, bicycling or walking
                            ios: {
                                preferGoogleMaps: true, // If the Google Maps app is installed, use that one instead of Apple Maps, because it supports waypoints. Default true.
                                allowGoogleMapsWeb: true // If waypoints are passed in and Google Maps is not installed, you can either open Apple Maps and the first waypoint is used as the to-address (the rest is ignored), or you can open Google Maps on web so all waypoints are shown (set this property to true). Default false.
                            }
                        }).then(() => {
                            console.log("Maps app launched.");
                        }, error => {
                            console.log(error);
                        });
                    }
                });
            })
            .catch((reason) => {

            })
    }

geolocation.getCurrentLocation goes into the catch with the following message

TypeError: Cannot read property 'GoogleApiAvailability' of undefined
    at _isGooglePlayServicesAvailable (geolocation.js:225)
    at geolocation.js:210
    at new ZoneAwarePromise (zone-nativescript.js:902)
    at _makeGooglePlayServicesAvailable (geolocation.js:209)
    at geolocation.js:179
    at ZoneDelegate.invoke (zone-nativescript.js:388)
    at Object.onInvoke (core.umd.js:16201)
    at ZoneDelegate.invoke (zone-nativescript.js:387)
    at Zone.run (zone-nativescript.js:138)
    at zone-nativescript.js:883
    at ZoneDelegate.invokeTask (zone-nativescript.js:421)
    at Object.onInvokeTask (core.umd.js:16192)
    at ZoneDelegate.invokeTask (zone-nativescript.js:420)
    at Zone.runTask (zone-nativescript.js:188)
    at drainMicroTaskQueue (zone-nativescript.js:595)
tgpetrov commented 5 years ago

Hi @anaszgh, There is a similar thread with a discussion of some issues including the above-mentioned error. Here's the answer that sums it up.