NativeScript / nativescript-geolocation

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

App crashes when I call getCurrentLocation #247

Closed imbrendino closed 4 years ago

imbrendino commented 4 years ago

The app is just closing itself without any alert also in the log. I tried both on an emulator and real device. This is the code:

import { Component, OnInit } from "@angular/core";
import { RadSideDrawer } from "nativescript-ui-sidedrawer";
import * as app from "tns-core-modules/application";
import { MapView, Style } from "nativescript-google-maps-sdk";
import { registerElement } from "nativescript-angular";
import { FileReaderService } from "../core/fileReader.service";
import * as platform from "tns-core-modules/platform";
import * as permissions from "nativescript-permissions";
import * as geolocation from "nativescript-geolocation";
import { Accuracy } from "tns-core-modules/ui/enums";

registerElement("MapView", () => MapView);

declare var android: any;

@Component({
    selector: "Home",
    templateUrl: "./home.component.html"
})
export class HomeComponent implements OnInit {

    latitude =  -33.86;
    longitude = 151.20;
    zoom = 8;
    minZoom = 0;
    maxZoom = 22;
    bearing = 0;
    tilt = 0;
    padding = [40, 40, 40, 40];
    mapView: MapView;
    allowExecution = false;

    lastCamera: string;

    constructor(private fileReader: FileReaderService) {
        // Use thecomponent constructor to inject providers.
    }

    ngOnInit(): void {
        // Init your component properties here.
    }

    onDrawerButtonTap(): void {
        const sideDrawer = <RadSideDrawer>app.getRootView();
        sideDrawer.showDrawer();
    }

    // Map events
    onMapReady(event) {

        console.log("Map Ready");

        const style = require("../../assets/mapStyle.json");

        this.mapView = event.object;
        this.mapView.setStyle(style);

        this.fetchMyLocation();
        console.log("Setting a marker...");
    }

    onCoordinateTapped(args) {
        this.fetchMyLocation();
        console.log("Coordinate Tapped, Lat: " + args.position.latitude + ", Lon: " + args.position.longitude, args);
    }

    onMarkerEvent(args) {
        console.log("Marker Event: '" + args.eventName
            + "' triggered on: " + args.marker.title
            + ", Lat: " + args.marker.position.latitude + ", Lon: " + args.marker.position.longitude, args);
    }

    onCameraChanged(args) {
        console.log("Camera changed: " + JSON.stringify(args.camera), JSON.stringify(args.camera) === this.lastCamera);
        this.lastCamera = JSON.stringify(args.camera);
    }

    onCameraMove(args) {
        console.log("Camera moving: " + JSON.stringify(args.camera));
    }

    fetchMyLocation() {
        geolocation
            .getCurrentLocation({
                desiredAccuracy: Accuracy.high,
                maximumAge: 1000,
                timeout: 20000
            })
            .then((res) => {
                this.latitude = res.latitude;
                this.longitude = res.longitude;
            })
            .catch((e) => {
                console.log("oh frak, error", e);
            });
    }
}
imbrendino commented 4 years ago

I didn't create the app/App_Resources/Android/before-plugins.gradle file with the Google Play Services Version

I was putting it in the app.gradle