2

I am building an app on nativescript+Angular2. I have downloaded the "nativescript-google-maps-sdk" plugin from npm. If I enable setMyLocationEnabled(true), I get the "my-location" button on the upper right corner of the screen and clicking it takes me to my actual location.

What I would like to do is to get these coordinates programmaticaly, because I will need them for other operations (markers, proximity values etc.). Ran through their code, but couldn't find how they are getting this current location. gMap.getMyLocation() is deprecated, so I can't use that, based on what's written here: https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap We should be using FusedLocationProviderApi. If this plugin isn't using it, then how does it acquire current location?

Can anyone shed some light?

mapReady(args) {
    console.log("Map Ready");

    var gMap = args.gMap;
    gMap.setMyLocationEnabled(true);
    // gMap.getMyLocation(); deprecated
    // need to get current location coordinates, somehow...
}
3
  • to be able to get current location you can use nativescript-geolocation plugin. For further help you can review this example - github.com/NativeScript/nativescript-sdk-examples-ng/tree/…. Commented Oct 18, 2016 at 5:35
  • I already was using the nativescript-geolocation plugin, to get my coordinates, but then it hit me - why would I need to use 2 plugins if gmaps already had a built in feature to locate you. I guess there is no way around this. Gona stick with it then, thank you. p.s. those examples are outdated I guess, seeing a "Location" plugin there, which is deprecated now. Commented Oct 18, 2016 at 5:58
  • With NativeScript 1.5.0 the Location module has been deprecated and has been moved to external plugin called nativescript-geolocation, which is supported from NativeScript developers team. Commented Oct 18, 2016 at 6:12

2 Answers 2

2

The nativescript-google-maps-sdk plugin doesn't support getting your location from the device.

You need to get the location from nativescript-geolocation ( you are already doing that) and then pass that to the google-map.

If you check the google-maps plugin's AndroidManifest.xml, it doesn't have the permission to access the device's location.

Sign up to request clarification or add additional context in comments.

1 Comment

If you check the google-maps plugin's AndroidManifest.xml, it doesn't have the permission to access the device's location. - It may not have a permission explicitly set there, but it still gets my location data when I set gMap.setMyLocationEnabled(true).
1

So, as it turns out, you can get your location from your device in 2 ways:

  • with built in android LocationManager
  • with google play services location module, which uses FusedLocationProviderApi which is built on default android LocationManager

The difference, from what I've read, is that the googles' version is more advanced - it switches from different location modes (gps, wifi) automatically and saves up your battery.

So, in order to use the googles' way, we need to:

Import google play services location module (+ means newest version):

dependencies {
    compile 'com.google.android.gms:play-services-location:+'
}

Then initialise the play services API:

declare var com: any;
GoogleApiClient = com.google.android.gms.common.api.GoogleApiClient;
LocationServices = com.google.android.gms.location.LocationServices;
var dis = this; // writing in typescript, so this is reference to our current component where this code will lay

// Create an instance of GoogleAPIClient.
if (this.googleApiClient == null) {
    this.googleApiClient = new dis.GoogleApiClient.Builder(application.android.context)
        .addConnectionCallbacks(new dis.GoogleApiClient.ConnectionCallbacks({
            onConnected: function() {
                console.log("GoogleApiClient: CONNECTED");
            }.bind(this),
            onConnectionSuspended: function() {
                console.log("GoogleApiClient: SUSPENDED");
            }.bind(this)
        }))
        .addOnConnectionFailedListener(new dis.GoogleApiClient.OnConnectionFailedListener({
            onConnectionFailed: function() {
                console.log("GoogleApiClient: CONNECTION ERROR");
            }.bind(this)
        }))
        .addApi(dis.LocationServices.API)
        .build();
}

this.googleApiClient.connect();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.