首页 文章

如何使用“nativescript-google-maps-sdk”获取谷歌 Map 中的当前位置?

提问于
浏览
0

我正在构建一个关于nativescript Angular2的应用程序 . 我已经从npm下载了"nativescript-google-maps-sdk"插件 . 如果我启用了 setMyLocationEnabled(true) ,我会在屏幕的右上角找到"my-location"按钮并单击它会将我带到我的实际位置 .

我想要做的是以编程方式获得这些坐标,因为我将需要它们用于其他操作(标记,接近值等) . 通过他们的代码,但无法找到他们如何获得当前位置 . gMap.getMyLocation() 已弃用,所以我可以在这里写一下:https://developers.google.com/android/reference/com/google/android/gms/maps/GoogleMap我们应该使用FusedLocationProviderApi . 如果这个插件没有使用它,那么它如何获取当前位置?

任何人都能解释一下吗?

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

    var gMap = args.gMap;
    gMap.setMyLocationEnabled(true);
    // gMap.getMyLocation(); deprecated
    // need to get current location coordinates, somehow...
}

2 回答

  • 0

    nativescript-google-maps-sdk插件不支持从设备获取您的位置 .

    您需要从nativescript-geolocation获取位置(您已经这样做了),然后将其传递给google-map .

    如果您查看google-maps插件的AndroidManifest.xml,则它无权访问该设备的位置 .

  • 1

    因此,事实证明,您可以通过两种方式从您的设备获取您的位置:

    • 内置android LocationManager

    • 使用google play服务定位模块,它使用 FusedLocationProviderApi ,它 Build 在默认的android LocationManager上

    与我所读到的不同之处在于谷歌的版本更先进 - 它可以自动切换不同的位置模式(gps,wifi)并节省电池电量 .

    所以,为了使用googles的方式,我们需要:

    导入google play服务位置模块(表示最新版本):

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

    然后初始化播放服务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();
    

相关问题