Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get meters per pixel at latitude #416

Merged
merged 11 commits into from
Oct 24, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,13 @@ public void onMethodCall(MethodCall call, MethodChannel.Result result) {
result.success(reply);
break;
}
case "map#getMetersPerPixelAtLatitude": {
Map<String, Object> 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) {
Expand Down
4 changes: 4 additions & 0 deletions example/lib/map_ui.dart
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ class MapUiBodyState extends State<MapUiBody> {
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) {
Expand Down
7 changes: 7 additions & 0 deletions ios/Classes/MapboxMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
6 changes: 6 additions & 0 deletions lib/src/controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> getMetersPerPixelAtLatitude(double latitude) async{
return MapboxGlPlatform.getInstance(_id).getMetersPerPixelAtLatitude(latitude);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,9 @@ abstract class MapboxGlPlatform {
throw UnimplementedError(
'toLatLng() has not been implemented.');
}

Future<double> getMetersPerPixelAtLatitude(double latitude) async{
throw UnimplementedError(
'getMetersPerPixelAtLatitude() has not been implemented.');
}
}
14 changes: 14 additions & 0 deletions mapbox_gl_platform_interface/lib/src/method_channel_mapbox_gl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -575,4 +575,18 @@ class MethodChannelMapboxGl extends MapboxGlPlatform {
return new Future.error(e);
}
}

@override
Future<double> getMetersPerPixelAtLatitude(double latitude) async{
try {
var latLngMap = await _channel
.invokeMethod('map#getMetersPerPixelAtLatitude', <String, dynamic>{
'latitude': latitude,
});
return latLngMap['metersperpixel'];
} on PlatformException catch (e) {
return new Future.error(e);
}
}

}
1 change: 1 addition & 0 deletions mapbox_gl_web/lib/mapbox_gl_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
8 changes: 8 additions & 0 deletions mapbox_gl_web/lib/src/mapbox_map_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> 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);
}
}
35 changes: 19 additions & 16 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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"