[android] #3759 - Add SupportMapFragment, test app integration, cleanup old fragment implementation

This commit is contained in:
Tobrun
2016-02-01 16:45:41 +01:00
parent 7694eebe88
commit a699690c16
9 changed files with 201 additions and 27 deletions

View File

@@ -52,6 +52,11 @@ public class MapboxConstants {
*/
public static final double MINIMUM_DIRECTION = 0;
/**
* Fragment Argument Key for MapboxMapOptions
*/
public static final String FRAG_ARG_MAPBOXMAPOPTIONS = "MapboxMapOptions";
// Save instance state keys
public static final String STATE_CAMERA_POSITION = "cameraPosition";
public static final String STATE_ZOOM_ENABLED = "zoomEnabled";

View File

@@ -1,16 +1,15 @@
package com.mapbox.mapboxsdk.maps;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.utils.ApiAccess;
import com.mapbox.mapboxsdk.maps.MapView;
/**
* Fragment wrapper around a {@link MapView}.
@@ -28,21 +27,6 @@ import com.mapbox.mapboxsdk.maps.MapView;
*/
public class MapFragment extends Fragment {
//
// Static members
//
// Tag used for logging
private static final String TAG = "MapFragment";
// Argument used for configuration
private static final String ARGS_MAPBOXMAP_OPTIONS = "MapboxMapOptions";
//
// Instance members
//
// The map
private MapView mMap;
private OnMapReadyCallback mMapReadyCallback;
@@ -53,7 +37,7 @@ public class MapFragment extends Fragment {
public static MapFragment newInstance(MapboxMapOptions mapboxMapOptions) {
final MapFragment mapFragment = new MapFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(ARGS_MAPBOXMAP_OPTIONS, mapboxMapOptions);
bundle.putParcelable(MapboxConstants.FRAG_ARG_MAPBOXMAPOPTIONS, mapboxMapOptions);
mapFragment.setArguments(bundle);
return mapFragment;
}

View File

@@ -0,0 +1,104 @@
package com.mapbox.mapboxsdk.maps;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.constants.MapboxConstants;
import com.mapbox.mapboxsdk.utils.ApiAccess;
/**
* Fragment wrapper around a {@link MapView}.
* <p>
* A Map component in an app. This fragment is the simplest way to place a map in an application.
* It's a wrapper around a view of a map to automatically handle the necessary life cycle needs.
* Being a fragment, this component can be added to an activity's layout or can dynamically be added
* using a FragmentManager.
* </p>
* <p>
* To get a reference to the MapView, use {@link #getMapAsync(OnMapReadyCallback)}}
* </p>
*
* @see #getMapAsync(OnMapReadyCallback)
*/
public class SupportMapFragment extends Fragment {
private MapView mMap;
private OnMapReadyCallback mMapReadyCallback;
public static SupportMapFragment newInstance(){
return new SupportMapFragment();
}
public static SupportMapFragment newInstance(MapboxMapOptions mapboxMapOptions) {
final SupportMapFragment mapFragment = new SupportMapFragment();
Bundle bundle = new Bundle();
bundle.putParcelable(MapboxConstants.FRAG_ARG_MAPBOXMAPOPTIONS, mapboxMapOptions);
mapFragment.setArguments(bundle);
return mapFragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
mMap = (MapView) inflater.inflate(R.layout.fragment_mapview, container, false);
mMap.setAccessToken(ApiAccess.getToken(inflater.getContext()));
mMap.onCreate(savedInstanceState);
return mMap;
}
@Override
public void onStart() {
super.onStart();
mMap.onStart();
}
@Override
public void onResume() {
super.onResume();
mMap.onResume();
if(mMapReadyCallback!=null){
mMapReadyCallback.onMapReady(mMap.getMapboxMap());
}
}
@Override
public void onPause() {
super.onPause();
mMap.onPause();
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
mMap.onSaveInstanceState(outState);
super.onSaveInstanceState(outState);
}
@Override
public void onStop() {
super.onStop();
mMap.onStop();
}
@Override
public void onLowMemory() {
mMap.onLowMemory();
super.onLowMemory();
}
@Override
public void onDestroyView() {
super.onDestroyView();
mMap.onDestroy();
mMap = null;
}
@NonNull
public void getMapAsync(@NonNull OnMapReadyCallback onMapReadyCallback){
mMapReadyCallback = onMapReadyCallback;
}
}

View File

@@ -50,6 +50,9 @@
<activity
android:name=".MapFragmentActivity"
android:label="@string/activity_map_fragment" />
<activity
android:name=".SupportMapFragmentActivity"
android:label="@string/activity_map_fragment_suport" />
<activity
android:name=".PressForMarkerActivity"
android:label="@string/activity_press_for_marker" />

View File

@@ -38,6 +38,7 @@ import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.layers.CustomLayer;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.SupportMapFragment;
import com.mapbox.mapboxsdk.maps.UiSettings;
import com.mapbox.mapboxsdk.testapp.layers.ExampleCustomLayer;
import com.mapbox.mapboxsdk.testapp.utils.GeoParseUtil;
@@ -225,10 +226,6 @@ public class MainActivity extends AppCompatActivity {
});
}
/**
* Dispatch onStart() to all fragments. Ensure any created loaders are
* now started.
*/
@Override
protected void onStart() {
super.onStart();
@@ -373,6 +370,10 @@ public class MainActivity extends AppCompatActivity {
startActivity(new Intent(getApplicationContext(), MapFragmentActivity.class));
return true;
case R.id.action_support_map_fragment:
startActivity(new Intent(getApplicationContext(), SupportMapFragmentActivity.class));
return true;
case R.id.action_press_for_marker:
startActivity(new Intent(getApplicationContext(), PressForMarkerActivity.class));
return true;

View File

@@ -1,8 +1,8 @@
package com.mapbox.mapboxsdk.testapp;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
@@ -34,11 +34,11 @@ public class MapFragmentActivity extends AppCompatActivity {
MapFragment mapFragment;
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, mapFragment = new MapFragment(), "com.mapbox.map");
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, mapFragment = MapFragment.newInstance(), "com.mapbox.map");
transaction.commit();
} else {
mapFragment = (MapFragment) getSupportFragmentManager().findFragmentByTag("com.mapbox.map");
mapFragment = (MapFragment) getFragmentManager().findFragmentByTag("com.mapbox.map");
}
mapFragment.getMapAsync(new OnMapReadyCallback() {

View File

@@ -0,0 +1,69 @@
package com.mapbox.mapboxsdk.testapp;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.constants.Style;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.maps.SupportMapFragment;
public class SupportMapFragmentActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map_fragment);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}
SupportMapFragment mapFragment;
if (savedInstanceState == null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.fragment_container, mapFragment = SupportMapFragment.newInstance(), "com.mapbox.map");
transaction.commit();
} else {
mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag("com.mapbox.map");
}
mapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull MapboxMap mapboxMap) {
mapboxMap.setStyleUrl(Style.SATELLITE_STREETS);
mapboxMap.moveCamera(CameraUpdateFactory.newCameraPosition(
new CameraPosition.Builder()
.target(new LatLng(48.861431, 2.334166))
.zoom(10)
.bearing(0)
.tilt(0)
.build()));
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}

View File

@@ -95,6 +95,12 @@
android:icon="@drawable/ic_now_widgets_24dp"
android:title="@string/action_map_fragment" />
<item
android:id="@+id/action_support_map_fragment"
android:checkable="false"
android:icon="@drawable/ic_now_widgets_24dp"
android:title="@string/action_map_fragment_support" />
<item
android:id="@+id/action_info_window"
android:checkable="false"

View File

@@ -7,6 +7,7 @@
<string name="activity_mapboxmap">MapboxMap</string>
<string name="activity_tilt">Map Tilt</string>
<string name="activity_infowindow_adapter">InfoWindow Adapter</string>
<string name="activity_map_fragment_suport">Support Map Fragment Activity</string>
<string name="activity_map_fragment">Map Fragment Activity</string>
<string name="activity_press_for_marker">Press For Marker Activity</string>
<string name="activity_marker_in_bulk">Add Bulk Markers Activity</string>
@@ -37,6 +38,7 @@
<string name="action_info_window_adapter">InfoWindow Adapter</string>
<string name="action_tilt">Tilt</string>
<string name="action_map_fragment">MapFragment</string>
<string name="action_map_fragment_support">SupportMapFragment</string>
<string name="action_press_for_marker">Press For Marker</string>
<string name="action_manual_zoom">Manual Zoom</string>
<string name="action_minmax_zoom">Min/Max Zoom</string>