在我的应用程序中,我想确定用户的当前位置.不过,在这方面,我确实有几个问题:

  1. 有不同的位置Provider ,哪一个最准确?GPS Provider号还是Network Provider号?

  2. 这些供应商之间的差异有多大?它们是如何运作的?

  3. 你能给我提供一些关于如何开始在我的应用程序中实现GPS功能的代码片段或教程吗?

推荐答案

There are 3 location providers in Android.

它们是:

gps –> (GPS, AGPS): Name of the GPS location provider. This provider determines location using satellites. Depending on conditions, this provider may take a while to return a location fix. Requires the permission android.permission.ACCESS_FINE_LOCATION.

network –> (AGPS, CellID, WiFi MACID): Name of the network location provider. This provider determines location based on availability of cell tower and WiFi access points. Results are retrieved by means of a network lookup. Requires either of the permissions android.permission.ACCESS_COARSE_LOCATION or android.permission.ACCESS_FINE_LOCATION.

passive –> (CellID, WiFi MACID): A special location provider for receiving locations without actually initiating a location fix. This provider can be used to passively receive location updates when other applications or services request them without actually requesting the locations yourself. This provider will return locations generated by other providers. Requires the permission android.permission.ACCESS_FINE_LOCATION, although if the GPS is not enabled this provider might only return coarse fixes. This is what Android calls these location providers, however, the underlying technologies to make this stuff work is mapped to the specific set of hardware and telco provided capabilities (network service).

The best way is to use the “network” or “passive” provider first, and then fallback on “gps”, and depending on the task, switch between providers. This covers all cases, and provides a lowest common denominator service (in the worst case) and great service (in the best case).

在此处输入图像描述

文章参考:Android Location Providers - gps, network, passive By Nazmul Idris

代码参考:https://stackoverflow.com/a/3145655/28557

-----------------------Update-----------------------

现在Android有Fused location provider

融合位置Provider 智能管理底层定位技术,并根据您的需求为您提供最佳位置.它简化了应用程序获取用户当前位置的方式,提高了精确度并降低了功耗

融合位置提供程序提供三种获取位置的方式

  1. Last Location:当你想知道当前位置时使用一次.
  2. Request Location using Listener:当应用程序在屏幕/前端且需要继续定位时使用.
  3. Request Location using Pending Intent:当应用程序在后台且需要继续定位时使用.

References :

官网:http://developer.android.com/google/play-services/location.html

融合位置Provider 示例: GIT:https://github.com/kpbird/fused-location-provider-example

http://blog.lemberg.co.uk/fused-location-provider

--------------------------------------------------------

Android相关问答推荐

更新android Studio后gradle同步失败

如何使用喷气背包压缩让Animated Image Vector每次动画化一条路径?

在Android Studio中陷入了不兼容的Gradle版本的循环

NativeScript在`ns run android`上重复Kotlin类

为什么Ionic-checkbox不总是刷新Android上的视图?

在内部创建匿名对象的繁忙循环调用函数会产生开销吗?

我怎样才能画一条线在喷气背包组成和有一个自定义的角落?

空数据来自改装

我无法连接到信号机

如何将 Room Persistence 依赖项正确添加到我的 Jetack Compose Android 应用程序

如何在 compose 中使用可变对象?

面向Jetpack Compose的可组合放置问题

使用 Dagger Hilt 获取接口的所有实例

组成不重叠的元素

如何在 Jetpack Compose 中禁用 Horizo​​ntalPager 的分页动画

react 从输入中找到路径'lib/arm64-v8a/libfbjni.so'的本机2个文件

Kotlin Coroutines 会取代 AsyncTask 吗?

如何从我的 android 应用程序中删除 QUERY_ALL_PACKAGES 权限?

如何在 Jetpack Compose 中填充矢量图像的背景?

更新应用程序是否会取消对应用程序特定文件的权限?