Created
May 23, 2016 06:35
-
-
Save jp1017/dc87d742ae5d1edf9fad0b43223f74bb to your computer and use it in GitHub Desktop.
利用 LocationManager 实时获取 GPS 定位信息,wgs84格式
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 获取原生gps信息,代码放 MainActivity 里就可以 | |
*/ | |
public void getGPSInfo() { | |
LocationManager mManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); | |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) | |
!= PackageManager.PERMISSION_GRANTED | |
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) | |
!= PackageManager.PERMISSION_GRANTED) { | |
return; | |
} | |
Location mLocation = mManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); | |
updateGPSInfo(mLocation); | |
mManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 8, new LocationListener() { | |
@Override | |
public void onLocationChanged(Location location) { | |
updateGPSInfo(mLocation);//位置变化时,更新位置信息 | |
} | |
@Override | |
public void onStatusChanged(String provider, int status, Bundle extras) { | |
} | |
@Override | |
public void onProviderEnabled(String provider) { | |
// 当GPS LocationProvider可用时,更新位置 | |
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) | |
!= PackageManager.PERMISSION_GRANTED | |
&& ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) | |
!= PackageManager.PERMISSION_GRANTED) { | |
return; | |
} | |
updateGPSInfo(mManager.getLastKnownLocation(provider)); | |
} | |
@Override | |
public void onProviderDisabled(String provider) { | |
} | |
}); | |
} | |
/** | |
* 更新经纬度信息 | |
* @param location | |
*/ | |
public void updateGPSInfo(Location location) { | |
if (location != null) { | |
mLat = location.getLatitude(); | |
mLng = location.getLongitude(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
安卓系统常用的 GPS 坐标有下面几种: