我正在尝试使用下面的代码获取位置,但它返回0.0作为经度和纬度的值。Android位置返回0.0
public void getLocation(){
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0,0, locationListener);
if(isLocationEnabled(MainActivity.this, lm)){
Location location = new Location(LocationManager.NETWORK_PROVIDER);
Log.d("LOCATION",String.valueOf(location.getLatitude()));
getWeather(location.getLongitude(), location.getLatitude());
}else{
buildDialog(MainActivity.this);
}
}
然而,当我在MyLocationListener
类打印位置,它返回正确的价值观。我究竟做错了什么?
public class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
System.out.println("LATITUDE AND LONGITUDE" + latitude + " " + longitude);
}
}