Skip to content

Commit

Permalink
Merge branch 'master' into av-web
Browse files Browse the repository at this point in the history
* master:
  Support setting map's content insets (flutter-mapbox-gl#215)
  Provide `onMapIdle` callback (flutter-mapbox-gl#214)
  [example] add full page map example (flutter-mapbox-gl#201)
  Fix missing location indicator on iOS (flutter-mapbox-gl#176)
  Returned a nil result so the future completes. (flutter-mapbox-gl#216)
  Update README: Access Tokens for self-hosted tiles (flutter-mapbox-gl#217)
  Change annotation priority (flutter-mapbox-gl#222)
  • Loading branch information
andrea689 committed Mar 13, 2020
2 parents ac3b48f + 16439ed commit 8daf3df
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ We're compiling a list of apps using this SDK. If you want to be listed here, pl
#### Mapbox Access Token

This project uses Mapbox vector tiles, which requires a Mapbox account and a Mapbox access token. Obtain a free access token on [your Mapbox account page](https://www.mapbox.com/account/access-tokens/).
> **Even if you do not use Mapbox vector tiles but vector tiles from a different source (like self-hosted tiles) with this plugin, you will need to specify any non-empty string as Access Token as explained below!**
##### Android

Expand Down
40 changes: 40 additions & 0 deletions example/lib/full_map.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'package:flutter/material.dart';
import 'package:mapbox_gl/mapbox_gl.dart';

import 'page.dart';

class FullMapPage extends Page {
FullMapPage()
: super(const Icon(Icons.map), 'Full screen map');

@override
Widget build(BuildContext context) {
return const FullMap();
}
}

class FullMap extends StatefulWidget {
const FullMap();

@override
State createState() => FullMapState();
}

class FullMapState extends State<FullMap> {
MapboxMapController mapController;

void _onMapCreated(MapboxMapController controller) {
mapController = controller;
}

@override
Widget build(BuildContext context) {
return new Scaffold(
body: MapboxMap(
onMapCreated: _onMapCreated,
initialCameraPosition:
const CameraPosition(target: LatLng(0.0, 0.0)),
)
);
}
}
3 changes: 3 additions & 0 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:location/location.dart';
import 'package:mapbox_gl_example/full_map.dart';

import 'animate_camera.dart';
import 'full_map.dart';
import 'line.dart';
import 'map_ui.dart';
import 'move_camera.dart';
Expand All @@ -17,6 +19,7 @@ import 'scrolling_map.dart';

final List<Page> _allPages = <Page>[
MapUiPage(),
FullMapPage(),
AnimateCameraPage(),
MoveCameraPage(),
PlaceSymbolPage(),
Expand Down
34 changes: 29 additions & 5 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,21 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
style.localizeLabels(into: nil)
}
result(nil)
case "map#updateContentInsets":
guard let arguments = methodCall.arguments as? [String: Any] else { return }

if let bounds = arguments["bounds"] as? [String: Any],
let top = bounds["top"] as? CGFloat,
let left = bounds["left"] as? CGFloat,
let bottom = bounds["bottom"] as? CGFloat,
let right = bounds["right"] as? CGFloat,
let animated = arguments["animated"] as? Bool {
mapView.setContentInset(UIEdgeInsets(top: top, left: left, bottom: bottom, right: right), animated: animated) {
result(nil)
}
} else {
result(nil)
}
case "map#setMapLanguage":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
if let localIdentifier = arguments["language"] as? String, let style = mapView.style {
Expand Down Expand Up @@ -117,12 +132,14 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
if let camera = Convert.parseCameraUpdate(cameraUpdate: cameraUpdate, mapView: mapView) {
mapView.setCamera(camera, animated: false)
}
result(nil)
case "camera#animate":
guard let arguments = methodCall.arguments as? [String: Any] else { return }
guard let cameraUpdate = arguments["cameraUpdate"] as? [Any] else { return }
if let camera = Convert.parseCameraUpdate(cameraUpdate: cameraUpdate, mapView: mapView) {
mapView.setCamera(camera, animated: true)
}
result(nil)
case "symbol#add":
guard let symbolAnnotationController = symbolAnnotationController else { return }
guard let arguments = methodCall.arguments as? [String: Any] else { return }
Expand Down Expand Up @@ -323,7 +340,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
// This is required in order to hide the default Maps SDK pin
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
if annotation is MGLUserLocation {
return MGLUserLocationAnnotationView()
return nil
}
return MGLAnnotationView(frame: CGRect(x: 0, y: 0, width: 10, height: 10))
}
Expand All @@ -340,17 +357,18 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
camera.pitch = initialTilt
mapView.setCamera(camera, animated: false)
}

lineAnnotationController = MGLLineAnnotationController(mapView: self.mapView)
lineAnnotationController!.annotationsInteractionEnabled = true
lineAnnotationController?.delegate = self

symbolAnnotationController = MGLSymbolAnnotationController(mapView: self.mapView)
symbolAnnotationController!.annotationsInteractionEnabled = true
symbolAnnotationController?.delegate = self

circleAnnotationController = MGLCircleAnnotationController(mapView: self.mapView)
circleAnnotationController!.annotationsInteractionEnabled = true
circleAnnotationController?.delegate = self

lineAnnotationController = MGLLineAnnotationController(mapView: self.mapView)
lineAnnotationController!.annotationsInteractionEnabled = true
lineAnnotationController?.delegate = self

mapReadyResult?(nil)
if let channel = channel {
Expand Down Expand Up @@ -427,6 +445,12 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
}
}

func mapViewDidBecomeIdle(_ mapView: MGLMapView) {
if let channel = channel {
channel.invokeMethod("map#onIdle", arguments: []);
}
}

func mapView(_ mapView: MGLMapView, regionWillChangeAnimated animated: Bool) {
if let channel = channel {
channel.invokeMethod("camera#onMoveStarted", arguments: []);
Expand Down
35 changes: 32 additions & 3 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ typedef void OnStyleLoadedCallback();
typedef void OnCameraTrackingDismissedCallback();
typedef void OnCameraTrackingChangedCallback(MyLocationTrackingMode mode);

typedef void OnMapIdleCallback();

/// Controller for a single MapboxMap instance running on the host platform.
///
/// Change listeners are notified upon changes to any of
Expand All @@ -31,7 +33,8 @@ class MapboxMapController extends ChangeNotifier {
{this.onStyleLoadedCallback,
this.onMapClick,
this.onCameraTrackingDismissed,
this.onCameraTrackingChanged})
this.onCameraTrackingChanged,
this.onMapIdle})
: assert(_id != null) {
_cameraPosition = initialCameraPosition;

Expand Down Expand Up @@ -101,21 +104,29 @@ class MapboxMapController extends ChangeNotifier {
onCameraTrackingDismissed();
}
});

MapboxGlPlatform.instance.onMapIdlePlatform.add((_) {
if (onMapIdle != null) {
onMapIdle();
}
});
}

static Future<MapboxMapController> init(
int id, CameraPosition initialCameraPosition,
{OnStyleLoadedCallback onStyleLoadedCallback,
OnMapClickCallback onMapClick,
OnCameraTrackingDismissedCallback onCameraTrackingDismissed,
OnCameraTrackingChangedCallback onCameraTrackingChanged}) async {
OnCameraTrackingChangedCallback onCameraTrackingChanged,
OnMapIdleCallback onMapIdle}) async {
assert(id != null);
await MapboxGlPlatform.instance.initPlatform(id);
return MapboxMapController._(id, initialCameraPosition,
onStyleLoadedCallback: onStyleLoadedCallback,
onMapClick: onMapClick,
onCameraTrackingDismissed: onCameraTrackingDismissed,
onCameraTrackingChanged: onCameraTrackingChanged);
onCameraTrackingChanged: onCameraTrackingChanged,
onMapIdle: onMapIdle);
}

final OnStyleLoadedCallback onStyleLoadedCallback;
Expand All @@ -125,6 +136,8 @@ class MapboxMapController extends ChangeNotifier {
final OnCameraTrackingDismissedCallback onCameraTrackingDismissed;
final OnCameraTrackingChangedCallback onCameraTrackingChanged;

final OnMapIdleCallback onMapIdle;

/// Callbacks to receive tap events for symbols placed on this map.
final ArgumentCallbacks<Symbol> onSymbolTapped = ArgumentCallbacks<Symbol>();

Expand Down Expand Up @@ -228,6 +241,22 @@ class MapboxMapController extends ChangeNotifier {
return MapboxGlPlatform.instance.matchMapLanguageWithDeviceDefault();
}

/// Updates the distance from the edges of the map view’s frame to the edges
/// of the map view’s logical viewport, optionally animating the change.
///
/// When the value of this property is equal to `EdgeInsets.zero`, viewport
/// properties such as centerCoordinate assume a viewport that matches the map
/// view’s frame. Otherwise, those properties are inset, excluding part of the
/// frame from the viewport. For instance, if the only the top edge is inset,
/// the map center is effectively shifted downward.
///
/// The returned [Future] completes after the change has been made on the
/// platform side.
Future<void> updateContentInsets(EdgeInsets insets,
[bool animated = false]) async {
return MapboxGlPlatform.instance.updateContentInsets(insets, animated);
}

/// Updates the language of the map labels to match the specified language.
/// Supported language strings are available here: https://github.com/mapbox/mapbox-plugins-android/blob/e29c18d25098eb023a831796ff807e30d8207c36/plugin-localization/src/main/java/com/mapbox/mapboxsdk/plugins/localization/MapLocale.java#L39-L87
///
Expand Down
12 changes: 11 additions & 1 deletion lib/src/mapbox_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class MapboxMap extends StatefulWidget {
this.onMapClick,
this.onCameraTrackingDismissed,
this.onCameraTrackingChanged,
this.onMapIdle,
}) : assert(initialCameraPosition != null);

final MapCreatedCallback onMapCreated;
Expand Down Expand Up @@ -130,6 +131,14 @@ class MapboxMap extends StatefulWidget {
final OnCameraTrackingDismissedCallback onCameraTrackingDismissed;
final OnCameraTrackingChangedCallback onCameraTrackingChanged;

/// Called when map view is entering an idle state, and no more drawing will
/// be necessary until new data is loaded or there is some interaction with
/// the map.
/// * No camera transitions are in progress
/// * All currently requested tiles have loaded
/// * All fade/transition animations have completed
final OnMapIdleCallback onMapIdle;

@override
State createState() => _MapboxMapState();
}
Expand Down Expand Up @@ -180,7 +189,8 @@ class _MapboxMapState extends State<MapboxMap> {
onStyleLoadedCallback: widget.onStyleLoadedCallback,
onMapClick: widget.onMapClick,
onCameraTrackingDismissed: widget.onCameraTrackingDismissed,
onCameraTrackingChanged: widget.onCameraTrackingChanged);
onCameraTrackingChanged: widget.onCameraTrackingChanged,
onMapIdle: widget.onMapIdle);
_controller.complete(controller);
if (widget.onMapCreated != null) {
widget.onMapCreated(controller);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ abstract class MapboxGlPlatform {
final ArgumentCallbacks<void> onCameraTrackingDismissedPlatform =
ArgumentCallbacks<void>();

final ArgumentCallbacks<void> onMapIdlePlatform = ArgumentCallbacks<void>();

/// The default instance of [MapboxGlPlatform] to use.
///
/// Defaults to [MethodChannelMapboxGl].
Expand Down Expand Up @@ -85,6 +87,10 @@ abstract class MapboxGlPlatform {
'matchMapLanguageWithDeviceDefault() has not been implemented.');
}

Future<void> updateContentInsets(EdgeInsets insets, bool animated) async {
throw UnimplementedError('updateContentInsets() has not been implemented.');
}

Future<void> setMapLanguage(String language) async {
throw UnimplementedError('setMapLanguage() has not been implemented.');
}
Expand Down
16 changes: 16 additions & 0 deletions mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
case 'map#onCameraTrackingDismissed':
onCameraTrackingDismissedPlatform(null);
break;
case 'map#onIdle':
onMapIdlePlatform(null);
break;
default:
throw MissingPluginException();
}
Expand Down Expand Up @@ -137,6 +140,19 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
await _channel.invokeMethod('map#matchMapLanguageWithDeviceDefault');
}

@override
Future<void> updateContentInsets(EdgeInsets insets, bool animated) async {
await _channel.invokeMethod('map#updateContentInsets', <String, dynamic>{
'bounds': <String, double>{
'top': insets.top,
'left': insets.left,
'bottom': insets.bottom,
'right': insets.right,
},
'animated': animated,
});
}

@override
Future<void> setMapLanguage(String language) async {
await _channel.invokeMethod('map#setMapLanguage', <String, dynamic>{
Expand Down
4 changes: 0 additions & 4 deletions mapbox_gl_web/lib/src/mapbox_map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,9 @@ class MapboxMapController extends MapboxGlPlatform
_myLastLocation = LatLng(e.coords.latitude, e.coords.longitude);
});
_geolocateControl.on('trackuserlocationstart', (_) {
print('trackuserlocationstart');
_onCameraTrackingChanged(true);
});
_geolocateControl.on('trackuserlocationend', (_) {
print('trackuserlocationend');
_onCameraTrackingChanged(false);
_onCameraTrackingDismissed();
});
Expand Down Expand Up @@ -436,13 +434,11 @@ class MapboxMapController extends MapboxGlPlatform

@override
void setCompassEnabled(bool compassEnabled) {
print('TODO: setCompassEnabled $compassEnabled');
_updateNavigationControl(compassEnabled: compassEnabled);
}

@override
void setCompassGravity(int gravity) {
print('TODO: setCompassGravity $gravity');
_updateNavigationControl(position: CompassViewPosition.values[gravity]);
}

Expand Down

0 comments on commit 8daf3df

Please sign in to comment.