Skip to content

Instantly share code, notes, and snippets.

@JamesCullum
Created January 27, 2020 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamesCullum/51d554f3dfa57dd1df9e3216fcbb02a1 to your computer and use it in GitHub Desktop.
Save JamesCullum/51d554f3dfa57dd1df9e3216fcbb02a1 to your computer and use it in GitHub Desktop.
Calculate intermediate point in percentage between two geographic location by latitude / longitude
/*
MIT License
Copyright (c) 2020 JamesCullum (Pseudonym)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
import 'dart:math' show cos, sqrt, sin, pi, atan2;
// Normally you'd use this in combination with the google_maps_flutter package, which isn't available on DartPad.
// This is why we have to mock the LatLng class.
class LatLng {
double _latitude;
double _longitude;
LatLng(this._latitude, this._longitude);
double get latitude => _latitude;
double get longitude => _longitude;
String toString() {
return "<$latitude, $longitude>";
}
}
void main() {
LatLng start = new LatLng(24.1489545, 51.5490672);
LatLng end = new LatLng(24.1376864, 49.0625771);
for (double i = 0.0; i <= 1.0; i += 0.25) {
LatLng intermediatePoint = calculateIntermediatePoint(start, end, i);
print("Position at ${(i * 100).round()}% is $intermediatePoint");
}
print(
"Compare coordinates on the map: https://goo.gl/maps/ePA2ksmpdZXTgyv9A");
}
// Original calculation from https://www.movable-type.co.uk/scripts/latlong.html
LatLng calculateIntermediatePoint(LatLng point1, LatLng point2, double perc) {
//const φ1 = this.lat.toRadians(), λ1 = this.lon.toRadians();
//const φ2 = point.lat.toRadians(), λ2 = point.lon.toRadians();
double lat1 = degreesToRadians(point1.latitude);
double lng1 = degreesToRadians(point1.longitude);
double lat2 = degreesToRadians(point2.latitude);
double lng2 = degreesToRadians(point2.longitude);
//const Δφ = φ2 - φ1;
//const Δλ = λ2 - λ1;
double deltaLat = lat2 - lat1;
double deltaLng = lng2 - lng1;
//const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) + Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ/2) * Math.sin(Δλ/2);
//const δ = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double calcA = sin(deltaLat / 2) * sin(deltaLat / 2) +
cos(lat1) * cos(lat2) * sin(deltaLng / 2) * sin(deltaLng / 2);
double calcB = 2 * atan2(sqrt(calcA), sqrt(1 - calcA));
//const A = Math.sin((1-fraction)*δ) / Math.sin(δ);
//const B = Math.sin(fraction*δ) / Math.sin(δ);
double A = sin((1 - perc) * calcB) / sin(calcB);
double B = sin(perc * calcB) / sin(calcB);
//const x = A * Math.cos(φ1) * Math.cos(λ1) + B * Math.cos(φ2) * Math.cos(λ2);
//const y = A * Math.cos(φ1) * Math.sin(λ1) + B * Math.cos(φ2) * Math.sin(λ2);
//const z = A * Math.sin(φ1) + B * Math.sin(φ2);
double x = A * cos(lat1) * cos(lng1) + B * cos(lat2) * cos(lng2);
double y = A * cos(lat1) * sin(lng1) + B * cos(lat2) * sin(lng2);
double z = A * sin(lat1) + B * sin(lat2);
//const φ3 = Math.atan2(z, Math.sqrt(x*x + y*y));
//const λ3 = Math.atan2(y, x);
double lat3 = atan2(z, sqrt(x * x + y * y));
double lng3 = atan2(y, x);
//const lat = φ3.toDegrees();
//const lon = λ3.toDegrees();
return LatLng(radiansToDegrees(lat3), radiansToDegrees(lng3));
}
double degreesToRadians(double degrees) {
return degrees * (pi / 180);
}
double radiansToDegrees(double radians) {
return radians * (180 / pi);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment