Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick upstream#791 #84

Merged
merged 4 commits into from
May 19, 2022
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
18 changes: 7 additions & 11 deletions example/android/settings.gradle
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
include ':app'

def flutterProjectRoot = rootProject.projectDir.parentFile.toPath()
def localPropertiesFile = new File(rootProject.projectDir, "local.properties")
def properties = new Properties()

def plugins = new Properties()
def pluginsFile = new File(flutterProjectRoot.toFile(), '.flutter-plugins')
if (pluginsFile.exists()) {
pluginsFile.withReader('UTF-8') { reader -> plugins.load(reader) }
}
assert localPropertiesFile.exists()
localPropertiesFile.withReader("UTF-8") { reader -> properties.load(reader) }

plugins.each { name, path ->
def pluginDirectory = flutterProjectRoot.resolve(path).resolve('android').toFile()
include ":$name"
project(":$name").projectDir = pluginDirectory
}
def flutterSdkPath = properties.getProperty("flutter.sdk")
assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
3 changes: 3 additions & 0 deletions example/lib/click_annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class ClickAnnotationBodyState extends State<ClickAnnotationBody> {
@override
void dispose() {
controller?.onFillTapped.remove(_onFillTapped);
controller?.onCircleTapped.remove(_onCircleTapped);
controller?.onLineTapped.remove(_onLineTapped);
controller?.onSymbolTapped.remove(_onSymbolTapped);
super.dispose();
}

Expand Down
22 changes: 10 additions & 12 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
private var mapView: MGLMapView
private var isMapReady = false
private var isStyleReady = false
private var isStyleReadyNotified = false
private var onStyleLoadedCalled = false
private var mapReadyResult: FlutterResult?

private var initialTilt: CGFloat?
Expand Down Expand Up @@ -86,10 +86,10 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
result(nil)
//Style could happen to been ready before the map was ready due to the order of methods invoked
//We should invoke onStyleLoaded
if isStyleReady && !isStyleReadyNotified {
if isStyleReady && !onStyleLoadedCalled {
if let channel = channel {
onStyleLoadedCalled = true
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
isStyleReadyNotified = true
}
}
} else {
Expand Down Expand Up @@ -891,7 +891,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
* MGLMapViewDelegate
*/
func mapView(_ mapView: MGLMapView, didFinishLoading style: MGLStyle) {
isStyleReadyNotified = false
onStyleLoadedCalled = false
isMapReady = true
updateMyLocationEnabled()

Expand Down Expand Up @@ -925,17 +925,15 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma
}
}

mapReadyResult?(nil)

//If map is ready and map#waitForMap was called, we invoke onStyleLoaded callback directly
//If not, we will have to call it when map#waitForMap is done
if let mapReadyResult = mapReadyResult {
mapReadyResult(nil)
if (!isStyleReadyNotified) {
if let channel = channel {
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
isStyleReadyNotified = true
}
if !onStyleLoadedCalled {
if let channel = channel {
onStyleLoadedCalled = true
channel.invokeMethod("map#onStyleLoaded", arguments: nil)
}

}

isStyleReady = true
Expand Down
11 changes: 8 additions & 3 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -261,11 +261,16 @@ class MaplibreMapController extends ChangeNotifier {
/// The json in [geojson] has to comply with the schema for FeatureCollection
/// as specified in https://datatracker.ietf.org/doc/html/rfc7946#section-3.3
///
/// [promoteId] can be used on web to promote an id from properties to be the
/// id of the feature. This is useful because by default mapbox-gl-js does not
/// support string ids
///
/// The returned [Future] completes after the change has been made on the
/// platform side.
Future<void> addGeoJsonSource(
String sourceId, Map<String, dynamic> geojson) async {
await _mapboxGlPlatform.addGeoJsonSource(sourceId, geojson);
Future<void> addGeoJsonSource(String sourceId, Map<String, dynamic> geojson,
{String? promoteId}) async {
await _mapboxGlPlatform.addGeoJsonSource(sourceId, geojson,
promoteId: promoteId);
}

/// Sets new geojson data to and existing source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,8 @@ abstract class MapLibreGlPlatform {
'getMetersPerPixelAtLatitude() has not been implemented.');
}

Future<void> addGeoJsonSource(
String sourceId, Map<String, dynamic> geojson) async {
Future<void> addGeoJsonSource(String sourceId, Map<String, dynamic> geojson,
{String? promoteId}) async {
throw UnimplementedError('addGeoJsonSource() has not been implemented.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,8 +726,8 @@ class MethodChannelMaplibreGl extends MapLibreGlPlatform {
}

@override
Future<void> addGeoJsonSource(
String sourceId, Map<String, dynamic> geojson) async {
Future<void> addGeoJsonSource(String sourceId, Map<String, dynamic> geojson,
{String? promoteId}) async {
await _channel.invokeMethod('source#addGeoJson', <String, dynamic>{
'sourceId': sourceId,
'geojson': jsonEncode(geojson),
Expand Down
55 changes: 44 additions & 11 deletions maplibre_gl_web/lib/src/mapbox_map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class MaplibreMapController extends MapLibreGlPlatform

late Map<String, dynamic> _creationParams;
late MapboxMap _map;
bool _mapReady = false;

List<String> annotationOrder = [];
final _featureLayerIdentifiers = Set<String>();
Expand Down Expand Up @@ -62,6 +63,13 @@ class MaplibreMapController extends MapLibreGlPlatform
),
);
_map.on('load', _onStyleLoaded);
_map.on('click', _onMapClick);
// long click not available in web, so it is mapped to double click
_map.on('dblclick', _onMapLongClick);
_map.on('movestart', _onCameraMoveStarted);
_map.on('move', _onCameraMove);
_map.on('moveend', _onCameraIdle);
_map.on('resize', _onMapResize);
}
Convert.interpretMapboxMapOptions(_creationParams['options'], this);

Expand Down Expand Up @@ -407,6 +415,7 @@ class MaplibreMapController extends MapLibreGlPlatform
}

void _onStyleLoaded(_) {
_mapReady = true;
for (final annotationType in annotationOrder) {
switch (annotationType) {
case 'AnnotationType.symbol':
Expand All @@ -428,15 +437,7 @@ class MaplibreMapController extends MapLibreGlPlatform
"Unknown annotation type: \(annotationType), must be either 'fill', 'line', 'circle' or 'symbol'");
}
}

onMapStyleLoadedPlatform(null);
_map.on('click', _onMapClick);
// long click not available in web, so it is mapped to double click
_map.on('dblclick', _onMapLongClick);
_map.on('movestart', _onCameraMoveStarted);
_map.on('move', _onCameraMove);
_map.on('moveend', _onCameraIdle);
_map.on('resize', _onMapResize);
}

void _onMapResize(Event e) {
Expand Down Expand Up @@ -715,7 +716,19 @@ class MaplibreMapController extends MapLibreGlPlatform

@override
void setStyleString(String? styleString) {
//remove old mouseenter callbacks to avoid multicalling
for (var layerId in _featureLayerIdentifiers) {
_map.off('mouseenter', layerId, _onMouseEnterFeature);
_map.off('mousemouve', layerId, _onMouseEnterFeature);
_map.off('mouseleave', layerId, _onMouseLeaveFeature);
}
_featureLayerIdentifiers.clear();

_map.setStyle(styleString);
// catch style loaded for later style changes
if (_mapReady) {
_map.once("styledata", _onStyleLoaded);
}
}

@override
Expand Down Expand Up @@ -789,9 +802,13 @@ class MaplibreMapController extends MapLibreGlPlatform
}

@override
Future<void> addGeoJsonSource(
String sourceId, Map<String, dynamic> geojson) async {
_map.addSource(sourceId, {"type": 'geojson', "data": geojson});
Future<void> addGeoJsonSource(String sourceId, Map<String, dynamic> geojson,
{String? promoteId}) async {
_map.addSource(sourceId, {
"type": 'geojson',
"data": geojson,
if (promoteId != null) "promoteId": promoteId
});
}

@override
Expand Down Expand Up @@ -857,5 +874,21 @@ class MaplibreMapController extends MapLibreGlPlatform
'layout': layout,
'paint': paint
}, belowLayerId);

_featureLayerIdentifiers.add(layerId);
if (layerType == "fill") {
_map.on('mousemove', layerId, _onMouseEnterFeature);
} else {
_map.on('mouseenter', layerId, _onMouseEnterFeature);
}
_map.on('mouseleave', layerId, _onMouseLeaveFeature);
}

void _onMouseEnterFeature(_) {
_map.getCanvas().style.cursor = 'pointer';
}

void _onMouseLeaveFeature(_) {
_map.getCanvas().style.cursor = '';
}
}