android获取位置的经纬度(原生和google)
由于用户主体是在国外,就没有考虑高德和百度或者腾讯地图的库了,如果是国内,建议用国内的地图库。
1.原生(java实现)
实现类代码和注释如下,这个代码排版感觉不太会弄。
public class LocationMethodUtil {
private LocationManager locationManager;
private String locationProvider;//位置提供器
private final long timeInterval =5000;
private MlocationCallback mlocationCallback =null;
private static LocationMethodUtil instance;
private Context mContext;
public static LocationMethodUtil getInstance(@NonNull final Context context) {
if (instance ==null) {
synchronized (LocationMethodUtil.class) {
if (instance ==null) {
instance =new LocationMethodUtil(context);}
}
}
return instance;
}
LocationListener mListener =new LocationListener() {
@Override
public void onStatusChanged(String provider,int status,Bundle extras) {//状态改变时调用
}
@Override
public void onProviderEnabled(String provider) {//provider启用时调用
}
@Override
public void onProviderDisabled(String provider) {//provider失效时调用
}
// 如果位置发生变化,重新显示
@SuppressLint("LongLogTag")
@Override
public void onLocationChanged(Location location) {
showLocation(location);
}
};
public LocationMethodUtil(Context context) {
mContext = context;
initLocation(context);
}
void initLocation(Context context){
//1.获取位置管理器
locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Criteria criteria =new Criteria();
Log.e("----tag---","locationManager");//设置粗略精确度
criteria.setAccuracy(Criteria.ACCURACY_FINE);//2.获取位置提供器,GPS或是NetWork
Listproviders =locationManager.getProviders(criteria,true);
if (providers.contains(LocationManager.NETWORK_PROVIDER)) {//如果是网络定位
locationProvider =LocationManager.NETWORK_PROVIDER;
}else if (providers.contains(LocationManager.GPS_PROVIDER)) {
locationProvider =LocationManager.GPS_PROVIDER;
}else {
Log.e("----tag---","没有可用的位置提供器");
}
@SuppressLint("MissingPermission")
private void getLocation(Context context) {//获取位置
if(ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_FINE_LOCATION) !=PackageManager.PERMISSION_GRANTED &&ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_COARSE_LOCATION) !=PackageManager.PERMISSION_GRANTED) {
return;
}
//获取上次的位置
Location location ;
if (locationProvider !=null){
location =locationManager.getLastKnownLocation(locationProvider);
if (mListener !=null)
locationManager.requestLocationUpdates(locationProvider,timeInterval,1,mListener);//监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
if (location !=null) {
showLocation(location);
}
}else {
initLocation(mContext);
}
}
private void showLocation(Location location) {
mlocationCallback.locationSuccess(location);
String address ="纬度:" + location.getLatitude() +"经度:" + location.getLongitude();
}
@SuppressLint("MissingPermission")
public void stopLocation(Context context) {
if(ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_FINE_LOCATION) !=PackageManager.PERMISSION_GRANTED &&ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_COARSE_LOCATION) !=PackageManager.PERMISSION_GRANTED &&mListener ==null) {
return;
}
locationManager.removeUpdates(mListener);
}
public void startLocation(Context context) {
Log.e("---tag---" ,"开始定位");
if(ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_FINE_LOCATION) !=PackageManager.PERMISSION_GRANTED &&ActivityCompat.checkSelfPermission(context,Manifest.permission.ACCESS_COARSE_LOCATION) !=PackageManager.PERMISSION_GRANTED) {//没有权限
return;
}
if (locationProvider==null){
initLocation(mContext);
}else {
getLocation(context);
}
public void addCallback(MlocationCallback callbacks) {
mlocationCallback = callbacks;
}
}
2.google的位置库(kotlin实现)
首先集成
implementation'com.google.android.gms:play-services-location:18.0.0'
具体实现类如下
object LocationGoogle {
private lateinit var mLocationRequest:LocationRequest
private val UPDATE_INTERVAL = (10 *1000 /* 10 secs */).toLong()
private val FASTEST_INTERVAL:Long =5000 /* 2 sec */
@SuppressLint("MissingPermission")
fun startLocationUpdates(context:Context) {//创建位置的更新请求
mLocationRequest = LocationRequest()
mLocationRequest.priority =LocationRequest.PRIORITY_HIGH_ACCURACY
mLocationRequest.interval =UPDATE_INTERVAL
mLocationRequest.fastestInterval =FASTEST_INTERVAL
val builder =LocationSettingsRequest.Builder() // 设置位置请求参数,创建请求对象
builder.addLocationRequest(mLocationRequest)
val locationSettingsRequest =builder.build()
val settingsClient =LocationServices.getSettingsClient(context)
settingsClient.checkLocationSettings(locationSettingsRequest)
LocationServices.getFusedLocationProviderClient(context).requestLocationUpdates(
mLocationRequest,object : LocationCallback() {
override fun onLocationResult(locationResult:LocationResult) {
onLocationChanged(locationResult.lastLocation)
}},
Looper.myLooper()
)}
fun onLocationChanged(location:Location) {// 获取位置的位置参数
val msg ="Updated Location: " + java.lang.Double.toString(location.latitude) +"," + java.lang.Double.toString(location.longitude)//位置参数
}
}
谷歌地图在国内有时候获取不到位置,不知道是不是区域的原因。原生获取效率叶不是很理想,如果有大佬有更高效的方法,敬请赐教。