0
如何通過Android中的GPS獲取當前位置。我使用下面的代碼..如何通過Android中的GPS獲取當前位置
但是,當我使用LastKnowKnown位置時,它拋出空指針異常..代碼如下
請任何機構給予幫助。如何解決?
public void getlocation()
{
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
}
class MyLocationListener implements LocationListener
{
String a;
String b;
String c;
public void onLocationChanged(Location loc)
{
if (loc != null) {
LAT = loc.getLatitude();
LONG = loc.getLongitude();
a=Double.toString(LAT);
b=Double.toString(LONG);
c="-----LAT is:"+a+" "+"LONG is--------:"+b;
System.out.println(c);
}
}
public void getPresentLocation() {
String a;
String b;
String c;
lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
LAT = location.getLatitude();
LONG = location.getLongitude();
a=Double.toString(LAT);
b=Double.toString(LONG);
c="-----LAT is:"+a+" "+"LONG is--------:"+b;
System.out.println(c);