0

我有以下课程

public class GPSManager  {


Context MyContext;
boolean GotLocation = false;
Location CurrentLocation;

public GPSManager(Context context){
    MyContext =  context;
}
LocationManager locationManager = (LocationManager)MyContext.getSystemService(MyContext.LOCATION_SERVICE);


LocationListener locationlistener = new LocationListener() {
    public void onLocationChanged(Location location) {
      // Called when a new location is found by the network location provider.
        GotLocation = true;
      CurrentLocation = location;
    }

//ETC

从活动中调用如下调用

GPSManager myGPS = new GPSManager(this);

但是当我运行它失败时,我相信它与上下文传递有关,因为在调试器中我认为(对于 Eclipse 来说仍然是新的)它是一个空值。

我想要的只是一个返回当前位置的类(不需要最后知道的位置)

4

1 回答 1

1

从您的活动中,您可以收到您当前的位置,添加
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); locationListener = new CurrentGPSLocation();locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
到您的 onCreate 函数中。
然后加...
private class CurrentGPSLocation implements LocationListener { @Override public void onLocationChanged(Location location) { if (location != null) { GeoPoint CurrentLocation = new GeoPoint( (int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6)); } }

于 2012-06-17T09:46:15.920 回答