修改名称。
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package com.xscm.moduleutil.utils;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.location.Address;
|
||||
import android.location.Geocoder;
|
||||
import android.os.Looper;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
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 java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class LocationUtils {
|
||||
private static final String TAG = "LocationUtils";
|
||||
private FusedLocationProviderClient fusedLocationClient;
|
||||
|
||||
public interface LocationCallbackInterface {
|
||||
void onLocationReceived(double latitude, double longitude, String city);
|
||||
void onFailed(String errorMessage);
|
||||
}
|
||||
|
||||
public LocationUtils(Context context) {
|
||||
fusedLocationClient = LocationServices.getFusedLocationProviderClient(context);
|
||||
|
||||
}
|
||||
|
||||
private void getLastLocation() {
|
||||
}
|
||||
|
||||
public void getLastLocation(Context context,LocationCallbackInterface callback) {
|
||||
if (ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
|
||||
// 请求定位权限
|
||||
ActivityCompat.requestPermissions(
|
||||
(Activity) context,
|
||||
new String[]{
|
||||
Manifest.permission.ACCESS_FINE_LOCATION,
|
||||
Manifest.permission.ACCESS_COARSE_LOCATION
|
||||
},
|
||||
1001 // 自定义常量,比如 1001
|
||||
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
fusedLocationClient.getLastLocation()
|
||||
.addOnSuccessListener(location -> {
|
||||
if (location != null) {
|
||||
// 获取城市名称
|
||||
String city = getCityName(context, location.getLatitude(), location.getLongitude());
|
||||
callback.onLocationReceived(location.getLatitude(), location.getLongitude(), city);
|
||||
} else {
|
||||
callback.onFailed("无法获取位置");
|
||||
}
|
||||
})
|
||||
.addOnFailureListener(e -> callback.onFailed("定位失败:" + e.getMessage()));
|
||||
}
|
||||
|
||||
public void requestLocationUpdates(Context context,LocationCallbackInterface callback) {
|
||||
LocationRequest locationRequest = LocationRequest.create();
|
||||
locationRequest.setInterval(10000); // 每10秒更新一次
|
||||
locationRequest.setFastestInterval(5000);
|
||||
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
|
||||
|
||||
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
|
||||
callback.onFailed("定位权限未授予");
|
||||
return;
|
||||
}
|
||||
|
||||
fusedLocationClient.requestLocationUpdates(locationRequest, new LocationCallback() {
|
||||
@Override
|
||||
public void onLocationResult(@NonNull LocationResult locationResult) {
|
||||
if (locationResult == null) {
|
||||
callback.onFailed("无位置更新");
|
||||
return;
|
||||
}
|
||||
|
||||
for (android.location.Location location : locationResult.getLocations()) {
|
||||
String city = getCityName(context, location.getLatitude(), location.getLongitude());
|
||||
callback.onLocationReceived(location.getLatitude(), location.getLongitude(), city);
|
||||
}
|
||||
}
|
||||
}, Looper.getMainLooper());
|
||||
}
|
||||
|
||||
private String getCityName(Context context, double latitude, double longitude) {
|
||||
Geocoder geocoder = new Geocoder(context, Locale.getDefault());
|
||||
try {
|
||||
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
|
||||
if (addresses != null && !addresses.isEmpty()) {
|
||||
Address address = addresses.get(0);
|
||||
return address.getLocality(); // 城市名
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "地理编码失败", e);
|
||||
}
|
||||
return "未知城市";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user