From 8f9838ff5db4e6d4c17e09a5088afb38430b693b Mon Sep 17 00:00:00 2001 From: GULERTOLGA Date: Sat, 24 Oct 2020 15:11:28 +0300 Subject: [PATCH] Get meters per pixel at latitude (#416) * fix git refferences * fix git refferences * implementation of getMetersPerPixelAtLatitude * getMetersPerPixelAtLatitude * fix refference paths * Android implementation and Example updated. * added comments to getMetersPerPixelAtLatitude method * IOS implementation * Removed modified lines from pubspec.yaml files * web implementation Co-authored-by: Tobrun --- .../mapbox/mapboxgl/MapboxMapController.java | 7 ++++ example/lib/map_ui.dart | 4 +++ ios/Classes/MapboxMapController.swift | 7 ++++ lib/src/controller.dart | 6 ++++ .../lib/src/mapbox_gl_platform_interface.dart | 5 +++ .../lib/src/method_channel_mapbox_gl.dart | 14 ++++++++ mapbox_gl_web/lib/mapbox_gl_web.dart | 1 + .../lib/src/mapbox_map_controller.dart | 8 +++++ pubspec.lock | 35 ++++++++++--------- 9 files changed, 71 insertions(+), 16 deletions(-) diff --git a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java index 56c37a19f..a73728a38 100644 --- a/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java +++ b/android/src/main/java/com/mapbox/mapboxgl/MapboxMapController.java @@ -513,6 +513,13 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) { result.success(reply); break; } + case "map#getMetersPerPixelAtLatitude": { + Map reply = new HashMap<>(); + Double retVal = mapboxMap.getProjection().getMetersPerPixelAtLatitude((Double)call.argument("latitude")); + reply.put("metersperpixel", retVal); + result.success(reply); + break; + } case "camera#move": { final CameraUpdate cameraUpdate = Convert.toCameraUpdate(call.argument("cameraUpdate"), mapboxMap, density); if (cameraUpdate != null) { diff --git a/example/lib/map_ui.dart b/example/lib/map_ui.dart index b27ceb944..216a0ecd5 100644 --- a/example/lib/map_ui.dart +++ b/example/lib/map_ui.dart @@ -309,6 +309,10 @@ class MapUiBodyState extends State { Point convertedPoint = await mapController.toScreenLocation(latLng); LatLng convertedLatLng = await mapController.toLatLng(point); print("Map long press converted: ${convertedPoint.x},${convertedPoint.y} ${convertedLatLng.latitude}/${convertedLatLng.longitude}"); + double metersPerPixel = await mapController.getMetersPerPixelAtLatitude(latLng.latitude); + + print ("Map long press The distance measured in meters at latitude ${latLng.latitude} is $metersPerPixel m"); + List features = await mapController.queryRenderedFeatures(point, [], null); if (features.length>0) { diff --git a/ios/Classes/MapboxMapController.swift b/ios/Classes/MapboxMapController.swift index 88a2d10da..e15b03942 100644 --- a/ios/Classes/MapboxMapController.swift +++ b/ios/Classes/MapboxMapController.swift @@ -176,6 +176,13 @@ class MapboxMapController: NSObject, FlutterPlatformView, MGLMapViewDelegate, Ma reply["x"] = returnVal.x as NSObject reply["y"] = returnVal.y as NSObject result(reply) + case "map#getMetersPerPixelAtLatitude": + guard let arguments = methodCall.arguments as? [String: Any] else { return } + var reply = [String: NSObject]() + guard let latitude = arguments["latitude"] as? Double else { return } + let returnVal = mapView.metersPerPoint(atLatitude:latitude) + reply["metersperpixel"] = returnVal as NSObject + result(reply) case "map#toLatLng": guard let arguments = methodCall.arguments as? [String: Any] else { return } guard let x = arguments["x"] as? Double else { return } diff --git a/lib/src/controller.dart b/lib/src/controller.dart index 812de8655..7848eabcc 100644 --- a/lib/src/controller.dart +++ b/lib/src/controller.dart @@ -790,4 +790,10 @@ class MapboxMapController extends ChangeNotifier { return MapboxGlPlatform.getInstance(_id).toLatLng(screenLocation); } + /// Returns the distance spanned by one pixel at the specified [latitude] and current zoom level. + /// The distance between pixels decreases as the latitude approaches the poles. This relationship parallels the relationship between longitudinal coordinates at different latitudes. + Future getMetersPerPixelAtLatitude(double latitude) async{ + return MapboxGlPlatform.getInstance(_id).getMetersPerPixelAtLatitude(latitude); + } + } \ No newline at end of file diff --git a/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart b/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart index 090f4a905..abb7f13e6 100644 --- a/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart +++ b/mapbox_gl_platform_interface/lib/src/mapbox_gl_platform_interface.dart @@ -254,4 +254,9 @@ abstract class MapboxGlPlatform { throw UnimplementedError( 'toLatLng() has not been implemented.'); } + + Future getMetersPerPixelAtLatitude(double latitude) async{ + throw UnimplementedError( + 'getMetersPerPixelAtLatitude() has not been implemented.'); + } } diff --git a/mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart b/mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart index 04645e189..0417a6b87 100644 --- a/mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart +++ b/mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart @@ -575,4 +575,18 @@ class MethodChannelMapboxGl extends MapboxGlPlatform { return new Future.error(e); } } + + @override + Future getMetersPerPixelAtLatitude(double latitude) async{ + try { + var latLngMap = await _channel + .invokeMethod('map#getMetersPerPixelAtLatitude', { + 'latitude': latitude, + }); + return latLngMap['metersperpixel']; + } on PlatformException catch (e) { + return new Future.error(e); + } + } + } diff --git a/mapbox_gl_web/lib/mapbox_gl_web.dart b/mapbox_gl_web/lib/mapbox_gl_web.dart index df5ca795f..1ee7b279a 100644 --- a/mapbox_gl_web/lib/mapbox_gl_web.dart +++ b/mapbox_gl_web/lib/mapbox_gl_web.dart @@ -5,6 +5,7 @@ import 'dart:async'; // ignore_for_file: avoid_web_libraries_in_flutter import 'dart:html'; import 'dart:js'; +import 'dart:math'; import 'dart:typed_data'; import 'dart:ui' as ui; diff --git a/mapbox_gl_web/lib/src/mapbox_map_controller.dart b/mapbox_gl_web/lib/src/mapbox_map_controller.dart index 4a5cc1765..4930247ff 100644 --- a/mapbox_gl_web/lib/src/mapbox_map_controller.dart +++ b/mapbox_gl_web/lib/src/mapbox_map_controller.dart @@ -628,4 +628,12 @@ class MapboxMapController extends MapboxGlPlatform var lngLat = _map.unproject(mapbox.Point(screenLocation.x, screenLocation.y)); return LatLng(lngLat.lat, lngLat.lng); } + + @override + Future getMetersPerPixelAtLatitude(double latitude) async{ + //https://wiki.openstreetmap.org/wiki/Zoom_levels + var circumference = 40075017.686; + var zoom = _map.getZoom(); + return circumference * cos(latitude * (pi/180)) / pow(2, zoom + 9); + } } diff --git a/pubspec.lock b/pubspec.lock index f15493608..1bf49dc00 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -15,6 +15,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.6.0" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.0" charcode: dependency: transitive description: @@ -28,7 +35,7 @@ packages: name: collection url: "https://pub.dartlang.org" source: hosted - version: "1.14.12" + version: "1.14.13" convert: dependency: transitive description: @@ -59,14 +66,14 @@ packages: name: image url: "https://pub.dartlang.org" source: hosted - version: "2.1.13" + version: "2.1.14" js: dependency: transitive description: name: js url: "https://pub.dartlang.org" source: hosted - version: "0.6.1+1" + version: "0.6.2" mapbox_gl_dart: dependency: transitive description: @@ -78,20 +85,16 @@ packages: dependency: "direct main" description: path: mapbox_gl_platform_interface - ref: HEAD - resolved-ref: fa9dac81b6d0f3a5f01688a6695ee938b71874e5 - url: "https://github.com/tobrun/flutter-mapbox-gl.git" - source: git - version: "0.7.0" + relative: true + source: path + version: "0.8.0" mapbox_gl_web: dependency: "direct main" description: path: mapbox_gl_web - ref: HEAD - resolved-ref: fa9dac81b6d0f3a5f01688a6695ee938b71874e5 - url: "https://github.com/tobrun/flutter-mapbox-gl.git" - source: git - version: "0.7.0" + relative: true + source: path + version: "0.8.0" meta: dependency: transitive description: @@ -124,7 +127,7 @@ packages: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.2.0" vector_math: dependency: transitive description: @@ -138,7 +141,7 @@ packages: name: xml url: "https://pub.dartlang.org" source: hosted - version: "3.7.0" + version: "4.5.1" sdks: - dart: ">=2.7.0 <3.0.0" + dart: ">=2.9.0-14.0.dev <3.0.0" flutter: ">=1.12.13+hotfix.4 <2.0.0"