最简单的选择可能是使用您的手机 gps 位置,而不使用信标。
为此,您需要找到一个博物馆 api 来显示有关最近博物馆的信息。
然后,您可以在Android中使用LocationManager首先检查是否授予了位置权限:
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();
另一种获取位置的优雅方法是实现getLastKnownLocation Reactive 方式。
这种方法允许使用 lambdas、链接可观察对象和使用延迟计算来保持代码简洁。一种方法是将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() 属于Maybe<T>,这是一个惰性实现,表示延迟的Rx 计算。
它允许onSuccess、onComplete 和onError 作为互斥事件操作,这与 Observable 不同,这样如果位置不可用,则不会向订阅者返回任何值。
使用这个库将 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() 调用属于Maybe<T>,所以它返回一个Maybe。我们通过toObservable将其转换为Observable,与支持线程并发的flatMap一起使用。这个库在rxLocation.geocoding() 中封装了Geocoder API:
.flatMap(location ->
rxLocation.geocoding().fromLocation(location).toObservable())
.subscribe(location -> {
// your code implementation for museum api
rxLocationSubscriber.dispose();
});
}
我认为最直接的方法是概述的第一种方法。
希望这有帮助。