Skip to end of metadata
Go to start of metadata

This documentation is no longer actively supported and may be out of date. Going forward, please visit and bookmark our new site (https://docs.phunware.com/) for up-to-date documentation.

Using a MapView in a Custom Layout

 

MapView is a subclass of the Google Maps MapView class.  It can be used to place a map in any Android View.

Users of the MapView class must forward all the activity lifecycle methods to the corresponding methods in the MapView class. Examples of these methods include onCreate(), onDestroy(), onResume() and onPause().

 

SimpleMapViewFragment.java

public class SimpleMapViewFragment extends Fragment implements OnPhunwareMapReadyCallback {

    private static final String TAG = SimpleMapViewFragment.class.getSimpleName();

    private PhunwareMapManager mapManager;
    private MapView mapView;

    public static SimpleMapViewFragment newInstance() {
        SimpleMapViewFragment fragment = new SimpleMapViewFragment();

        Bundle args = new Bundle();
        fragment.setArguments(args);

        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mapManager = ((App)getActivity().getApplication()).getMapManager();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_map, container, false);

        mapView = (MapView)view.findViewById(R.id.map_view);
        mapView.onCreate(savedInstanceState);

        mapView.getPhunwareMapAsync(this);

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        mapView.onResume();
    }

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

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

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

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    @Override
    public void onPhunwareMapReady(final PhunwareMap map) {

        mapManager.setPhunwareMap(map);
        mapManager.addBuilding(getResources().getInteger(R.integer.building_id),
                new Callback<Building>() {
                    @Override
                    public void onSuccess(final Building building) {
                        if (building == null) {
                            Toast.makeText(getActivity(), "No building", Toast.LENGTH_LONG)
                                    .show();
                            return;
                        }

                        // animate the camera to the new building
                        final CameraUpdate cameraUpdate;
                        FloorOptions initialFloor = building.initialFloor();
                        if (initialFloor == null) {
                            // if we don't have a floor with reference points, just zoom in on the
                            // building, at a relatively safe distance
                            cameraUpdate = CameraUpdateFactory
                                    .newLatLngZoom(building.getLocation(), 17);
                        } else {
                            building.selectFloor(building.getInitialFloor());
                            cameraUpdate = CameraUpdateFactory
                                    .newLatLngBounds(initialFloor.getBounds(), 4);
                        }
                        map.getGoogleMap().setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                            // Wait to animate the camera until the map is rendered.
                            @Override
                            public void onMapLoaded() {
                                map.getGoogleMap().animateCamera(cameraUpdate);
                            }
                        });

                        // enable my location (blue dot)
                        mapManager.setMyLocationEnabled(true);
                    }

                    @Override
                    public void onFailure(final Throwable e) {
                        getActivity().runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(getActivity(), e.getLocalizedMessage(),
                                        Toast.LENGTH_LONG).show();
                            }
                        });
                    }
                });
    }
}

 

fragment_map.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.phunware.mapping.MapView
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="48dp" />

</android.support.design.widget.CoordinatorLayout>

 

  • No labels