[ACCEPTED]-Google map for android my location custom button-android-maps

Accepted answer
Score: 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 3 in java class, declare your location button:

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

Click 2 Event:

imgMyLocation.setOnClickListener(new OnClickListener() {

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

}

Get Location Method, in that just 1 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);
    }
    });
Score: 19

With a simple trick, you can replace the 3 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 2 a new button to your desired location in 1 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();

            }
         }
    });
Score: 14

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

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

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

Score: 3

This might be useful to someone who is looking 3 for changing the location icon directly, instead 2 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 1 the right thing to be done.

Score: 0

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

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

Then you can manipulate the imageView then.

imageView.setImageResource(R.drawable.icon_custom_location);

More Related questions