Witam, otrzymuję z bazy danych informacje o położeniu, obiektem którego położenie chciałbym aktualizować co sekundę jest marker. W jaki sposób mogę to zrobić? Próbowałem Wywoływać AsyncTaska i podawać jako argument mapę lub marker lecz wyrzucało błędy. Kod który wklejam wykonałem do testów, zmienia co sekundę wartość wspolrzędnych.
private class LocationAsyncTask extends AsyncTask<MarkerOptions, Void, Void>
{
boolean running = true;
@Override
protected Void doInBackground(MarkerOptions... markerOptions) {
Log.i("Location: ", "doInBackground");
while (this.running) {
SystemClock.sleep(3000);
if(count==0)
{
Log.i("doInBackground: ", "AsyncTask: 0");
moto_longitude = 19.91834;
}
if(count==1)
{
Log.i("doInBackground: ", "AsyncTask: 1");
moto_longitude = 19.91842;
}
markerOptions[0].position(new LatLng(moto_latitude, moto_longitude));
count++;
if(count==2)
{
count = 0;
}
}
return null;
}
public void stopStream() {
this.running = false;
}
}
private GoogleMap mMap;
private static final int LOCATION_REQUEST = 500;
double moto_latitude = 50.066421;
double moto_longitude = 19.91842;
LocationAsyncTask locationAsyncTask;
MarkerOptions markerOptions;
public int count = 0;
@SuppressLint("ServiceCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.addMarker(new MarkerOptions().position(new LatLng(moto_latitude,moto_longitude)).title("E-Moto is here!"));
// Add a marker in moto and move the camera
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(moto_latitude,moto_longitude), 18), 5000, null);
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
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, LOCATION_REQUEST);
return;
}
//create marker
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(new LatLng(moto_latitude, moto_longitude));
//add moto marker to map
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mMap.addMarker(markerOptions);
//set my location enabled
mMap.setMyLocationEnabled(true);
// locationAsyncTask = new LocationAsyncTask();
// locationAsyncTask.execute(markerOptions);
}
}