我正在尝试通过 GPS 功能获取用户的当前位置,
写了一个简单的类来实现LocationListener
public class LocationManagerHelper implements LocationListener {
private static double latitude;
private static double longitude;
@Override
public void onLocationChanged(Location loc) {
latitude = loc.getLatitude();
longitude = loc.getLongitude();
}
@Override
public void onProviderDisabled(String provider) { }
@Override
public void onProviderEnabled(String provider) { }
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
public static double getLatitude() {
return latitude;
}
public static double getLongitude() {
return longitude;
}
}
并通过一个简单的操作,我正在访问这些经度和纬度值
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** create a TextView and write Hello World! */
TextView tv = new TextView(this);
LocationManager mlocManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new LocationManagerHelper();
mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,
mlocListener);
if (mlocManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
tv.append("Latitude:- " + LocationManagerHelper.getLatitude()
+ '\n');
tv.append("Longitude:- " + LocationManagerHelper.getLongitude()
+ '\n');
} else {
tv.setText("GPS is not turned on...");
}
/** set the content view to the TextView */
setContentView(tv);
但它总是返回 0.0 作为结果。无法找出问题所在。