| 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 LICENSE file. |
| 4 |
| 5 // A JS Interop sample accessing the Google Maps API. The sample is based on |
| 6 // the directions-panel example here: |
| 7 // https://google-developers.appspot.com/maps/documentation/javascript/examples/
directions-panel |
| 8 |
| 9 import 'dart:html'; |
| 10 import 'package:js/js.dart' as js; |
| 11 |
| 12 // Save the JS google.maps namespace for convenience. |
| 13 final maps = js.context.google.maps; |
| 14 |
| 15 var directionsDisplay; |
| 16 var directionsService; |
| 17 |
| 18 void main() { |
| 19 |
| 20 // Allocate a new JS Map with the following options. See: |
| 21 // https://developers.google.com/maps/documentation/javascript/reference#Map |
| 22 var myOptions = js.map({ |
| 23 'zoom': 9, |
| 24 'mapTypeId': maps.MapTypeId.ROADMAP, |
| 25 'center': new js.Proxy(maps.LatLng, 47.6097, -122.3331) |
| 26 }); |
| 27 |
| 28 var map = new js.Proxy(maps.Map, querySelector('#map_canvas'), myOptions); |
| 29 |
| 30 // Allocate a new JS DirectionsRenderer to display directions on the page. |
| 31 // See |
| 32 // https://developers.google.com/maps/documentation/javascript/reference#Direc
tionsRenderer |
| 33 directionsDisplay = new js.Proxy(maps.DirectionsRenderer, |
| 34 js.map({'map': map})); |
| 35 directionsDisplay.setPanel(querySelector('#directions_panel')); |
| 36 |
| 37 // Allocate a new JS DirectionService to forward requests to the server. |
| 38 // See: |
| 39 // https://developers.google.com/maps/documentation/javascript/reference#Direc
tionsService |
| 40 directionsService = new js.Proxy(maps.DirectionsService); |
| 41 |
| 42 var control = querySelector('#control'); |
| 43 control.style.display = 'block'; |
| 44 map.controls[maps.ControlPosition.TOP].push(control); |
| 45 |
| 46 // Recalculate the route when the start or end points are changed. |
| 47 querySelector('#start').onChange.listen(calcRoute); |
| 48 querySelector('#end').onChange.listen(calcRoute); |
| 49 } |
| 50 |
| 51 void calcRoute(e) { |
| 52 final panel = querySelector('#directions_panel'); |
| 53 final SelectElement start = querySelector('#start'); |
| 54 final SelectElement end = querySelector('#end'); |
| 55 |
| 56 panel.innerHtml = "<b>Thinking...</b>"; |
| 57 |
| 58 // Submit a new directions request. |
| 59 final request = js.map({ |
| 60 'origin': start.value, |
| 61 'destination': end.value, |
| 62 'travelMode': maps.DirectionsTravelMode.DRIVING |
| 63 }); |
| 64 |
| 65 directionsService.route(request, (response, status) { |
| 66 if (status == maps.DirectionsStatus.OK) { |
| 67 querySelector('#directions_panel').innerHtml = ""; |
| 68 directionsDisplay.setDirections(response); |
| 69 } else { |
| 70 querySelector('#directions_panel').innerHtml = "<b>Err, try flying.</b>"; |
| 71 } |
| 72 }); |
| 73 } |
| OLD | NEW |