Google map for android my location custom button
Asked Answered
S

5

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?

Stadtholder answered 27/5, 2014 at 7:14 Comment(8)
great. it was second question :)Stadtholder
yes and wat about first, found solution or not? :)Mcphee
hey found anything or not?Mcphee
not yet. because it pending now.Stadtholder
hmmm so not got any solution till now, I have sample code for itMcphee
yes. waiting you code :)Stadtholder
Let us continue this discussion in chat.Mcphee
Hello, 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.Kolomna
M
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);
    }
    });
Mcphee answered 29/5, 2014 at 6:1 Comment(4)
your answer helped me a lot. thanx +1 for you.Hosmer
what is getLatitude/getLongitudeKrawczyk
Its your latitude and longitude which you want to use there. @AlexSorokoletovMcphee
Hello, 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.Kolomna
H
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();

            }
         }
    });
Hallagan answered 28/4, 2018 at 18:49 Comment(2)
I did like in this example, but sometimes crash occures #61613743 This is my questionCongelation
I did it diferenttly, but using the same principles, thank you very, very much.Sourdine
F
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

Freeze answered 8/1, 2017 at 23:24 Comment(2)
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.Kolomna
@JaiminModi can you elaborate a bit more? If I could see a screenshot of the problem that might helpFreeze
N
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.

Nave answered 5/10, 2018 at 7:37 Comment(1)
I did like in this example, but sometimes crash occures #61613743 This is my questionCongelation
M
1

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);
Magdala answered 12/1, 2021 at 7:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.