Skip to main content

How to change position of MyLocationButton on new Map API v2? Explained

When an app use map and design some layout on this map (TextView, Button or image), developer needs to change position of default buttons of map view. (MyLocationButton, ZoomControl or compass...) As far as i know, there are two possible way for changing position of myLocationButton's position.

1- * - Disable it
    * - Create new button programmatically
    * - Add animateCamera(toCurrentLocation) to onClickListener of this button
    * - And place it wherever you want

But if you don't want to write all that there is a hack for that.

2 - * - findViewById() of hardcoded button with;
ZoomControl id = 0x1
MyLocation button id = 0x2
     * - And do whatever you want:
// Find map fragment
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

// Find myLocationButton view
View myLocationButton = mapFragment.getView().findViewById(0x2);

if (myLocationButton != null && myLocationButton.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
   // ZoomControl is inside of RelativeLayout
   RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) myLocationButton.getLayoutParams();
   // Align it to - parent BOTTOM|LEFT
   params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
   params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
 
   params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
   params.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);

   // Update margins, set to 10dp
   final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
           getResources().getDisplayMetrics());
   params.setMargins(margin, margin, margin, margin);
 
   myLocationButton.setLayoutParams(params);
}
But this is not a good solution but it works for me. On the otherhand it can be change every new build of google play service. Do a lot of test before implement this.
I hope this works for you.

Here some more discussion on that topic...
https://code.google.com/p/gmaps-api-issues/issues/detail?id=4670

http://stackoverflow.com/questions/14489880/how-to-change-the-position-of-maps-apis-get-my-location-button

http://stackoverflow.com/questions/14299435/mylocation-button-hidden-behind-transparent-action-bar-how-to-reposition/15740101#15740101

http://stackoverflow.com/questions/14071230/android-maps-library-v2-zoom-controls-custom-position

Comments

Popular posts from this blog

How to change position of MyLocationButton on new Map API v2 - 2

In my previuos post[1], i mention a workaround for changing buttons on Google Map which we use with google services. It is good to know they listen developers and make their products more development friendly. (I hope it goes like that way all the time and all the product lines of Google) Now we have a method for setting padding of map. It is add padding to useful area of map. We can see full screen map but with padding we can use prefered area of it. It is good for overlay action bar and other things on map view. [ 2] They write a blog and take a video about it. Read blog and watch video if you are using google map in your app :) [3] And finally, They add this method because of this issue. That means, if you find a bug or want extra feature from and api, write to google via this issue tracking system. [1] http://blog.kozaxinan.com/2013/08/how-to-change-position-of.html [2] https://developers.google.com/maps/documentation/android/map#map_padding [3] http://googlegeodeve

How to set alarm to show a notification at future date with AlarmManager?

Lets assume you want to do a job a future time or show a notification at a selected time. AlarmManager can help to send an intent in future time. First we need to select date, Lets create a date in 2050, Calendar calendar = Calendar. getInstance () ; calendar.set(Calendar. YEAR , 2050 ) ; Date date = calendar.getTime() ; For showing notification we need a service. public class AlarmReceiver extends Service { public AlarmReceiver() { } @Override public IBinder onBind (Intent intent) { } } Add service to Manifest.xml <service android :name= "com.kozaxinan.example.alarm.AlarmReceiver" android :enabled= "true" android :exported= "true" > </service> We will modify AlarmReceiver for sending notification. But first we need to register a alarm. AlarmManager alarmManager = (AlarmManager) getSystemService( ALARM_SERVICE ) ; Intent intent = new Intent( this, AlarmReceiver. class ) ; intent.putExtra( &q

Meteor Crush

You are the last hope of world Hit meteors and save everyone Accelerate satellites and crush meteors.  Clear endless danger waves. Tear meteors to pieces so planet and all living things can live. Smash meteorites and help world. Meteor Crush https://play.google.com/store/apps/details?id=com.kozaxinan.meteorcrush