How to customize the current location marker (GPS) in the Maps api v2 android?

Asked

Viewed 1,307 times

2

Well people I want to customize the blue dot of the gps in Maps and I want me to be able to move it anywhere on the map, as well as works in the Taxi apps..

  • 1

    Your question is "how to add the Marker" or "how to customize the blue dot"?

  • Customizing the blue dot

  • 1

    I think it would be better if you [Dit] also the title of your question.

1 answer

4

To change the default blue icon that defines the user’s location on your map, you first need to disable the automatic search for this location that is done with the method setMyLocationEnabled. So do the following first:

map.setMyLocationEnabled(false);

And also, considering you don’t use any mechanism "traker", I have a class like this, which 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 extending from AsyncTask to make a call in the background will fetch the user’s location by the GPS provider (GPS_PROVIDER) and also by the network (NETWORK_PROVIDER), what is available and what comes first.

As soon as I find onPostExecute is that the new marker will be added to your map and the Listener location will be canceled. If you wish that at each user location update the marker goes "walking", as a tracker, you will need to keep always listening and go removing the marker to add a new one with the new coordinates.

And finally, in his Activity has the call:

BuscarCoordenadasTask task = new BuscarCoordenadasTask();
task.execute();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.