首页 > 解决方案 > 如何获取位置或简单的 id 来提供基于位置的内容?

问题描述

我正在构建一个 android 应用程序,我希望我的应用程序提供基于位置的内容。例如,如果我在博物馆附近或其中一个,向我显示有关该博物馆的详细信息。问题是如何识别那个地方。最好的方法是什么?

信标完美地完成了这项工作,它们发出带有 id 的信号,我的应用程序读取 id 并提供相关信息。但是我需要一些时间才能获得一些信标,所以如果有任何类似的技术,请列出它们下来这里。

GPS 技术或 API 只要简单而不会过于复杂就可以了,因为我只想让我的应用程序验证它已到达某个位置,并在此基础上显示内容。

我有什么选择?

标签: androidapigoogle-mapsgeolocationbeacon

解决方案


最简单的选择可能是使用您的手机 gps 位置,而不使用信标。

为此,您需要找到一个博物馆 api 来显示有关最近博物馆的信息。

然后,您可以LocationManager在 Android 中使用首先检查是否授予位置权限:

private Location  getLastKnownLocation() {
    Location n = null;
    LocationManager mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);

    List<String> locationProviders = mLocationManager.getProviders(true);

    for (String provider : locationProviders) {
if(ContextCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED) {

    n = mLocationManager.getLastKnownLocation(provider);
   }
  }
    return n;
}

然后我们需要做以下事情:

Location gps = getLastKnownLocation();

String latitude = Double.toString(gps.getLatitude());
Log.e(“User Location Latitude”, “” + gps.getLatitude());

String longitude = Double.toString(gps.getLongitude());
Log.e(“User Location Longitude”, “” + gps.getLongitude());

Uri baseUri = Uri.parse(“YOUR_API_URL” + latitude + “YOUR_API_URL” + longitude + “YOUR_API_URL”);
Log.e(“Api Query”, “” + baseUri);

Uri.Builder uriBuilder = baseUri.buildUpon();

获取位置的另一种优雅方法是实现getLastKnownLocationReactive 方式。

这种方法允许使用 lambdas、链接 observables 和使用延迟计算来保持代码简洁。这样做的一种方法是将RxPermissions库和RxLocation库结合起来以获得灵活性。

这篇文章的以下代码取自两个库提供的官方文档,并进行了一些修改以适应本次讨论:

final RxPermissions rxPermissions = new RxPermissions(this);

rxLocation = new RxLocation(this); 

就像之前的传统非反应式方法一样,我们创建getLastKnownLocation

private void getLastKnownLocation(final String apiSearch) {
    compositeDisposable.add(rxPermissions
        .request(Manifest.permission.ACCESS_FINE_LOCATION)
        .subscribe(granted -> {
           if (granted) {

下面lastLocation()属于 a Maybe<T>,它是一个惰性实现,表示延迟的 Rx 计算。

与 Observable 不同,它允许onSuccess, onComplete&onError作为互斥事件操作,这样如果位置不可用,则不会向订阅者返回任何值。

使用这个库将 LocationServices.FusedLocationApi 包装在rxLocation.location()

      rxLocation.location().lastLocation()

         .doOnSuccess(location -> {
// Your code implementation for Received Last Known Location: If last known location is already known & exists, to pass the existing last known location into your museum api, to fetch results to your adapter   
})

        .doOnComplete(() -> {
// Your code implementation for handling the Location Request, then doing a Completing action: This sends a new request to request the latest user location, before subscribing to your museum api, to fetch results to your adapter   

})
        .doOnError(throwable ->
// Your code implementation to handle any errors thrown: Pops a toast message to prompt user to enable GPS location on phone   

})

根据文档,我们发送用户位置请求:

private void latestLocation(final String apiSearch) {
  LocationRequest locationRequest = LocationRequest.create()
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
      .setInterval(6000); // 6s

我们订阅位置更新,以防止位置返回 null,这在传统的非反应式方法中是可能的。使用这个库将 LocationServices.FusedLocationApi 包装在rxLocation.location()

rxLocationSubscriber = rxLocation.location().updates(locationRequest)

因为fromLocation()下面的调用属于 a Maybe<T>,所以它返回一个 Maybe。我们通过 将其转换为 Observable toObservable,与它一起使用flatMap,支持线程并发。该库将 Geocoder API 包装在rxLocation.geocoding()

.flatMap(location ->

    rxLocation.geocoding().fromLocation(location).toObservable())

.subscribe(location -> {

    // your code implementation for museum api

    rxLocationSubscriber.dispose();
  });
} 

我认为最直接的方法是概述的第一种方法。

希望这会有所帮助。


推荐阅读