It was a bit of a struggle to get this working. But I got it done, and in the process also started to move the zoom buttons around. Here my complete code:
package com.squirrel.hkairpollution;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.UiSettings;
import com.google.android.gms.maps.model.LatLng;
public class MySupportMapFragment extends SupportMapFragment {
private static final String TAG = HKAirPollution.TAG;
public MySupportMapFragment() {
return;
}
@Override
public View onCreateView(LayoutInflater arg0, ViewGroup arg1, Bundle arg2) {
Log.v(TAG, "In overridden onCreateView.");
View v = super.onCreateView(arg0, arg1, arg2);
Log.v(TAG, "Initialising map.");
initMap();
return v;
}
@Override
public void onViewCreated (View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
resetButtons();
}
private void initMap(){
UiSettings settings = getMap().getUiSettings();
settings.setAllGesturesEnabled(true);
settings.setMyLocationButtonEnabled(true);
LatLng latLong = new LatLng(22.320542, 114.185715);
getMap().moveCamera(CameraUpdateFactory.newLatLngZoom(latLong,11));
}
/**
* Move my position button at the bottom of map
*/
private void resetButtons()
{
// Get a reference to the zoom buttons and the position button.
ViewGroup v1 = (ViewGroup)this.getView();
ViewGroup v2 = (ViewGroup)v1.getChildAt(0);
ViewGroup v3 = (ViewGroup)v2.getChildAt(0);
ViewGroup v4 = (ViewGroup)v3.getChildAt(1);
// The My Position button
View position = (View)v4.getChildAt(0);
int positionWidth = position.getLayoutParams().width;
int positionHeight = position.getLayoutParams().height;
// Lay out the My Position button.
RelativeLayout.LayoutParams positionParams = new RelativeLayout.LayoutParams(positionWidth,positionHeight);
int margin = positionWidth/5;
positionParams.setMargins(0, 0, 0, margin);
positionParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
positionParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
position.setLayoutParams(positionParams);
// The Zoom buttons
View zoom = (View)v4.getChildAt(2);
int zoomWidth = zoom.getLayoutParams().width;
int zoomHeight = zoom.getLayoutParams().height;
// Lay out the Zoom buttons.
RelativeLayout.LayoutParams zoomParams = new RelativeLayout.LayoutParams(zoomWidth, zoomHeight);
zoomParams.setMargins(0, 0, 0, margin);
zoomParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
zoomParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
zoom.setLayoutParams(zoomParams);
}
}