From 08d75f9cf94f949638594b6241c3d5ac68444e70 Mon Sep 17 00:00:00 2001 From: m0nac0 <58807793+m0nac0@users.noreply.github.com> Date: Sun, 15 May 2022 11:22:26 +0200 Subject: [PATCH] Cherry-pick upstream#776 (Handle line color and geometry) (#80) Cherry-pick upstream#776 https: //github.com/flutter-mapbox-gl/maps/pull/776 Co-Authored-By: Anton Averin <1481332+AAverin@users.noreply.github.com> --- .../com/mapbox/mapboxgl/LineController.java | 2 +- example/lib/line.dart | 62 +++++++++++-------- ios/Classes/Convert.swift | 22 +++++++ ios/Classes/MapboxMapController.swift | 37 ++++------- 4 files changed, 73 insertions(+), 50 deletions(-) diff --git a/android/src/main/java/com/mapbox/mapboxgl/LineController.java b/android/src/main/java/com/mapbox/mapboxgl/LineController.java index af78536ee..7400fa84d 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/LineController.java +++ b/android/src/main/java/com/mapbox/mapboxgl/LineController.java @@ -17,7 +17,7 @@ import com.mapbox.mapboxsdk.geometry.LatLng; import com.mapbox.mapboxsdk.plugins.annotation.Line; import com.mapbox.mapboxsdk.plugins.annotation.LineManager; -import com.mapbox.mapboxsdk.utils.ColorUtils; +import android.graphics.Color; /** * Controller of a single Line on the map. diff --git a/example/lib/line.dart b/example/lib/line.dart index b278ae64d..13b34ad54 100644 --- a/example/lib/line.dart +++ b/example/lib/line.dart @@ -45,26 +45,20 @@ class LineBodyState extends State { super.dispose(); } - void _onLineTapped(Line line) { - if (_selectedLine != null) { - _updateSelectedLine( - const LineOptions( - lineWidth: 28.0, - ), - ); - } + _onLineTapped(Line line) async { + await _updateSelectedLine( + LineOptions(lineColor: "#ff0000"), + ); setState(() { _selectedLine = line; }); - _updateSelectedLine( - LineOptions( - // linecolor: , - ), + await _updateSelectedLine( + LineOptions(lineColor: "#ffe100"), ); } - void _updateSelectedLine(LineOptions changes) { - controller!.updateLine(_selectedLine!, changes); + _updateSelectedLine(LineOptions changes) async { + if (_selectedLine != null) controller!.updateLine(_selectedLine!, changes); } void _add() { @@ -86,6 +80,17 @@ class LineBodyState extends State { }); } + _move() async { + final currentStart = _selectedLine!.options.geometry![0]; + final currentEnd = _selectedLine!.options.geometry![1]; + final end = + LatLng(currentEnd.latitude + 0.001, currentEnd.longitude + 0.001); + final start = + LatLng(currentStart.latitude - 0.001, currentStart.longitude - 0.001); + await controller! + .updateLine(_selectedLine!, LineOptions(geometry: [start, end])); + } + void _remove() { controller!.removeLine(_selectedLine!); setState(() { @@ -101,7 +106,7 @@ class LineBodyState extends State { current = 1.0; } - _updateSelectedLine( + await _updateSelectedLine( LineOptions(lineOpacity: current < 0.1 ? 1.0 : current * 0.75), ); } @@ -112,13 +117,13 @@ class LineBodyState extends State { // default value current = 1.0; } - _updateSelectedLine( + await _updateSelectedLine( LineOptions(lineOpacity: current == 0.0 ? 1.0 : 0.0), ); } - void onStyleLoadedCallback() { - controller!.addLine( + _onStyleLoadedCallback() async { + await controller!.addLine( LineOptions( geometry: [LatLng(37.4220, -122.0841), LatLng(37.4240, -122.0941)], lineColor: "#ff0000", @@ -136,11 +141,10 @@ class LineBodyState extends State { children: [ Center( child: SizedBox( - width: 300.0, - height: 200.0, + height: 400.0, child: MaplibreMap( onMapCreated: _onMapCreated, - onStyleLoadedCallback: onStyleLoadedCallback, + onStyleLoadedCallback: _onStyleLoadedCallback, initialCameraPosition: const CameraPosition( target: LatLng(-33.852, 151.211), zoom: 11.0, @@ -153,9 +157,9 @@ class LineBodyState extends State { child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ - Row( + Column( children: [ - Column( + Row( children: [ TextButton( child: const Text('add'), @@ -165,9 +169,17 @@ class LineBodyState extends State { child: const Text('remove'), onPressed: (_selectedLine == null) ? null : _remove, ), + TextButton( + child: const Text('move'), + onPressed: (_selectedLine == null) + ? null + : () async { + await _move(); + }, + ), ], ), - Column( + Row( children: [ TextButton( child: const Text('change alpha'), @@ -194,7 +206,7 @@ class LineBodyState extends State { ], ), ], - ) + ), ], ), ), diff --git a/ios/Classes/Convert.swift b/ios/Classes/Convert.swift index 23b435564..f72c71363 100644 --- a/ios/Classes/Convert.swift +++ b/ios/Classes/Convert.swift @@ -327,6 +327,28 @@ class Convert { } } + class func getCoordinates(options: Any?) -> [CLLocationCoordinate2D] { + var coordinates: [CLLocationCoordinate2D] = [] + + if let options = options as? [String: Any], + let geometry = options["geometry"] as? [[Double]], geometry.count > 0 { + for coordinate in geometry { + coordinates.append(CLLocationCoordinate2DMake(coordinate[0], coordinate[1])) + } + } + return coordinates + } + + class func interpretGeometryUpdate(options: Any?, delegate: MGLLineStyleAnnotation) { + if let options = options as? [String: Any], + let geometry = options["geometry"] as? [[Double]], geometry.count > 0 { + if let feature = delegate.feature as? MGLPolylineFeature { + var coordinates = Convert.getCoordinates(options: options) + feature.setCoordinates(&coordinates, count: UInt(coordinates.count)) + } + } + } + class func interpretFillOptions(options: Any?, delegate: MGLPolygonStyleAnnotation) { guard let options = options as? [String: Any] else { return } if let fillOpacity = options["fillOpacity"] as? CGFloat { diff --git a/ios/Classes/MapboxMapController.swift b/ios/Classes/MapboxMapController.swift index a64bad542..294433a75 100644 --- a/ios/Classes/MapboxMapController.swift +++ b/ios/Classes/MapboxMapController.swift @@ -50,7 +50,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma singleTap.require(toFail: recognizer) } mapView.addGestureRecognizer(singleTap) - + let longPress = UILongPressGestureRecognizer(target: self, action: #selector(handleMapLongPress(sender:))) for recognizer in mapView.gestureRecognizers! where recognizer is UILongPressGestureRecognizer { longPress.require(toFail: recognizer) @@ -419,16 +419,11 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma case "line#add": guard let lineAnnotationController = lineAnnotationController else { return } guard let arguments = methodCall.arguments as? [String: Any] else { return } - // Parse geometry - if let options = arguments["options"] as? [String: Any], - let geometry = options["geometry"] as? [[Double]] { - // Convert geometry to coordinate and create a line. - var lineCoordinates: [CLLocationCoordinate2D] = [] - for coordinate in geometry { - lineCoordinates.append(CLLocationCoordinate2DMake(coordinate[0], coordinate[1])) - } - let line = MGLLineStyleAnnotation(coordinates: lineCoordinates, count: UInt(lineCoordinates.count)) - Convert.interpretLineOptions(options: arguments["options"], delegate: line) + + if let options = arguments["options"] as? [String: Any] { + var coordinates = Convert.getCoordinates(options: options) + let line = MGLLineStyleAnnotation(coordinates: &coordinates, count: UInt(coordinates.count)) + Convert.interpretLineOptions(options: options, delegate: line) lineAnnotationController.addStyleAnnotation(line) lineAnnotationController.annotationsInteractionEnabled = annotationConsumeTapEvents.contains("AnnotationType.line") result(line.identifier) @@ -439,23 +434,16 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma case "line#addAll": guard let lineAnnotationController = lineAnnotationController else { return } guard let arguments = methodCall.arguments as? [String: Any] else { return } - // Parse geometry + var identifier: String? = nil if let allOptions = arguments["options"] as? [[String: Any]]{ var lines: [MGLLineStyleAnnotation] = []; for options in allOptions { - if let geometry = options["geometry"] as? [[Double]] { - guard geometry.count > 0 else { break } - // Convert geometry to coordinate and create a line. - var lineCoordinates: [CLLocationCoordinate2D] = [] - for coordinate in geometry { - lineCoordinates.append(CLLocationCoordinate2DMake(coordinate[0], coordinate[1])) - } - let line = MGLLineStyleAnnotation(coordinates: lineCoordinates, count: UInt(lineCoordinates.count)) - Convert.interpretLineOptions(options: options, delegate: line) - lines.append(line) - } + var coordinates = Convert.getCoordinates(options: options) + let line = MGLLineStyleAnnotation(coordinates: &coordinates, count: UInt(coordinates.count)) + Convert.interpretLineOptions(options: options, delegate: line) + lines.append(line) } if !lines.isEmpty { lineAnnotationController.addStyleAnnotations(lines) @@ -474,6 +462,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma for line in lineAnnotationController.styleAnnotations() { if line.identifier == lineId { + Convert.interpretGeometryUpdate(options: arguments["options"], delegate: line as! MGLLineStyleAnnotation) Convert.interpretLineOptions(options: arguments["options"], delegate: line as! MGLLineStyleAnnotation) lineAnnotationController.updateStyleAnnotation(line) break; @@ -864,7 +853,6 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma } - /* * MGLAnnotationControllerDelegate */ @@ -922,6 +910,7 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma case "AnnotationType.line": lineAnnotationController = MGLLineAnnotationController(mapView: self.mapView) lineAnnotationController!.annotationsInteractionEnabled = annotationConsumeTapEvents.contains("AnnotationType.line") + lineAnnotationController?.delegate = self case "AnnotationType.circle": circleAnnotationController = MGLCircleAnnotationController(mapView: self.mapView)