| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the COPYING file. |
| 4 |
| 5 // This is a port of "A Simple Trip Meter Using the Geolocation API" to Dart. |
| 6 // See: http://www.html5rocks.com/en/tutorials/geolocation/trip_meter/ |
| 7 |
| 8 import 'dart:html'; |
| 9 import 'dart:math'; |
| 10 |
| 11 // Reused code - copyright Moveable Type Scripts |
| 12 // http://www.movable-type.co.uk/scripts/latlong.html |
| 13 // Under Creative Commons License http://creativecommons.org/licenses/by/3.0/ |
| 14 num calculateDistance(num lat1, num lon1, num lat2, num lon2) { |
| 15 const EARTH_RADIUS = 6371; // km |
| 16 num latDiff = lat2 - lat1; |
| 17 num lonDiff = lon2 - lon1; |
| 18 |
| 19 // a is the square of half the chord length between the points. |
| 20 var a = pow(sin(latDiff / 2), 2) + |
| 21 cos(lat1) * cos (lat2) * |
| 22 pow(sin(lonDiff / 2), 2); |
| 23 |
| 24 var angularDistance = 2 * atan2(sqrt(a), sqrt(1 - a)); |
| 25 return EARTH_RADIUS * angularDistance; |
| 26 } |
| 27 |
| 28 // Don't use alert() in real code ;) |
| 29 void alertError(PositionError error) { |
| 30 window.alert("Error occurred. Error code: ${error.code}"); |
| 31 } |
| 32 |
| 33 void main(){ |
| 34 Geoposition startPosition; |
| 35 |
| 36 window.navigator.geolocation.getCurrentPosition() |
| 37 .then((Geoposition position) { |
| 38 startPosition = position; |
| 39 query("#start-lat").text = "${startPosition.coords.latitude}"; |
| 40 query("#start-lon").text = "${startPosition.coords.longitude}"; |
| 41 }, onError: (error) => alertError(error)); |
| 42 |
| 43 window.navigator.geolocation.watchPosition().listen((Geoposition position) { |
| 44 query("#current-lat").text = "${position.coords.latitude}"; |
| 45 query("#current-lon").text = "${position.coords.longitude}"; |
| 46 num distance = calculateDistance( |
| 47 startPosition.coords.latitude, |
| 48 startPosition.coords.longitude, |
| 49 position.coords.latitude, |
| 50 position.coords.longitude); |
| 51 query("#distance").text = "$distance"; |
| 52 }, onError: (error) => alertError(error)); |
| 53 } |
| OLD | NEW |