android 获取地理位置
- 通过网络获取
- 通过GPS获取
获取地理位置工具类
import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
public class GPSUtils {
private static String TAG = GPSUtils.class.getSimpleName();
private static GPSUtils mInstance;
private Context mContext;
private static LocationListener mLocationListener = new LocationListener() {
// Provider的状态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "onStatusChanged");
}
// Provider被enable时触发此函数,比如GPS被打开
@Override
public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled");
}
// Provider被disable时触发此函数,比如GPS被关闭
@Override
public void onProviderDisabled(String provider) {
Log.d(TAG, "onProviderDisabled");
}
//当坐标改变时触发此函数,如果Provider传进相同的坐标,它就不会被触发
@Override
public void onLocationChanged(Location location) {
}
};
private GPSUtils(Context context) {
this.mContext = context;
}
public static GPSUtils getInstance(Context context) {
if (mInstance == null) {
mInstance = new GPSUtils(context.getApplicationContext());
}
return mInstance;
}
private boolean isWifi(Context mContext) {
ConnectivityManager connectivityManager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
if (info != null
&& info.getType() == ConnectivityManager.TYPE_WIFI) {
return true;
}
return false;
}
/**
* 获取地理位置,先根据GPS获取,再根据网络获取
*
* @return
*/
@SuppressLint("MissingPermission")
public Location getLocation() {
Location location = null;
try {
if (mContext == null) {
return null;
}
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
if (locationManager == null) {
return null;
}
if (isWifi(mContext)) {
location = getLocationByNetwork();
} else {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) { //从gps获取经纬度
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {//当GPS信号弱没获取到位置的时候再从网络获取
location = getLocationByNetwork();
}
} else { //从网络获取经纬度
location = getLocationByNetwork();
}
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return location;
}
/**
* 判断是否开启了GPS或网络定位开关
*
* @return
*/
public boolean isLocationProviderEnabled() {
boolean result = false;
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
if (locationManager == null) {
return result;
}
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
result = true;
}
return result;
}
/**
* 获取地理位置,先根据GPS获取,再根据网络获取
*
* @return
*/
@SuppressLint("MissingPermission")
private Location getLocationByNetwork() {
Location location = null;
LocationManager locationManager = (LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE);
try {
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, mLocationListener);
location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return location;
}
}
通过GPS获取还需要进行授权
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
private void initPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//检查权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
//请求权限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
boolean flag = grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED;
if (flag) {
Location location = GPSUtils.getInstance(this).getLocation();
if(location != null)
{
Logger.i("设备位置:经度:" + location.getLatitude() + ",纬度:" + location.getLongitude());
}
//DataCenter.setAppLoaction(GPSUtils.getInstance(this).getLocation());
}
}
}
网友评论