Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
4 changes: 2 additions & 2 deletions android/src/main/java/com/mapbox/mapboxgl/LineController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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 Expand Up @@ -59,7 +59,7 @@ public void setLineOpacity(float lineOpacity) {

@Override
public void setLineColor(String lineColor) {
line.setLineColor(ColorUtils.rgbaToColor(lineColor));
line.setLineColor(Color.parseColor(lineColor));
}

@Override
Expand Down
62 changes: 37 additions & 25 deletions example/lib/line.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,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 @@ -87,6 +81,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 @@ -102,7 +107,7 @@ class LineBodyState extends State<LineBody> {
current = 1.0;
}

_updateSelectedLine(
await _updateSelectedLine(
LineOptions(lineOpacity: current < 0.1 ? 1.0 : current * 0.75),
);
}
Expand All @@ -113,13 +118,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 @@ -137,12 +142,11 @@ class LineBodyState extends State<LineBody> {
children: <Widget>[
Center(
child: SizedBox(
width: 300.0,
height: 200.0,
height: 400.0,
child: MapboxMap(
accessToken: MapsDemo.ACCESS_TOKEN,
onMapCreated: _onMapCreated,
onStyleLoadedCallback: onStyleLoadedCallback,
onStyleLoadedCallback: _onStyleLoadedCallback,
initialCameraPosition: const CameraPosition(
target: LatLng(-33.852, 151.211),
zoom: 11.0,
Expand All @@ -155,9 +159,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 @@ -167,9 +171,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 @@ -196,7 +208,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] {
Copy link
Collaborator

@felix-ht felix-ht Nov 19, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rename this to something like getLineCoordinates

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if same code can be later reused for other annotations like Circle or Fill. But you're right, in this PR getLineCoordinates probably makes more sense

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm it could be indeed be reused for polygons (not for circles or symbols tho) so the name is probably fine!

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 @@ -54,7 +54,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 @@ -428,16 +428,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 @@ -448,23 +443,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 @@ -483,6 +471,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 @@ -873,7 +862,6 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma

}


/*
* MGLAnnotationControllerDelegate
*/
Expand Down Expand Up @@ -947,6 +935,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