Reputation: 2653
I am trying to move the My Location button in the Google Maps v2, using the solution as suggested here.
The problem I'm having is that I am getting a nullpointerexception, and it turns out that this.getView()
returns null, which is odd, as by the time that line is reached, the fragment has been created. The onCreateView() routine has finished, some inits have been done to the map, so I'm sure it exists. The rest of the app can access this map object this fine, as well.
The relevant part of my main.xml file (there is a lot more in this RelativeLayout of course, but this is the only map fragment present):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.squirrel.hkairpollution.MySupportMapFragment"
/>
And the code:
package com.squirrel.hkairpollution;
(imports omitted)
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();
Log.v(TAG, "Moving the MyPositionButton");
resetMyPositionButton();
return v;
}
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 resetMyPositionButton()
{
//deep paths for map controls
ViewGroup v1 = (ViewGroup)this.getView();
ViewGroup v2 = (ViewGroup)v1.getChildAt(0);
ViewGroup v3 = (ViewGroup)v2.getChildAt(0);
ViewGroup v4 = (ViewGroup)v3.getChildAt(1);
//my position button
View position = (View)v4.getChildAt(0);
int positionWidth = position.getLayoutParams().width;
int positionHeight = position.getLayoutParams().height;
//lay out 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_BOTTOM, RelativeLayout.TRUE);
position.setLayoutParams(positionParams);
}
}
Upvotes: 0
Views: 2754
Reputation: 2653
That was a quick solution, great. Here the complete and working 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.GoogleMap;
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);
resetMyPositionButton();
}
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 resetMyPositionButton()
{
//deep paths for map controls
ViewGroup v1 = (ViewGroup)this.getView();
ViewGroup v2 = (ViewGroup)v1.getChildAt(0);
ViewGroup v3 = (ViewGroup)v2.getChildAt(0);
ViewGroup v4 = (ViewGroup)v3.getChildAt(1);
//my position button
View position = (View)v4.getChildAt(0);
int positionWidth = position.getLayoutParams().width;
int positionHeight = position.getLayoutParams().height;
//lay out 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_BOTTOM, RelativeLayout.TRUE);
position.setLayoutParams(positionParams);
}
}
Upvotes: 1