在Android应用开发中,获取用户当前位置并展示在地图上是很多场景的必备功能,比如出行类、本地生活类应用都会用到。下面我们就一步步实现这个功能。

前期准备
首先需要在项目中集成地图SDK,这里以Google Maps API为例,你需要在Google Cloud控制台创建项目,开启Maps SDK for Android服务,获取API密钥,然后在项目的AndroidManifest.xml中添加如下配置:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.locationmapdemo">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application>
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="你的API密钥" />
</application>
</manifest>申请位置权限
Android 6.0及以上版本需要动态申请危险权限,位置权限属于危险权限,所以需要在代码中主动申请,避免应用直接崩溃。
// 检查位置权限是否授予
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// 未授予则发起权限申请
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
LOCATION_PERMISSION_REQUEST_CODE);
}
// 处理权限申请结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == LOCATION_PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 权限授予成功,开始获取位置
getLastLocation();
} else {
Toast.makeText(this, "需要位置权限才能使用该功能", Toast.LENGTH_SHORT).show();
}
}
}获取用户当前位置
我们可以使用LocationManager来获取用户的位置信息,优先获取最后一次已知位置,减少位置获取的耗时。
private void getLastLocation() {
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 检查位置服务是否开启
boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGpsEnabled && !isNetworkEnabled) {
Toast.makeText(this, "请开启位置服务", Toast.LENGTH_SHORT).show();
return;
}
// 获取最后一次已知位置
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Location lastLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (lastLocation != null) {
// 已经有位置信息,直接展示
updateMapWithLocation(lastLocation);
} else {
// 没有缓存位置,监听实时位置
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 1, locationListener);
}
}
}
// 位置监听回调
LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(@NonNull Location location) {
updateMapWithLocation(location);
// 获取到位置后停止监听,避免持续耗电
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.removeUpdates(this);
}
};在地图上显示位置
获取到位置经纬度后,就可以在Google Map上添加标记并移动到对应位置,首先在布局文件中添加地图控件:
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />然后在代码中初始化地图,添加位置标记:
private void updateMapWithLocation(Location location) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
if (mapFragment != null) {
mapFragment.getMapAsync(googleMap -> {
LatLng userLatLng = new LatLng(location.getLatitude(), location.getLongitude());
// 添加位置标记
googleMap.addMarker(new MarkerOptions()
.position(userLatLng)
.title("当前位置"));
// 移动相机到该位置,缩放级别设为15
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(userLatLng, 15));
// 开启我的位置图层,显示蓝色定位点
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true);
}
});
}
}常见问题说明
- 如果获取位置一直返回null,先检查位置服务是否开启,尽量在开阔区域测试GPS定位
- 动态权限申请时,要处理好用户拒绝权限的场景,避免应用出现无响应情况
- 实时位置监听会持续消耗电量,非必要场景建议在获取到位置后及时停止监听
- 地图API密钥需要对应应用的包名和签名,配置错误会导致地图无法正常加载
Android_Studio用户位置获取地图显示LocationManagerGoogle_Maps_API修改时间:2026-06-02 04:30:57