27

How can I change google map my location default button? I set my location enable and map draw standard image to find location, is it possible to change default image?

Nininea
  • 2,671
  • 6
  • 31
  • 57

5 Answers5

34

See below xml file to custom button:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <fragment
        android:id="@+id/maps"
        android:name="pl.mg6.android.maps.extensions.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp" >

        <ImageView
            android:id="@+id/imgMyLocation"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:scaleType="fitXY"
            android:src="@drawable/track_my_location" />
    </LinearLayout>

</RelativeLayout>

Then in java class, declare your location button:

private ImageView imgMyLocation;
        imgMyLocation = (ImageView) findViewById(R.id.imgMyLocation);

Click Event:

imgMyLocation.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                getMyLocation();

}

Get Location Method, in that just pass your current latitude and longitude.

private void getMyLocation() {
        LatLng latLng = new LatLng(Double.parseDouble(getLatitude()), Double.parseDouble(getLongitude()));
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(latLng, 18);
        googleMap.animateCamera(cameraUpdate);
    }
    });
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
19

With a simple trick, you can replace the my location button with a custom one.

  1. Customize the Layout of your new button.
  2. Set my location enabled in maps api.
  3. Hide default button of my location.
  4. call click method of my location on your custom button click.

1. Customize the layout of your new button

add a new button to your desired location in layout file of map activity.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:map="http://schemas.android.com/apk/res-auto"
     xmlns:app="http://schemas.android.com/apk/res-auto"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_height="match_parent"
     android:layout_width="match_parent">

    <fragment
         android:id="@+id/map"
         android:name="com.google.android.gms.maps.SupportMapFragment"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context=".activities.MapsActivity" />

   <ImageView
        android:id="@+id/ic_location"
        android:layout_width="18dp"
        android:layout_height="18dp"
        android:src="@drawable/ic_location"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="18dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>

2. Set my location enabled in maps api

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    // Enable My Location
    mMap.setMyLocationEnabled(true);
    /* and DON'T disable the default location button*/
 }

3. Hide default button of my location.

    //Create field for map button.
    private View locationButton;

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMyLocationEnabled(true);

        // get your maps fragment
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
        // Extract My Location View from maps fragment 
        locationButton = mapFragment.getView().findViewById(0x2);
        // Change the visibility of my location button
        if(locationButton != null)
            locationButton.setVisibility(View.GONE);
    }

4. call click method of my location on your custom button click.

findViewById(R.id.ic_location).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mMap != null)
            {
                if(locationButton != null)
                    locationButton.callOnClick();

            }
         }
    });
Basil Victor
  • 111
  • 2
  • 13
TrustCoder
  • 246
  • 2
  • 6
  • I did like in this example, but sometimes crash occures https://stackoverflow.com/questions/61613243/binding-mapview-findviewbyidinteger-parseint1-getparent-nullpointerexcep This is my question – TikTak May 05 '20 at 12:59
  • I did it diferenttly, but using the same principles, thank you very, very much. – Luis Felipe de Melo Jun 11 '21 at 14:56
15

If you want to retain the smoothness of the default My Location button but not the look, call the following lines:

//Make sure you have explicit permission/catch SecurityException
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(false);

Now you can start & stop location updates while keeping the default behavior

Zach
  • 447
  • 6
  • 16
  • Hello @Zach, I am using Clustering and This current location Circle is overlapping my cluster count. Is there any way to put this current location circle to back of cluster count ? Thanks. – Jaimin Modi Dec 19 '17 at 06:54
  • @JaiminModi can you elaborate a bit more? If I could see a screenshot of the problem that might help – Zach Feb 06 '18 at 22:17
3

This might be useful to someone who is looking for changing the location icon directly, instead of using any trick or workaround:

locationButton = (mapView.findViewById<View>(Integer.parseInt("1")).parent as View)
    .findViewById<ImageView>(Integer.parseInt("2"))

locationButton.setImageResource(R.drawable.ic_location_current)`

Here findViewById<ImageView> is the right thing to be done.

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
  • I did like in this example, but sometimes crash occures https://stackoverflow.com/questions/61613243/binding-mapview-findviewbyidinteger-parseint1-getparent-nullpointerexcep This is my question – TikTak May 05 '20 at 13:00
0

I get the default ImageView of my location using tag instead of idfor preventingExpected resource of type id` lint warning -

ImageView imageView = ((ImageView)mapFragment.getView().findViewWithTag("GoogleMapMyLocationButton"));

Then you can manipulate the imageView then.

imageView.setImageResource(R.drawable.icon_custom_location);
Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42