Well folks I want to customize the blue dot of the GPS in Maps and I want that I can move it to any place of the map, as well as it works in the applications of Taxi.
Well folks I want to customize the blue dot of the GPS in Maps and I want that I can move it to any place of the map, as well as it works in the applications of Taxi.
To change the default blue icon that defines the location of the user on your map, you first need to disable the automatic search of this location that is done with the setMyLocationEnabled
. Then do the following first:
map.setMyLocationEnabled(false);
And also, considering that you do not use some "traker" mechanism, I have a class like this that I add inside my Activity :
private class BuscarCoordenadasTask extends AsyncTask<Void, Void, Void> {
private LocationManager locationManager;
private Location userLocation;
private LocationListener locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
userLocation = location;
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
@Override
protected void onPreExecute() {
super.onPreExecute();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0.0f, locationListener);
}
if (locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0.0f, locationListener);
}
} else {
// Nenhum provedor de localização, cancela requisição
cancel(true);
}
}
@Override
protected Void doInBackground(Void... params) {
while (userLocation == null) {}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
locationManager.removeUpdates(locationListener);
// Adiciona localização do usuário no mapa, com novo ícone
map.addMarker(new MarkerOptions().position(userLocation).icon(BitmapDescriptorFactory.fromResource(R.drawable.icon_meu_local)));
}
@Override
protected void onCancelled() {
super.onCancelled();
locationManager.removeUpdates(locationListener);
}
}
In short, the class that extends from AsyncTask
to make a call in the background will fetch the location of the user by the GPS provider ( GPS_PROVIDER
) and also by the network ( NETWORK_PROVIDER
), whichever is available and come first.
As soon as you find in onPostExecute
, the new marker will be added to your map and the location listener will be canceled. If you want each marker user's location update to "walk" like a tracker , you will need to keep listening and remove the marker to add a new one with the new coordinates.
Finally, in your Activity you have the call:
BuscarCoordenadasTask task = new BuscarCoordenadasTask();
task.execute();