diff options
author | tobrun <tobrun.van.nuland@gmail.com> | 2018-05-17 18:26:28 +0200 |
---|---|---|
committer | Tobrun <tobrun@mapbox.com> | 2018-05-17 19:36:57 +0200 |
commit | 4ceb687f6c7bdb4cd768310eeae558d826dcf506 (patch) | |
tree | c79ef77c30015af2c9d2df10e05d344b5b7cff03 /platform | |
parent | 518b857d64f90fa3e1c7794f4c2b5dea0c811ee8 (diff) | |
download | qtlocation-mapboxgl-4ceb687f6c7bdb4cd768310eeae558d826dcf506.tar.gz |
[android] - expose MapView created callbacks on MapFragment and MapSupportFragment
Diffstat (limited to 'platform')
4 files changed, 160 insertions, 64 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java index 5fcf206a5a..280877d61a 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/MapFragment.java @@ -8,7 +8,6 @@ import android.support.annotation.Nullable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; - import com.mapbox.mapboxsdk.utils.MapFragmentUtils; import java.util.ArrayList; @@ -31,6 +30,7 @@ import java.util.List; public final class MapFragment extends Fragment implements OnMapReadyCallback { private final List<OnMapReadyCallback> mapReadyCallbackList = new ArrayList<>(); + private OnMapViewReadyCallback mapViewReadyCallback; private MapboxMap mapboxMap; private MapView map; @@ -56,6 +56,19 @@ public final class MapFragment extends Fragment implements OnMapReadyCallback { } /** + * Called when the context attaches to this fragment. + * + * @param context the context attaching + */ + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof OnMapViewReadyCallback) { + mapViewReadyCallback = (OnMapViewReadyCallback) context; + } + } + + /** * Creates the fragment view hierarchy. * * @param inflater Inflater used to inflate content. @@ -75,15 +88,25 @@ public final class MapFragment extends Fragment implements OnMapReadyCallback { * Called when the fragment view hierarchy is created. * * @param view The content view of the fragment - * @param savedInstanceState THe saved instance state of the framgnt + * @param savedInstanceState The saved instance state of the fragment */ @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); map.onCreate(savedInstanceState); map.getMapAsync(this); + + // notify listeners about mapview creation + if (mapViewReadyCallback != null) { + mapViewReadyCallback.onMapViewReady(map); + } } + /** + * Called when the style of the map has successfully loaded. + * + * @param mapboxMap The public api controller of the map + */ @Override public void onMapReady(MapboxMap mapboxMap) { this.mapboxMap = mapboxMap; @@ -170,4 +193,21 @@ public final class MapFragment extends Fragment implements OnMapReadyCallback { onMapReadyCallback.onMapReady(mapboxMap); } } + + /** + * Callback to be invoked when the map fragment has inflated its MapView. + * <p> + * To use this interface the context hosting the fragment must implement this interface. + * That instance will be set as part of Fragment#onAttach(Context context). + * </p> + */ + public interface OnMapViewReadyCallback { + + /** + * Called when the map has been created. + * + * @param mapView The created mapview + */ + void onMapViewReady(MapView mapView); + } } diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java index 8aa4c7fd09..307b33b0c7 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/maps/SupportMapFragment.java @@ -31,6 +31,7 @@ import java.util.List; public class SupportMapFragment extends Fragment implements OnMapReadyCallback { private final List<OnMapReadyCallback> mapReadyCallbackList = new ArrayList<>(); + private MapFragment.OnMapViewReadyCallback mapViewReadyCallback; private MapboxMap mapboxMap; private MapView map; @@ -56,6 +57,19 @@ public class SupportMapFragment extends Fragment implements OnMapReadyCallback { } /** + * Called when the context attaches to this fragment. + * + * @param context the context attaching + */ + @Override + public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof MapFragment.OnMapViewReadyCallback) { + mapViewReadyCallback = (MapFragment.OnMapViewReadyCallback) context; + } + } + + /** * Creates the fragment view hierarchy. * * @param inflater Inflater used to inflate content. @@ -82,6 +96,11 @@ public class SupportMapFragment extends Fragment implements OnMapReadyCallback { super.onViewCreated(view, savedInstanceState); map.onCreate(savedInstanceState); map.getMapAsync(this); + + // notify listeners about MapView creation + if (mapViewReadyCallback != null) { + mapViewReadyCallback.onMapViewReady(map); + } } @Override diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/MapFragmentActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/MapFragmentActivity.java index 930626078d..f5e0371aad 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/MapFragmentActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/MapFragmentActivity.java @@ -1,14 +1,13 @@ package com.mapbox.mapboxsdk.testapp.activity.fragment; -import android.app.FragmentTransaction; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; - 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.MapFragment; +import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.MapboxMapOptions; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; @@ -20,53 +19,71 @@ import com.mapbox.mapboxsdk.testapp.R; * Uses MapboxMapOptions to initialise the Fragment. * </p> */ -public class MapFragmentActivity extends AppCompatActivity implements OnMapReadyCallback { +public class MapFragmentActivity extends AppCompatActivity implements MapFragment.OnMapViewReadyCallback, + OnMapReadyCallback, MapView.OnMapChangedListener { private MapboxMap mapboxMap; + private MapView mapView; + private boolean initialCameraAnimation = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map_fragment); - - MapFragment mapFragment; if (savedInstanceState == null) { - final FragmentTransaction transaction = getFragmentManager().beginTransaction(); + MapFragment mapFragment = MapFragment.newInstance(createFragmentOptions()); + getFragmentManager() + .beginTransaction() + .add(R.id.fragment_container,mapFragment, "com.mapbox.map") + .commit(); + mapFragment.getMapAsync(this); + } + } - MapboxMapOptions options = new MapboxMapOptions(); - options.styleUrl(Style.OUTDOORS); + private MapboxMapOptions createFragmentOptions() { + MapboxMapOptions options = new MapboxMapOptions(); + options.styleUrl(Style.OUTDOORS); - options.scrollGesturesEnabled(false); - options.zoomGesturesEnabled(false); - options.tiltGesturesEnabled(false); - options.rotateGesturesEnabled(false); + options.scrollGesturesEnabled(false); + options.zoomGesturesEnabled(false); + options.tiltGesturesEnabled(false); + options.rotateGesturesEnabled(false); + options.debugActive(false); - options.debugActive(false); + LatLng dc = new LatLng(38.90252, -77.02291); - LatLng dc = new LatLng(38.90252, -77.02291); + options.minZoomPreference(9); + options.maxZoomPreference(11); + options.camera(new CameraPosition.Builder() + .target(dc) + .zoom(11) + .build()); + return options; + } - options.minZoomPreference(9); - options.maxZoomPreference(11); - options.camera(new CameraPosition.Builder() - .target(dc) - .zoom(11) - .build()); + @Override + public void onMapViewReady(MapView map) { + mapView = map; + mapView.addOnMapChangedListener(this); + } - mapFragment = MapFragment.newInstance(options); + @Override + public void onMapReady(MapboxMap map) { + mapboxMap = map; + } - transaction.add(R.id.fragment_container, mapFragment, "com.mapbox.map"); - transaction.commit(); - } else { - mapFragment = (MapFragment) getFragmentManager().findFragmentByTag("com.mapbox.map"); + @Override + public void onMapChanged(int change) { + if (initialCameraAnimation && change == MapView.DID_FINISH_RENDERING_MAP_FULLY_RENDERED) { + mapboxMap.animateCamera( + CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().tilt(45.0).build()), 5000); + initialCameraAnimation = false; } - - mapFragment.getMapAsync(this); } @Override - public void onMapReady(MapboxMap map) { - mapboxMap = map; - mapboxMap.animateCamera( - CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().tilt(45.0).build()), 10000); + protected void onDestroy() { + super.onDestroy(); + mapView.removeOnMapChangedListener(this); } } diff --git a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/SupportMapFragmentActivity.java b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/SupportMapFragmentActivity.java index 4f828060ee..f494a9231e 100644 --- a/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/SupportMapFragmentActivity.java +++ b/platform/android/MapboxGLAndroidSDKTestApp/src/main/java/com/mapbox/mapboxsdk/testapp/activity/fragment/SupportMapFragmentActivity.java @@ -1,13 +1,13 @@ package com.mapbox.mapboxsdk.testapp.activity.fragment; import android.os.Bundle; -import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; - 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.MapFragment; +import com.mapbox.mapboxsdk.maps.MapView; import com.mapbox.mapboxsdk.maps.MapboxMap; import com.mapbox.mapboxsdk.maps.MapboxMapOptions; import com.mapbox.mapboxsdk.maps.OnMapReadyCallback; @@ -20,51 +20,71 @@ import com.mapbox.mapboxsdk.testapp.R; * Uses MapboxMapOptions to initialise the Fragment. * </p> */ -public class SupportMapFragmentActivity extends AppCompatActivity implements OnMapReadyCallback { +public class SupportMapFragmentActivity extends AppCompatActivity implements MapFragment.OnMapViewReadyCallback, + OnMapReadyCallback, MapView.OnMapChangedListener { private MapboxMap mapboxMap; + private MapView mapView; + private boolean initialCameraAnimation = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_map_fragment); - - SupportMapFragment mapFragment; if (savedInstanceState == null) { - final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); - - MapboxMapOptions options = new MapboxMapOptions(); - options.styleUrl(Style.SATELLITE_STREETS); - - options.debugActive(false); - options.compassEnabled(false); - options.attributionEnabled(false); - options.logoEnabled(false); + SupportMapFragment mapFragment = SupportMapFragment.newInstance(createFragmentOptions()); + getSupportFragmentManager() + .beginTransaction() + .add(R.id.fragment_container, mapFragment, "com.mapbox.map") + .commit(); + mapFragment.getMapAsync(this); + } + } - LatLng dc = new LatLng(38.90252, -77.02291); + private MapboxMapOptions createFragmentOptions() { + MapboxMapOptions options = new MapboxMapOptions(); + options.styleUrl(Style.MAPBOX_STREETS); - options.minZoomPreference(9); - options.maxZoomPreference(11); - options.camera(new CameraPosition.Builder() - .target(dc) - .zoom(11) - .build()); + options.scrollGesturesEnabled(false); + options.zoomGesturesEnabled(false); + options.tiltGesturesEnabled(false); + options.rotateGesturesEnabled(false); + options.debugActive(false); - mapFragment = SupportMapFragment.newInstance(options); + LatLng dc = new LatLng(38.90252, -77.02291); - transaction.add(R.id.fragment_container, mapFragment, "com.mapbox.map"); - transaction.commit(); - } else { - mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentByTag("com.mapbox.map"); - } + options.minZoomPreference(9); + options.maxZoomPreference(11); + options.camera(new CameraPosition.Builder() + .target(dc) + .zoom(11) + .build()); + return options; + } - mapFragment.getMapAsync(this); + @Override + public void onMapViewReady(MapView map) { + mapView = map; + mapView.addOnMapChangedListener(this); } @Override public void onMapReady(MapboxMap map) { mapboxMap = map; - mapboxMap.animateCamera( - CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().tilt(45.0).build()), 10000); } -} + + @Override + public void onMapChanged(int change) { + if (initialCameraAnimation && change == MapView.DID_FINISH_RENDERING_MAP_FULLY_RENDERED) { + mapboxMap.animateCamera( + CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder().tilt(45.0).build()), 5000); + initialCameraAnimation = false; + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + mapView.removeOnMapChangedListener(this); + } +}
\ No newline at end of file |