Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Expose MapView created callbacks on MapFragment #11934

Merged
merged 1 commit into from
May 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mapbox.mapboxsdk.utils.MapFragmentUtils;

import java.util.ArrayList;
Expand All @@ -31,6 +30,7 @@
public final class MapFragment extends Fragment implements OnMapReadyCallback {

private final List<OnMapReadyCallback> mapReadyCallbackList = new ArrayList<>();
private OnMapViewReadyCallback mapViewReadyCallback;
private MapboxMap mapboxMap;
private MapView map;

Expand All @@ -55,6 +55,19 @@ public static MapFragment newInstance(@Nullable MapboxMapOptions mapboxMapOption
return mapFragment;
}

/**
* 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.
*
Expand All @@ -75,15 +88,25 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
* 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;
Expand Down Expand Up @@ -170,4 +193,21 @@ public void getMapAsync(@NonNull final OnMapReadyCallback 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
public class SupportMapFragment extends Fragment implements OnMapReadyCallback {

private final List<OnMapReadyCallback> mapReadyCallbackList = new ArrayList<>();
private MapFragment.OnMapViewReadyCallback mapViewReadyCallback;
private MapboxMap mapboxMap;
private MapView map;

Expand All @@ -55,6 +56,19 @@ public static SupportMapFragment newInstance(@Nullable MapboxMapOptions mapboxMa
return mapFragment;
}

/**
* 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.
*
Expand Down Expand Up @@ -82,6 +96,11 @@ 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);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,53 +19,71 @@
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,51 +20,71 @@
* 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);
}
}