Skip to content

Commit

Permalink
Cherry-pick upstream#776 (Handle line color and geometry) (#80)
Browse files Browse the repository at this point in the history
Cherry-pick upstream#776

https: //github.com/flutter-mapbox-gl/maps/pull/776

Co-Authored-By: Anton Averin <1481332+AAverin@users.noreply.github.com>
  • Loading branch information
m0nac0 and AAverin committed May 15, 2022
1 parent 592bda1 commit 08d75f9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
62 changes: 37 additions & 25 deletions example/lib/line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,20 @@ class LineBodyState extends State<LineBody> {
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() {
Expand All @@ -86,6 +80,17 @@ class LineBodyState extends State<LineBody> {
});
}

_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(() {
Expand All @@ -101,7 +106,7 @@ class LineBodyState extends State<LineBody> {
current = 1.0;
}

_updateSelectedLine(
await _updateSelectedLine(
LineOptions(lineOpacity: current < 0.1 ? 1.0 : current * 0.75),
);
}
Expand All @@ -112,13 +117,13 @@ class LineBodyState extends State<LineBody> {
// 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",
Expand All @@ -136,11 +141,10 @@ class LineBodyState extends State<LineBody> {
children: <Widget>[
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,
Expand All @@ -153,9 +157,9 @@ class LineBodyState extends State<LineBody> {
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
Column(
children: <Widget>[
Column(
Row(
children: <Widget>[
TextButton(
child: const Text('add'),
Expand All @@ -165,9 +169,17 @@ class LineBodyState extends State<LineBody> {
child: const Text('remove'),
onPressed: (_selectedLine == null) ? null : _remove,
),
TextButton(
child: const Text('move'),
onPressed: (_selectedLine == null)
? null
: () async {
await _move();
},
),
],
),
Column(
Row(
children: <Widget>[
TextButton(
child: const Text('change alpha'),
Expand All @@ -194,7 +206,7 @@ class LineBodyState extends State<LineBody> {
],
),
],
)
),
],
),
),
Expand Down
22 changes: 22 additions & 0 deletions ios/Classes/Convert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
37 changes: 13 additions & 24 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -864,7 +853,6 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma

}


/*
* MGLAnnotationControllerDelegate
*/
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 08d75f9

Please sign in to comment.