android处理大数据_在Android中处理位置数据

android处理大数据

In this blog post, you’ll be learning about how to implement and leverage location data in Android.

在此博客文章中,您将学习如何在Android中实现和利用位置数据。

Leveraging location data from user devices, you can find nearby shops, cafes, cinemas, hospitals, your time zone, provide location-based suggestions to users, share locations with other users or 3rd parties, reach your destination, and more!

利用来自用户设备的位置数据,您可以找到附近的商店,咖啡馆,电影院,医院,您的时区,向用户提供基于位置的建议,与其他用户或第三方共享位置,到达目的地等等!

Today, most major applications like Google Maps, Facebook, Uber, Swiggy, etc. are using this feature to provide some of the best services to the users as well.

如今,大多数主要应用程序(例如Google Maps,Facebook,Uber,Swiggy等)都使用此功能向用户提供一些最佳服务。

All of this is achieved through GPS tracking information that gets beamed to various satellites orbiting the Earth. Apart from this, information from Wi-Fi and Bluetooth also affects the location you receive. Hence, the final location is a combination of data obtained through these tools. This is why the location is not always accurate—it’s more of an approximate location.

所有这些都是通过GPS跟踪信息实现的,该信息被发射到环绕地球运行的各种卫星上。 除此之外,来自Wi-Fi和蓝牙的信息也会影响您的接收位置。 因此,最终位置是通过这些工具获得的数据的组合。 这就是位置不总是准确的原因,它更像是一个近似位置。

位置访问的类型 (Types of Location Access)

Here are the following 4 different options for location access:

以下是用于位置访问的以下4个不同选项:

  • Allow only while using the app — This option allows developers to retrieve a user’s location only while the app is actively being used, i.e. the app is in the foreground. This is the most suitable and recommended way to get the location in the applications. This option was recently added to Android 10 and versions above it (screenshot attached below).

    仅在使用应用程序时允许-此选项允许开发人员仅在积极使用该应用程序(即该应用程序位于前台)时才检索用户的位置。 这是在应用程序中获取位置的最合适和推荐的方法。 此选项最近添加到了Android 10及其更高的版本中(下面的截图)。

  • One time only —Added in Android 11, this is the same as “Allow only while using the app”, but for a limited amount of time.

    仅一次-在Android 11中添加,与“仅在使用该应用程序时允许”相同,但时间有限。

  • Deny — This means that developers don't have any access to location information.

    拒绝 -这意味着开发人员无权访问位置信息。

  • Allow all the time — Allows location access all the time, but requires extra permission for Android 10 and higher.

    一直允许 -一直允许位置访问,但需要 Android 10及更高版本的额外权限

This blog will focus on the most common location permission option—Allow only while using the app. To receive location updates, your app must either have a visible activity or a service running in the foreground (with a notification).

该博客将重点介绍最常见的位置权限选项- 仅在使用该应用程序时允许 。 要接收位置更新,您的应用必须具有可见的活动或在前台运行的服务(带有通知)。

So let’s start coding!

因此,让我们开始编码!

1.添加Gradle依赖项 (1. Adding Gradle dependencies)

Your app can access the set of supported location services through classes in the com.google.android.gms.location package. So add it in your app’s build.gradle file:

您的应用可以通过com.google.android.gms.location包中的类访问支持的位置服务集。 因此,将其添加到应用程序的build.gradle文件中:

implementation 'com.google.android.gms:play-services-location:17.0.0'

2.添加权限 (2. Add the Permissions)

Now, to get access to the location information of the app, we need the help of the location provider. In Android, the 3 frequently-used location providers are:

现在,要访问应用程序的位置信息,我们需要位置提供者的帮助。 在Android中,三种常用的位置信息提供者是:

  1. GPS_PROVIDERThis provider determines location using satellites. Depending on conditions such as connectivity, climatic conditions, etc., this provider may take a while to return a fixed location. It requires the permission android.permission.ACCESS_FINE_LOCATION.

    GPS_PROVIDER 该提供商使用卫星确定位置。 根据连接性,气候条件等条件,此提供程序可能需要一段时间才能返回固定位置。 它需要权限android.permission.ACCESS_FINE_LOCATION

  2. NETWORK_PROVIDER This provider determines location based on the availability of cell tower and Wi-Fi 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.

    NETWORK_PROVIDER 该提供商根据蜂窝塔和Wi-Fi接入点的可用性来确定位置。 通过网络查找来检索结果。 需要android.permission.ACCESS_COARSE_LOCATIONandroid.permission.ACCESS_FINE_LOCATION权限。

So if we want to use any location provider, we must request the user’s permission by declaring the corresponding permissions in our Android manifest file:

因此,如果要使用任何位置提供程序,则必须通过在Android清单文件中声明相应的权限来请求用户的权限:

  • ACCESS_COARSE_LOCATIONIf you specify ACCESS_COARSE_LOCATION, the API can use Wi-Fi, mobile data, or both to determine the device's location. The API returns an approximate location.

    ACCESS_COARSE_LOCATION 如果指定ACCESS_COARSE_LOCATION ,则API可以使用Wi-Fi和/或移动数据来确定设备的位置。 API返回一个大概位置。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值