我嘗試在Android中使用GPS_Provider
查找位置。當嘗試使用Network_Provider
獲得位置比它給我的位置和調用回調函數正常,但是當我嘗試使用GPS_Provider
獲得位置比它給我的位置空 &也不會調用回調函數的LocationListener
onLocationChanged
。GPS位置提供程序在Android中給我空位置
我的代碼是:
LocationManager lm;
boolean isNetwork = false;
Location location;
LocationListener locationListener;
LocationListener loc;
MyLocation myLocation;
Location location1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lm = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
try {
myLocation = new MyLocation();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
Log.e("Location", "Permission Not Given");
return;
}
Log.e("Location", "Permission is Given");
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, myLocation);
location1=lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}catch(Exception e)
{
}
}
MyLocation.class
public class MyLocation implements LocationListener {
@Override
public void onLocationChanged(Location location) {
if(location!=null)
{
String lat,Longi;
lat=String.valueOf(location.getLatitude());
Longi=String.valueOf(location.getLongitude());
Log.e("Latitude: " , lat);
Log.e("Longitude: " , Longi);
}
else
Log.e("Location is null", "Location not Found");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e("Latitude: " ,""+ provider+ " Status: "+status);
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Log.e("Latitude: " , provider);
}
}
它給我的logcat錯誤如下:
02-04 00:50:52.280 14480-14480/com.arc.locationusingnetwork E/Location: Permission is Given
02-04 00:50:52.360 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 46
02-04 00:50:52.370 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 49
02-04 00:50:52.370 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.380 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.380 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-04 00:50:52.390 14480-14480/com.arc.locationusingnetwork E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 52
添加完整日誌跟蹤 –
@VPS:嘗試使用Google Fused Location API進行位置更新。 * GPS不能在屋頂下工作。 – kevz
'location1 = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);'你在說這是空嗎? –