0
votes

I have the following code:

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {

    ViewGroup view = (ViewGroup) inflater.inflate(R.layout.fragment_left, null);

    // test

    SupportMapFragment fragment = new SupportMapFragment();
    FragmentManager fm = getActivity().getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.map, fragment).commit();

    GoogleMap googleMap = fragment.getMap();

    if(getCameraPosition() == null) { // first time in app
        setCameraPosition(new CameraPosition(getYourLatLng(), 12, 0 ,0));
    }
    googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(getCameraPosition()));       

    googleMap.addMarker(new MarkerOptions()
    .title("Your Position")
    .snippet("This is where your smartphone is located.")
    .position(getYourLatLng()));

    return view;
}

This is what I'm trying to replace in the xml file:

     <FrameLayout
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

Whats up with this? Anyway to update the id?

NEW Stack Trace:

10-29 22:37:52.277: E/AndroidRuntime(16273): FATAL EXCEPTION: main
10-29 22:37:52.277: E/AndroidRuntime(16273): java.lang.NullPointerException: CameraUpdateFactory is not initialized
10-29 22:37:52.277: E/AndroidRuntime(16273):    at com.google.android.gms.internal.s.b(Unknown Source)
10-29 22:37:52.277: E/AndroidRuntime(16273):    at com.google.android.gms.maps.CameraUpdateFactory.aX(Unknown Source)
1
Why do you need to use findFragmentById(....)? Surely you can just call fragment.getMap() directly? - Squonk
@Squonk Thanks! That did the trick (still wondering why it won't work though). However now I've bumped into another problem: 10-29 22:37:52.277: E/AndroidRuntime(16273): java.lang.NullPointerException: CameraUpdateFactory is not initialized - I'll update my code here in SO. - basickarl
You can't do camera operations pre-layout. Try moving the code to move the camera into your onActivityCreate (or any other lifecycle call after the view has been created) - dymmeh

1 Answers

0
votes

You need to set a MapReadyCallback.

https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment

When the map is ready, you can move the Camera. (There is a function providing a GoogleMap that allow you to move camera.)

https://developers.google.com/android/reference/com/google/android/gms/maps/OnMapReadyCallback.html