Zach
Zach

Reputation: 10139

MapFragment inside PagerAdapter

I am trying to load a Mapfragment inside a PagerAdapter. Here is teh adapter code,

public class ShopPagerAdapter extends PagerAdapter {

    private List<LoyaltyOutlet> data;
    private Context context;
    FragmentManager fragmentManager;

    public ShopPagerAdapter(List<LoyaltyOutlet> data, Context context,FragmentManager manager) {
        this.data = data;
        this.context = context;
        this.fragmentManager = manager;
    }

    @Override
    public Object instantiateItem(ViewGroup collection, int position) {
        LoyaltyOutlet outlet = data.get(position);
        LayoutInflater inflater = LayoutInflater.from(context);
        ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.fragment_loyalty_shop_item, collection, false);
        TextView address = (TextView)layout.findViewById(R.id.txt_ShopAddress);
        GoogleMap googleMap = ((SupportMapFragment) fragmentManager.findFragmentById(R.id.shopMap)).getMap();
        CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(43.1, -87.9), 10);
        googleMap.animateCamera(cameraUpdate);
        address.setText(outlet.getOutletName());

        collection.addView(layout);
        return layout;

    }

    @Override
    public void destroyItem(ViewGroup collection, int position, Object view) {
        collection.removeView((View) view);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "";
    }

}

But when I run code I am getting an exception,

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference

Is it possible to use the MapFragment here or should I move this code inside the fragment lifecycle methods?

Thanks.

Upvotes: 1

Views: 790

Answers (1)

Vivek Mishra
Vivek Mishra

Reputation: 5705

This is how I implemented it using MapView. Hope this will be helpful

Xml for fragment

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.google.android.gms.maps.MapView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Java Code

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view=inflater.inflate(R.layout.fragment_lugar,container,false);
    latitude = 26.78;
    longitude = 72.56;
    mMapView = (MapView) view.findViewById(R.id.mapView);
    mMapView.onCreate(savedInstanceState);

    mMapView.onResume();// needed to get the map to display immediately

    try {
        MapsInitializer.initialize(getActivity().getApplicationContext());
    } catch (Exception e) {
        e.printStackTrace();
    }

    googleMap = mMapView.getMap();
    // latitude and longitude
    double latitude = 17.385044;
    double longitude = 78.486671;

    // create marker
    MarkerOptions marker = new MarkerOptions().position(
            new LatLng(latitude, longitude)).title("Hello Maps");

    // Changing marker icon
    marker.icon(BitmapDescriptorFactory
            .defaultMarker(BitmapDescriptorFactory.HUE_ROSE));

    // adding marker
    googleMap.addMarker(marker);
    CameraPosition cameraPosition = new CameraPosition.Builder()
            .target(new LatLng(17.385044, 78.486671)).zoom(12).build();
    googleMap.animateCamera(CameraUpdateFactory
            .newCameraPosition(cameraPosition));
    return view;
}
/***** Sets up the map if it is possible to do so *****/
@Override
public void onResume() {
    super.onResume();
    mMapView.onResume();
}

@Override
public void onPause() {
    super.onPause();
    mMapView.onPause();
}

@Override
public void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();
}

@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();
}

Upvotes: 1

Related Questions