Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何获取位置或仅获取id以提供基于位置的内容?_Android_Api_Google Maps_Geolocation_Beacon - Fatal编程技术网

Android 如何获取位置或仅获取id以提供基于位置的内容?

Android 如何获取位置或仅获取id以提供基于位置的内容?,android,api,google-maps,geolocation,beacon,Android,Api,Google Maps,Geolocation,Beacon,我正在构建一个android应用程序,我希望我的应用程序能够提供基于位置的内容。例如,如果我在一个博物馆附近或在一个博物馆内,请向我展示该博物馆的详细信息。问题是如何识别那个地方。最好的方法是什么 信标能很好地完成这项工作,当信标发出带有id的信号时,我的应用程序会读取id并提供相关信息。但我需要一些时间才能获得一些信标,因此如果有任何类似的技术,请在这里列出 GPS技术或API只要简单而不过于复杂就行,因为我只想让我的应用程序验证它是否已到达某个位置,并以此为基础,显示内容 我有哪些选择?最简

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

信标能很好地完成这项工作,当信标发出带有id的信号时,我的应用程序会读取id并提供相关信息。但我需要一些时间才能获得一些信标,因此如果有任何类似的技术,请在这里列出

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


我有哪些选择?

最简单的选择可能是使用手机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;
}
获取位置的另一种优雅方式是实现反应式方式

这种方法允许使用lambda保持代码简洁,链接观察值&使用延迟计算。实现这一点的一种方法是将库和库结合起来,以实现灵活性

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

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) {
      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   

})
下面的
lastLocation()
属于
Maybe
,这是一个表示延迟Rx计算的延迟实现

它允许
onSuccess
onComplete
onError
作为互斥事件运行,这与可观察事件不同,因此,如果位置不可用,则不会向订户返回任何值

使用此库将LocationServices.FusedLocationApi包装在
rxLocation.location()
中:

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

private void latestLocation(final String apiSearch) {
  LocationRequest locationRequest = LocationRequest.create()
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
      .setInterval(6000); // 6s
我们订阅位置更新,以防止位置返回空值,这在传统的非反应性方法中是可能的。使用此库将LocationServices.FusedLocationApi包装在
rxLocation.location()
中:

因为下面的
fromLocation()
调用属于
Maybe
,所以它返回Maybe。我们通过
toObservable
将其转换为一个可观察的,用于支持线程并发的
flatMap
。此库将Geocoder API封装在
rxLocation.geocoding()
中:

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


希望这有帮助。

最简单的选择可能是使用手机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;
}
获取位置的另一种优雅方式是实现反应式方式

这种方法允许使用lambda保持代码简洁,链接观察值&使用延迟计算。实现这一点的一种方法是将库和库结合起来,以实现灵活性

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

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) {
      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   

})
下面的
lastLocation()
属于
Maybe
,这是一个表示延迟Rx计算的延迟实现

它允许
onSuccess
onComplete
onError
作为互斥事件运行,这与可观察事件不同,因此,如果位置不可用,则不会向订户返回任何值

使用此库将LocationServices.FusedLocationApi包装在
rxLocation.location()
中:

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

private void latestLocation(final String apiSearch) {
  LocationRequest locationRequest = LocationRequest.create()
      .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
      .setInterval(6000); // 6s
我们订阅位置更新,以防止位置返回空值,这在传统的非反应性方法中是可能的。使用此库将LocationServices.FusedLocationApi包装在
rxLocation.location()
中:

因为下面的
fromLocation()
调用属于
Maybe
,所以它返回Maybe。我们通过
toObservable
将其转换为一个可观察的,用于支持线程并发的
flatMap
。此库将Geocoder API封装在
rxLocation.geocoding()
中:

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


希望这对您有所帮助。

您可以使用地理定位API了解用户的位置。您可以使用地理定位API了解用户的位置。