Skip to content

Commit

Permalink
Migrate to flutter_map v6
Browse files Browse the repository at this point in the history
  • Loading branch information
HugoHeneault committed Oct 31, 2023
1 parent 75d73b7 commit 20093c5
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 35 deletions.
1 change: 1 addition & 0 deletions .fvm/flutter_sdk
4 changes: 4 additions & 0 deletions .fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"flutterSdkVersion": "3.10.3",
"flavors": {}
}
1 change: 1 addition & 0 deletions example/.fvm/flutter_sdk
4 changes: 4 additions & 0 deletions example/.fvm/fvm_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"flutterSdkVersion": "3.10.3",
"flavors": {}
}
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
5 changes: 4 additions & 1 deletion example/ios/Runner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 50;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -179,10 +179,12 @@
/* Begin PBXShellScriptBuildPhase section */
3B06AD1E1E4923F5004D2608 /* Thin Binary */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${TARGET_BUILD_DIR}/${INFOPLIST_PATH}",
);
name = "Thin Binary";
outputPaths = (
Expand All @@ -193,6 +195,7 @@
};
9740EEB61CF901F6004384FC /* Run Script */ = {
isa = PBXShellScriptBuildPhase;
alwaysOutOfDate = 1;
buildActionMask = 2147483647;
files = (
);
Expand Down
2 changes: 2 additions & 0 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,7 @@
<false/>
<key>CADisableMinimumFrameDurationOnPhone</key>
<true/>
<key>UIApplicationSupportsIndirectInputEvents</key>
<true/>
</dict>
</plist>
4 changes: 2 additions & 2 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class _MyHomePageState extends State<MyHomePage> {
),
body: FlutterMap(
options: MapOptions(
center: LatLng(45.1313258, 5.5171205),
zoom: 11.0,
initialCenter: LatLng(45.1313258, 5.5171205),
initialZoom: 11.0,
),
children: [
TileLayer(
Expand Down
12 changes: 10 additions & 2 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ packages:
dependency: transitive
description:
name: flutter_map
sha256: "5286f72f87deb132daa1489442d6cc46e986fc105cb727d9ae1b602b35b1d1f3"
sha256: "2b925948b675ef74ca524179fb133dbe0a21741889ccf56ad08fc8dcc38ba94b"
url: "https://pub.dev"
source: hosted
version: "5.0.0"
version: "6.0.1"
flutter_map_tappable_polyline:
dependency: "direct main"
description:
Expand Down Expand Up @@ -130,6 +130,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.0"
logger:
dependency: transitive
description:
name: logger
sha256: "6bbb9d6f7056729537a4309bda2e74e18e5d9f14302489cc1e93f33b3fe32cac"
url: "https://pub.dev"
source: hosted
version: "2.0.2+1"
matcher:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: example
description: A new Flutter project.
version: 5.0.0
version: 6.0.0
publish_to: "none"

environment:
Expand Down
55 changes: 30 additions & 25 deletions lib/flutter_map_tappable_polyline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ library flutter_map_tappable_polyline;
import 'dart:math';

import 'package:flutter/widgets.dart';
import 'package:flutter_map/plugin_api.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:latlong2/latlong.dart';

/// A polyline with a tag
Expand Down Expand Up @@ -45,35 +45,36 @@ class TappablePolylineLayer extends PolylineLayer {
this.onTap,
this.onMiss,
this.pointerDistanceTolerance = 15,
super.polylineCulling = false,
super.key,
});
polylineCulling = false,
key,
}) : super(key: key, polylines: polylines, polylineCulling: polylineCulling);

@override
Widget build(BuildContext context) {
final map = FlutterMapState.of(context);
final mapCamera = MapCamera.of(context);

return _build(
context,
Size(map.size.x, map.size.y),
Size(mapCamera.size.x, mapCamera.size.y),
polylineCulling
? polylines
.where((p) => p.boundingBox.isOverlapping(map.bounds))
.where(
(p) => p.boundingBox.isOverlapping(mapCamera.visibleBounds))
.toList()
: polylines,
);
}

Widget _build(BuildContext context, Size size, List<TaggedPolyline> lines) {
FlutterMapState mapState = FlutterMapState.maybeOf(context)!;
final mapState = MapCamera.of(context);

for (TaggedPolyline polyline in lines) {
polyline._offsets.clear();
var i = 0;
for (var point in polyline.points) {
var pos = mapState.project(point);
pos = (pos * mapState.getZoomScale(mapState.zoom, mapState.zoom)) -
mapState.pixelOrigin;
mapState.pixelOrigin.toDoublePoint();
polyline._offsets.add(Offset(pos.x.toDouble(), pos.y.toDouble()));
if (i > 0 && i < polyline.points.length) {
polyline._offsets.add(Offset(pos.x.toDouble(), pos.y.toDouble()));
Expand All @@ -88,10 +89,10 @@ class TappablePolylineLayer extends PolylineLayer {
// For some strange reason i have to add this callback for the onDoubleTapDown callback to be called.
},
onDoubleTapDown: (TapDownDetails details) {
_zoomMap(details, context, mapState);
_zoomMap(details, context);
},
onTapUp: (TapUpDetails details) {
_forwardCallToMapOptions(details, context, mapState);
_forwardCallToMapOptions(details, context);
_handlePolylineTap(details, onTap, onMiss);
},
child: Stack(
Expand Down Expand Up @@ -173,16 +174,17 @@ class TappablePolylineLayer extends PolylineLayer {
onTap!(candidates[closestToTapKey], details);
}

void _forwardCallToMapOptions(
TapUpDetails details, BuildContext context, FlutterMapState mapState) {
void _forwardCallToMapOptions(TapUpDetails details, BuildContext context) {
final latlng = _offsetToLatLng(details.localPosition, context.size!.width,
context.size!.height, mapState);
context.size!.height, context);

final mapOptions = MapOptions.of(context);

final tapPosition =
TapPosition(details.globalPosition, details.localPosition);

// Forward the onTap call to map.options so that we won't break onTap
mapState.options.onTap?.call(tapPosition, latlng);
mapOptions.onTap?.call(tapPosition, latlng);
}

double _distance(Offset point1, Offset point2) {
Expand All @@ -194,21 +196,24 @@ class TappablePolylineLayer extends PolylineLayer {
return distance;
}

void _zoomMap(
TapDownDetails details, BuildContext context, FlutterMapState mapState) {
void _zoomMap(TapDownDetails details, BuildContext context) {
final mapCamera = MapCamera.of(context);
final mapController = MapController.of(context);

var newCenter = _offsetToLatLng(details.localPosition, context.size!.width,
context.size!.height, mapState);
mapState.move(newCenter, mapState.zoom + 0.5,
source: MapEventSource.doubleTap);
context.size!.height, context);
mapController.move(newCenter, mapCamera.zoom + 0.5);
}

LatLng _offsetToLatLng(
Offset offset, double width, double height, FlutterMapState mapState) {
var localPoint = CustomPoint(offset.dx, offset.dy);
Offset offset, double width, double height, BuildContext context) {
final mapCamera = MapCamera.of(context);

var localPoint = Point(offset.dx, offset.dy);
var localPointCenterDistance =
CustomPoint((width / 2) - localPoint.x, (height / 2) - localPoint.y);
var mapCenter = mapState.project(mapState.center);
Point((width / 2) - localPoint.x, (height / 2) - localPoint.y);
var mapCenter = mapCamera.project(mapCamera.center);
var point = mapCenter - localPointCenterDistance;
return mapState.unproject(point);
return mapCamera.unproject(point);
}
}
12 changes: 10 additions & 2 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ packages:
dependency: "direct main"
description:
name: flutter_map
sha256: "5286f72f87deb132daa1489442d6cc46e986fc105cb727d9ae1b602b35b1d1f3"
sha256: "2b925948b675ef74ca524179fb133dbe0a21741889ccf56ad08fc8dcc38ba94b"
url: "https://pub.dev"
source: hosted
version: "5.0.0"
version: "6.0.1"
http:
dependency: transitive
description:
Expand Down Expand Up @@ -102,6 +102,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.0"
logger:
dependency: transitive
description:
name: logger
sha256: "6bbb9d6f7056729537a4309bda2e74e18e5d9f14302489cc1e93f33b3fe32cac"
url: "https://pub.dev"
source: hosted
version: "2.0.2+1"
material_color_utilities:
dependency: transitive
description:
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_map: ^5.0.0
flutter_map: ^6.0.1
latlong2: ^0.9.0

dev_dependencies:
Expand Down

0 comments on commit 20093c5

Please sign in to comment.