【问题标题】:How to get current location in google maps using "nativescript-google-maps-sdk"?如何使用“nativescript-google-maps-sdk”在谷歌地图中获取当前位置?
【发布时间】:2016-10-17 15:00:02
【问题描述】:

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

我想做的是以编程方式获取这些坐标,因为我将需要它们进行其他操作(标记、邻近值等)。 浏览他们的代码,但找不到他们如何获得这个当前位置。 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...
}

【问题讨论】:

  • 为了能够获取当前位置,您可以使用nativescript-geolocation 插件。如需进一步帮助,您可以查看此示例 - github.com/NativeScript/nativescript-sdk-examples-ng/tree/…
  • 我已经在使用 nativescript-geolocation 插件来获取我的坐标,但后来它击中了我——如果 gmaps 已经有一个内置功能来定位你,为什么我还需要使用 2 个插件。我想没有办法解决这个问题。那就坚持下去,谢谢。 p.s.我猜这些例子已经过时了,在那里看到一个“位置”插件,现在已弃用。
  • 在 NativeScript 1.5.0 中,Location module 已被弃用,并已移至名为 nativescript-geolocation 的外部插件,该插件由 NativeScript 开发团队提供支持。

标签: google-maps-android-api-2 nativescript angular2-nativescript


【解决方案1】:

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

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

如果您检查 google-maps 插件的 AndroidManifest.xml,它没有访问设备位置的权限。

【讨论】:

  • If you check the google-maps plugin's AndroidManifest.xml, it doesn't have the permission to access the device's location. - 它可能没有在那里明确设置权限,但是当我设置gMap.setMyLocationEnabled(true) 时它仍然会获取我的位置数据。
【解决方案2】:

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

  • 内置 android LocationManager
  • 带有谷歌播放服务定位模块,它使用FusedLocationProviderApi,它是在默认的android LocationManager 上构建的

据我所知,不同之处在于 google 的版本更先进 - 它会自动从不同的位置模式(gps、wifi)切换并节省电池电量。

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

导入 google play services 定位模块(+ 表示最新版本):

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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 2012-11-26
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多