Created
January 18, 2020 22:01
-
-
Save xantiagoma/fa1f0b2beddd82109bbcaf13e9704c9f to your computer and use it in GitHub Desktop.
LatLng Tilr x y z point
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'package:latlong/latlong.dart'; | |
| import 'dart:math' as math; | |
| extension LatLngExtension on LatLng { | |
| Map<String, double> toPoint() { | |
| var siny = math.min( | |
| math.max(math.sin(this.latitude * (math.pi / 180)), -.9999), | |
| .9999, | |
| ); | |
| return { | |
| 'x': 128 + this.longitude * (256 / 360), | |
| 'y': 128 + | |
| 0.5 * math.log((1 + siny) / (1 - siny)) * -(256 / (2 * math.pi)), | |
| }; | |
| } | |
| Map<String, num> toTile(double zoom) { | |
| final t = math.pow(2, zoom); | |
| final s = 256 / t; | |
| final p = this.toPoint(); | |
| return {'x': (p['x'] / s).floor(), 'y': (p['y'] / s).floor(), 'z': zoom}; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment