Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(433)

Side by Side Diff: src/site/samples/geolocation/example/trip_meter.dart

Issue 47213005: Added geolocation (new); surfaced appcache and webaudio samples (Closed) Base URL: https://github.com/dart-lang/dartlang.org.git@master
Patch Set: More tweaks Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « src/site/samples/geolocation/example/trip_meter.css ('k') | src/site/samples/geolocation/example/trip_meter.dart.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698