4
String logloc ="";
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null) {
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();
    logloc = String.valueOf(latitude)+","+String.valueOf(longitude);
    notfi(logloc); 
}

在上面的代码中,即使我的 gps 设备处于打开状态,并且在建筑物外和建筑物内,我的位置总是为空,我只得到空值,请帮助我运行此代码

4

2 回答 2

1

只是因为您的设备没有最后一个已知位置,这就是原因。首先为onLocationChanged(). 它将获取当前位置并将其保存为lastKnownLocation. 然后该方法将起作用。

您可以在此处获取当前位置的帮助

如何在 Android 中以编程方式获取当前 GPS 位置?

于 2013-08-23T06:49:19.043 回答
0

像这样尝试你的活动必须是这样的

Activity extends MapActivity implements LocationListener

 LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
 Criteria criteria = new Criteria();
 String provider = locationManager.getBestProvider(criteria, false);
 Location location = locationManager.getLastKnownLocation(provider);

    if (location != null) {
        System.out.println("Provider " + provider + " has been selected.");
        lat = (double) (location.getLatitude());
        lng = (double) (location.getLongitude());

        Log.i(TAG, "Lattitude:" + lat);
        Log.i(TAG, "Longitude:" + lng);

    }
于 2013-08-23T07:03:55.770 回答