gpt4 book ai didi

android - LocationListener InSide AsyncTask

转载 作者:搜寻专家 更新时间:2023-11-01 09:13:08 25 4
gpt4 key购买 nike

您好,我是 Android 编程的新手,目前正在开发一个使用位置管理器获取用户位置并在 map 上放置标记的应用程序。我正在尝试使用 AsyncTask 来运行 LocationListener 并在用户位置发生变化时不断更新标记。这是我正在上的课...

public class IncidentActivity extends MapActivity{


public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setContentView(R.layout.incidentactivity);

mapView = (MapView)findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapView.setTraffic(true);


mapController = mapView.getController();
String coordinates[] = {"-26.167004","27.965505"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
geoPoint = new GeoPoint((int)(lat*1E6), (int)(lng*1E6));
mapController.animateTo(geoPoint);
mapController.setZoom(16);
mapView.invalidate();

new MyLocationAsyncTask().execute();

}



private class MyLocationAsyncTask extends AsyncTask<Void, Location, Void> implements LocationListener{
private double latLocation;
private Location l;

//location management variables to track and maintain user location
protected LocationManager locationManager;
protected LocationListener locationListener;

@Override
protected Void doInBackground(Void... arg0) {
Looper.prepare();
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 1, locationListener);

this.publishProgress(l);
return null;
}

@Override
protected void onPostExecute(Void result) {

super.onPostExecute(result);
}

@Override
protected void onProgressUpdate(Location... values) {

super.onProgressUpdate(values);
}

//this method is never executed i dont know why...?
public void onLocationChanged(Location location) {
if (location != null){
latLocation = location.getLatitude();
Toast.makeText(getBaseContext(), " Your latLocation :" + latLocation, Toast.LENGTH_LONG).show();
//Log.d("Your Location", ""+latLocation);
}
}

public void onProviderDisabled(String provider) {


}

public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub

}

public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub

}


}


}

最佳答案

我刚刚实现了这样的 AsyncTask:

class GetPositionTask extends AsyncTask<Void, Void, Location> implements LocationListener
{
final long TWO_MINUTES = 2*60*1000;
private Location location;
private LocationManager lm;

protected void onPreExecute()
{
// Configure location manager - I'm using just the network provider in this example
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
nearProgress.setVisibility(View.VISIBLE);
}

protected Location doInBackground(Void... params)
{
// Try to use the last known position
Location lastLocation = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

// If it's too old, get a new one by location manager
if (System.currentTimeMillis() - lastLocation.getTime() > TWO_MINUTES)
{
while (location == null)
try { Thread.sleep(100); } catch (Exception ex) {}

return location;
}

return lastLocation;
}

protected void onPostExecute(Location location)
{
nearProgress.setVisibility(View.GONE);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.removeUpdates(this);

// HERE USE THE LOCATION
}

@Override
public void onLocationChanged(Location newLocation)
{
location = newLocation;
}

public void onProviderDisabled(String provider) {}
public void onProviderEnabled(String provider) {}
public void onStatusChanged(String provider, int status, Bundle extras) {}
}

关于android - LocationListener InSide AsyncTask,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6794998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com