Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google map for android my location custom button

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?

like image 832
Nininea Avatar asked May 27 '14 07:05

Nininea


People also ask

Can you add your own address to Google Maps?

Google Maps – Setting Your New Home Address 1) Make sure you have the updated version of Google Maps on your phone, then open it. At the bottom, tap on 'Contribute'. Next, tap on 'Add Place'. 2) Time to fill in all the relevant details, and to make sure that everything is as complete as possible!


2 Answers

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);     }     }); 
like image 171
Pratik Dasa Avatar answered Oct 24 '22 14:10

Pratik Dasa


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();              }          }     }); 
like image 30
Ali009 Avatar answered Oct 24 '22 14:10

Ali009