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
How to Display a Yes/No Dialog Box on Android
Android, Make an Image at a Url Equal to Imageview's Image
Set Span for Items in Gridlayoutmanager Using Spansizelookup
Is Secure.Android_Id Unique for Each Device
Is There a Simple Example of the Popupwindow Class Using Android V2.0
Starting the Android Emulator in Sdk Tools, Revision 12
How to Change the Fontsize in an Android Webview
Apache Commons Codec with Android: Could Not Find Method
Android Actionbar How to Add Supporting Library V7 Appcompat for Eclipse
Android Device Manager Fails to Launch After Updating to MACos Big Sur 11.3
Overlap Scrolling View with Appbarlayout
How to Animate View.Setvisibility(Gone)
Android Deep Linking: Use the Same Link for the App and the Play Store
Android:Actionbarstyle Requires API Level 11
Android Download Manager Completed
Android Persistence Room: "Cannot Figure Out How to Read This Field from a Cursor"