android 获取手机位置信息

时间:2021-08-08 10:51:42

1,获取LocationManager 对象

LocationManager locaManager =(LocationManager) getSystemService(Context.LOCATION_SERVICE);


2,调用locationManager 中requuestLocationUpdates()函数


locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 500, 0, new LocationListener() {


@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub


}


@Override
public void onProviderEnabled(String provider) {
// 当GPS locationProvider 可用时,更新位置
updateView(locManager.getLastKnownLocation(provider));


}


@Override
public void onProviderDisabled(String provider) {
updateView(null);


}


@Override
public void onLocationChanged(Location location) {
// 当GPS定位信息发生改变时,更新位置
updateView(location);


Log.d("UPDATElocation", " 00000000");

}
});


3,获取经纬度洗洗

           private void updateView(Location newlocation) {


if (newlocation != null) {

Log.d("UPDATElocation", " 00000000");

StringBuilder sb = new StringBuilder();
sb.append("经度:");
sb.append(newlocation.getLongitude());
sb.append("\n纬度:");
sb.append(newlocation.getLatitude());


show.setText(sb.toString());
} else {
show.setText("xxx");
}
}