private LocationManager locationManager;
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
1000, 5, new MyLocationListener());
Location location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
System.out.println("*************");
System.out.println("经度:" + location.getLongitude());
System.out.println("纬度:" + location.getLatitude());
} else {
System.out.println("空指针");
}
class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
show.setText("用户位置发生改变" + "\n经度:" + location.getLongitude()
+ "\n纬度:" + location.getAltitude() + "\n数据精确度:"
+ location.getAccuracy() + "\n时间:" + location.getTime()
+ "\n速度:" + location.getSpeed() + "\n方位:"
+ location.getBearing());
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
在这里说明一下,因为不是完整的代码,这只是其中的一个小模块。供大家使用,事先声明,这几行代码在真机上wify下运行没问题,但是在手机网络为移动数据时候,运行就不行了,不知道怎么回事,或许是因为这个原因所以好多人不喜欢用谷歌的定位系统吧,不过我觉得还行。个人意见,不同意不要喷,我也挺喜欢百度地图的,过几天准备研究研究。