找不到用户位置

时间:2011-09-07 10:05:38

标签: java android

我不久前发了一个问题。基本上我要做的是让我的位置经理返回我的经度和纬度。我的getBestProvider()方法返回网络,但是我的locationManager.getLastKnownLocation(provider)返回null。如你所见,我已经实现了监听器。我一定做错了。

这是代码。

public class Activity1 extends Activity implements LocationListener {
       private LocationManager locationManager;
       private String provider;



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    readFile();




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

            Criteria criteria = new Criteria();
            provider = locationManager.getBestProvider(criteria, false);
            System.out.println(provider);
            System.out.println(locationManager.getProviders(criteria, false));
            System.out.println(locationManager.getProvider("network"));
            System.out.println(locationManager.getAllProviders());


            Location location = locationManager.getLastKnownLocation(provider);

            // Initialize the location fields
            if (location != null) {
                System.out.println("Provider " + provider + " has been selected.");
                int lat = (int) (location.getLatitude());
                int lng = (int) (location.getLongitude());
                System.out.println(String.valueOf(lat));
                System.out.println(String.valueOf(lng));

            } else {
                System.out.println("Provider not available");
                System.out.println("Provider not available");
            }


}@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(provider, 400, 1, this);
}

/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
    super.onPause();
    locationManager.removeUpdates(this);
}

@Override
public void onLocationChanged(Location location) {
    int lat = (int) (location.getLatitude());
    int lng = (int) (location.getLongitude());
    System.out.println(String.valueOf(lat));
    System.out.println(String.valueOf(lng));
}

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

}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(this, "Enabled new provider " + provider,
            Toast.LENGTH_SHORT).show();

}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(this, "Disenabled provider " + provider,
            Toast.LENGTH_SHORT).show();
}

}

1 个答案:

答案 0 :(得分:0)

    LocationManager locationManager;
    String context = Context.LOCATION_SERVICE;
    locationManager = (LocationManager)getSystemService(context);
    String provider = LocationManager.NETWORK_PROVIDER;
    Location location = locationManager.getLastKnownLocation(provider);
    updateWithNewLocation(location);
}

private void updateWithNewLocation(Location location){
    String latLongString;TextView myLocationText;
    myLocationText = (TextView)findViewById(R.id.myLocationText);
    if (location != null) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        latLongString = "Lat:" + lat + "\nLong:" + lng;
    } else { latLongString = "No location found"; }
}