我创建了一个GPS跟踪应用程序。
不幸的是,应用程序没有显示实际位置(纬度,长度)。它始终显示lat:50.059644 long:19.9206669。但我想得到并显示用户(我)的实际位置。
这是我的主要活动:
Button btnShowLocation;
GPSTracker gps;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
btnShowLocation = (Button) findViewById(R.id.btn1);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
gps = new GPSTracker(About.this);
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
gps.showSettingsAlert();
}
}
});
}
这是我的GPS追踪器:
public class GPSTracker extends Service implements LocationListener{
private final Context mContext;
boolean isGPSEnabled = false;
boolean canGetLocation = false;
Location location;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
try{
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!isGPSEnabled) {
System.out.println("disabled");
//no network provider is enabled
} else {
this.canGetLocation = true;
if(isGPSEnabled){
System.out.println("gps wlaczony");
if (location == null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if(locationManager != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
} catch(Exception e){
e.printStackTrace();
}
return location;
}
答案 0 :(得分:1)
您在此处请求位置更新:
locationManager.requestLocationUpdates
但不要等待更新到来。
您必须注册您的请求并为onLocationChanged方法提供监听器。当您拥有新位置时,将调用该方法。
除此之外,为了使过程正常工作,每次点击Btn时都无法初始化GPSTracker。做一次并等待更新。
试试这个:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about);
btnShowLocation = (Button) findViewById(R.id.btn1);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
gps = new GPSTracker(About.this);
}
});
}
public class GPSTracker extends Service implements LocationListener{
private final Context mContext;
boolean isGPSEnabled = false;
boolean canGetLocation = false;
Location location;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
protected LocationManager locationManager;
public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}
public Location getLocation() {
locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(!isGPSEnabled) {
System.out.println("disabled");
//no network provider is enabled
} else {
this.canGetLocation = true;
if(isGPSEnabled){
System.out.println("gps wlaczony");
if (location == null){
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if(locationManager != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
@Override
public void onLocationChanged(Location location) {
double latitude = location.getLatitude();
double longitude = location.getLongitude();
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show()
}
}