Replace Default Android Maps API V2 Change Mylocation Icon

Replace default Android Maps API v2 Change MyLocation icon

The latest update to Google Play Services now allows you to use 'Flat' Markers and rotate them which is exactly what I needed. Here is a simple version of the way I implemented it. There is probably a fair bit I can do to optimize and tweak the animation, but it does the job for the moment. Any feedback is welcome.

Marker mPositionMarker;
GoogleMap mMap;

@Override
public void onLocationChanged(Location location) {

if (location == null)
return;

if (mPositionMarker == null) {

mPositionMarker = mMap.addMarker(new MarkerOptions()
.flat(true)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.positionIndicator))
.anchor(0.5f, 0.5f)
.position(
new LatLng(location.getLatitude(), location
.getLongitude())));
}

animateMarker(mPositionMarker, location); // Helper method for smooth
// animation

mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(location
.getLatitude(), location.getLongitude())));

}

public void animateMarker(final Marker marker, final Location location) {
final Handler handler = new Handler();
final long start = SystemClock.uptimeMillis();
final LatLng startLatLng = marker.getPosition();
final double startRotation = marker.getRotation();
final long duration = 500;

final Interpolator interpolator = new LinearInterpolator();

handler.post(new Runnable() {
@Override
public void run() {
long elapsed = SystemClock.uptimeMillis() - start;
float t = interpolator.getInterpolation((float) elapsed
/ duration);

double lng = t * location.getLongitude() + (1 - t)
* startLatLng.longitude;
double lat = t * location.getLatitude() + (1 - t)
* startLatLng.latitude;

float rotation = (float) (t * location.getBearing() + (1 - t)
* startRotation);

marker.setPosition(new LatLng(lat, lng));
marker.setRotation(rotation);

if (t < 1.0) {
// Post again 16ms later.
handler.postDelayed(this, 16);
}
}
});
}

Android Google Maps V2 change My Location Icon

Ok, so i just figured that if you don't add the title or snippet, the marker will not have effect on a click event, and it acts as an image overlay. Even though it's not perfect, it suits my needs

private Marker marker = mMap.addMarker(new MarkerOptions()
.position( latLng)
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));

Thanks everyone

Android Google Maps API V2: How to change my location icon?

I think, that v2 does not support custom location icon, but you can work-around it by disabling your location and setting up custom location listener, which adds/updates your custom groundoverlay on every location update.

How to replace blue dot with my icon on google maps v2 in android?

You will have to stop using GoogleMap.setMyLocationEnabled and write a bit more code, including receiving location updates on your own and adding Circle for accuracy.

How to change the position or remove the default GPS icon of google maps?

to remove Location Button in onMapReady add

map = googleMap;
map.getUiSettings().setMyLocationButtonEnabled(false);

How to change by default blue round location icon in google map fragment in Android?

Try this,works for me

  ImageView btnMyLocation = (ImageView) ((View) mapFragment.getView().findViewById(Integer.parseInt("1")).getParent()).findViewById(Integer.parseInt("2"));
btnMyLocation.setImageResource(R.drawable.ic_current_location);

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)
btnMyLocation.getLayoutParams();

layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layoutParams.setMargins(0, 0, 30, 30);
btnMyLocation.setLayoutParams(layoutParams);


Related Topics



Leave a reply



Submit