Android 中的许多应用程序需要用户位置访问权限才能获得更好的功能。
可以通过以下步骤在 Android 中获取用户的位置
1.在Gradle中添加依赖
在 build.gradle(app) 文件中添加以下依赖项:
dependencies {
implementation 'com.google.android.gms:play-services-location:19.0.0'
}
2.获取用户权限
可以使用以下任何权限
(在 Android Manifest.xml 文件中添加这些权限)
ACCESS_COARSE_LOCATION:它提供城市街区内的位置准确性
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
ACCESS_FINE_LOCATION:提供更准确的位置
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
ACCESS_BACKGROUND_LOCATION:如果应用程序在后台运行时需要访问用户的位置,我们需要将此权限与上述权限一起添加
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
📌 准确度
Android 支持以下级别的位置准确度:
- 近似
提供设备位置的估计值,大约在 1 英里(1.6 公里)范围内。当您声明 ACCESS_COARSE_LOCATION 权限而不是 ACCESS_FINE_LOCATION 权限时,您的应用会使用此级别的位置准确性。
- 精确
提供尽可能准确的设备位置估计,通常在大约 160 英尺(50 米)范围内,有时精确到 10 英尺(几米)或更好。当您声明 ACCESS_FINE_LOCATION 权限时,您的应用会使用此级别的位置准确性。
笔记:
在 Android 12(API 级别 31)或更高版本上,用户可以请求您的应用仅检索大致位置信息,即使您的应用请求 ACCESS_FINE_LOCATION 运行时权限也是如此。
要处理这种潜在的用户行为,请不要单独请求 ACCESS_FINE_LOCATION 权限。相反,在单个运行时请求中请求 ACCESS_FINE_LOCATION 权限和 ACCESS_COARSE_LOCATION 权限。如果您尝试仅请求 ACCESS_FINE_LOCATION,系统会在 Android 12 的某些版本上忽略该请求。
如果您的应用面向 Android 12 或更高版本,系统会在 Logcat 中记录以下错误消息:
ACCESS_FINE_LOCATION must be requested with ACCESS_COARSE_LOCATION
当您的应用同时请求 ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION 时,系统权限对话框包括以下用户选项:
精确:这允许您的应用获取精确的位置信息。
Approximate:这允许您的应用仅获取大概的位置信息。
3. 设计布局
创建一个TextView在屏幕上显示用户的经纬度
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Location here"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
4. 逻辑写作
我们在主 Activity 文件中编写逻辑。就我而言,它是 MainActivity.java。
创建一些基本变量
Location 对象来存储用户的位置,
FusedLocationProviderClient 对象来获取位置,一个整数请求代码的权限,以及 TextView 对象来显示获取的位置给用户
还创建了一个 fetchLocation() 函数来获取位置。
为了获取用户的最后位置,请使用 Java 公共类FusedLocationProviderClient。
它实际上是一种定位服务,结合了 GPS 定位和网络定位,以达到电池消耗和准确度之间的平衡。GPS 位置用于提供准确性,而网络位置用于在用户在室内时获取位置。
public class MainActivity extends AppCompatActivity {
Location currentLocation;
FusedLocationProviderClient fusedLocationProviderClient;
private static final int REQUEST_CODE = 101;
TextView textView;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fetchLocation();
}
}
所需的权限是
- Manifest.permission.ACCESS_COARSE_LOCATION
- Manifest.permission.ACCESS_FINE_LOCATION
String[] LocationPermissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION};
fetchLocation() 的逻辑如下
- 检查我们请求的权限是否已启用。
- 如果未启用,请请求权限。
- 如果接受权限并启用位置,则获取用户的最后位置
@RequiresApi(api = Build.VERSION_CODES.N)
private void fetchLocation() {
// Check for permissions
if (ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Request for permissions
ActivityCompat.requestPermissions(
this, LocationPermissions, REQUEST_CODE);
return;
} else if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(this, "Location cannot be determined", Toast.LENGTH_SHORT).show();
// Request for permissions
ActivityCompat.requestPermissions(
this, LocationPermissions, REQUEST_CODE);
}
// Getting last location from Fused Location Provider Client Object
fusedLocationProviderClient.getLastLocation().
addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location == null) {
requestNewLocationData();
} else {
currentLocation = location;
textView.setText("Latitude:"+currentLocation.getLatitude()+"\nLongitude:"+currentLocation.getLongitude());
}
}
});
}
OnRequestPermissionResult :
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
fetchLocation();
}
else {
// Permission not granted
Toast.makeText(this, "Location cannot be determined", Toast.LENGTH_SHORT).show();
currentLocation=null;
}
}
}
正如您在上面的 fetchLocation 方法中看到的,如果 fusedLocationProviderClient.getLastLocation() 获取的位置为空,我们会请求新的位置数据,因此会创建一个名为 requestNewLocationData() 的方法
fusedLocationProviderClient.getLastLocation().
addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location == null) {
requestNewLocationData(); // Here
} else {
// .....
}
}
请求新位置数据()
现在去requestNewLocationData()方法。
🚩 创建 LocationCallback 类的变量(用于在设备位置发生变化或无法确定时接收来自 FusedLocationProviderApi 的通知)
private LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location=locationResult.getLastLocation();
currentLocation=location;
}
};
🚩 创建一个新的 LocationRequest 变量。在这个对象上,设置多种方法,例如设置位置的准确度或间隔的优先级,进行位置请求。
🚩如果需要非常高的精度,请使用 PRIORITY_HIGH_ACCURACY 作为 setPriority(int) 方法的参数。对于城市级精度(低精度),请使用 PRIORITY_LOW_POWER。
🚩LocationRequest 对象准备好后,将其设置在 FusedLocationProviderClient 对象上以获取最终位置
private void requestNewLocationData() {
// Initializing LocationRequest object with appropriate methods
LocationRequest locationRequest = new LocationRequest();
// For a city level accuracy(low accuracy), use PRIORITY_LOW_POWER.
locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
// Set the desired interval for active location updates, in milliseconds.
locationRequest.setInterval(5);
// Explicitly set the fastest interval for location updates, in milliseconds
locationRequest.setFastestInterval(0);
// Set the number of location updates.
locationRequest.setNumUpdates(1);
// setting LocationRequest on FusedLocationClient
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
// Check for permissions
if (ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Request for permissions
ActivityCompat.requestPermissions(
this, LocationPermissions, REQUEST_CODE);
return;
} else if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(this, "Location cannot be determined", Toast.LENGTH_SHORT).show();
// Request for permissions
ActivityCompat.requestPermissions(
this, LocationPermissions, REQUEST_CODE);
}
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
现在您已经成功获取了用户的位置,并且可以根据需要在您的应用中实现这一点。
完整代码
完整的 MainActivity.java(在我的例子中)看起来像这样 -
package com.example.locationdemo;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Build;
import android.os.Bundle;
import android.os.Looper;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationCallback;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationResult;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
public class MainActivity extends AppCompatActivity {
Location currentLocation;
FusedLocationProviderClient fusedLocationProviderClient;
private static final int REQUEST_CODE = 101;
TextView textView;
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
fetchLocation();
}
String[] LocationPermissions = new String[]{Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION};
@RequiresApi(api = Build.VERSION_CODES.N)
private void fetchLocation() {
// Check for permissions
if (ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Request for permissions
ActivityCompat.requestPermissions(
this, LocationPermissions, REQUEST_CODE);
return;
} else if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(this, "Location cannot be determined", Toast.LENGTH_SHORT).show();
// Request for permissions
ActivityCompat.requestPermissions(
this, LocationPermissions, REQUEST_CODE);
}
// Getting last location from Fused Location Provider Client Object
fusedLocationProviderClient.getLastLocation().
addOnCompleteListener(new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
Location location = task.getResult();
if (location == null) {
requestNewLocationData();
} else {
currentLocation = location;
textView.setText("Latitude:"+currentLocation.getLatitude()+"\nLongitude:"+currentLocation.getLongitude());
}
}
});
}
private LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
Location location=locationResult.getLastLocation();
currentLocation=location;
}
};
private void requestNewLocationData() {
// Initializing LocationRequest object with appropriate methods
LocationRequest locationRequest = new LocationRequest();
// For a city level accuracy(low accuracy), use PRIORITY_LOW_POWER.
locationRequest.setPriority(LocationRequest.PRIORITY_LOW_POWER);
// Set the desired interval for active location updates, in milliseconds.
locationRequest.setInterval(5);
// Explicitly set the fastest interval for location updates, in milliseconds
locationRequest.setFastestInterval(0);
// Set the number of location updates.
locationRequest.setNumUpdates(1);
// setting LocationRequest on FusedLocationClient
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
// Check for permissions
if (ActivityCompat.checkSelfPermission
(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// Request for permissions
ActivityCompat.requestPermissions(
this, LocationPermissions, REQUEST_CODE);
return;
} else if (ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(this, "Location cannot be determined", Toast.LENGTH_SHORT).show();
// Request for permissions
ActivityCompat.requestPermissions(
this, LocationPermissions, REQUEST_CODE);
}
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
fetchLocation();
}
else {
// Permission not granted
Toast.makeText(this, "Location cannot be determined", Toast.LENGTH_SHORT).show();
currentLocation=null;
}
}
}
}
结论
这是文章的结尾。如果您喜欢这个,请务必关注。
如果大伙有什么好的学习方法或建议欢迎大家在评论中积极留言哈,希望大家能够共同学习、共同努力、共同进步。
小编在这里祝小伙伴们在未来的日子里都可以 升职加薪,当上总经理,出任CEO,迎娶白富美,走上人生巅峰!!
不论遇到什么困难,都不应该成为我们放弃的理由!
很多人在刚接触这个行业的时候或者是在遇到瓶颈期的时候,总会遇到一些问题,比如学了一段时间感觉没有方向感,不知道该从那里入手去学习,需要一份小编整理出来的学习资料的关注我主页或者点击文末微信卡片免费领取Android学习资料~
这里是关于我自己的Android 学习,面试文档,视频收集大整理,有兴趣的伙伴们可以看看~
如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢。