| OLD | NEW |
| (Empty) |
| 1 #!mojo mojo:js_content_handler | |
| 2 | |
| 3 define("main", [ | |
| 4 "console", | |
| 5 "examples/js/maps/geocoder.mojom", | |
| 6 "mojo/public/js/core", | |
| 7 "mojo/public/js/unicode", | |
| 8 "mojo/services/public/js/application", | |
| 9 "mojo/services/network/public/interfaces/network_service.mojom", | |
| 10 "mojo/services/network/public/interfaces/url_loader.mojom", | |
| 11 "third_party/js/url", | |
| 12 ], function(console, geocoder, core, unicode, application, network, loader, url)
{ | |
| 13 | |
| 14 const Application = application.Application; | |
| 15 const Bounds = geocoder.Bounds; | |
| 16 const Geocoder = geocoder.Geocoder; | |
| 17 const Geometry = geocoder.Geometry; | |
| 18 const Location = geocoder.Location; | |
| 19 const NetworkService = network.NetworkService; | |
| 20 const Result = geocoder.Result; | |
| 21 const Status = geocoder.Status; | |
| 22 const URLRequest = loader.URLRequest; | |
| 23 const URL = url.URL; | |
| 24 | |
| 25 var netService; | |
| 26 | |
| 27 Location.prototype.queryString = function() { | |
| 28 // TBD: format floats to 6 decimal places | |
| 29 return this.latitude + ", " + this.longitude; | |
| 30 } | |
| 31 | |
| 32 Location.fromJSON = function(json) { | |
| 33 return !json ? null : new Location({ | |
| 34 latitude: json.lat, | |
| 35 longitude: json.lng, | |
| 36 }); | |
| 37 } | |
| 38 | |
| 39 Bounds.fromJSON = function(json) { | |
| 40 return !json ? null : new Bounds({ | |
| 41 northeast: Location.fromJSON(json.northeast), | |
| 42 southwest: Location.fromJSON(json.southwest), | |
| 43 }); | |
| 44 } | |
| 45 | |
| 46 Geometry.fromJSON = function(json) { | |
| 47 return !json ? null : new Geometry({ | |
| 48 location: Location.fromJSON(json.location), | |
| 49 location_type: json.location_type, | |
| 50 viewport: Bounds.fromJSON(json.viewport), | |
| 51 bounds: Bounds.fromJSON(json.bounds), | |
| 52 }); | |
| 53 } | |
| 54 | |
| 55 Result.fromJSON = function(json) { | |
| 56 return !json ? null : new Result({ | |
| 57 partial_match: !!json.partial_match, | |
| 58 formatted_address: json.formatted_address, | |
| 59 geometry: Geometry.fromJSON(json.geometry), | |
| 60 types: json.types, | |
| 61 // TBD: address_components | |
| 62 }); | |
| 63 } | |
| 64 | |
| 65 function parseGeocodeResponse(arrayBuffer) { | |
| 66 return JSON.parse(unicode.decodeUtf8String(new Uint8Array(arrayBuffer))); | |
| 67 } | |
| 68 | |
| 69 function geocodeRequest(url) { | |
| 70 return new Promise(function(resolveRequest) { | |
| 71 var urlLoader; | |
| 72 netService.createURLLoader(function(urlLoaderProxy) { | |
| 73 urlLoader = urlLoaderProxy; | |
| 74 }); | |
| 75 | |
| 76 var urlRequest = new URLRequest({ | |
| 77 url: url.format(), | |
| 78 method: "GET", | |
| 79 auto_follow_redirects: true | |
| 80 }); | |
| 81 | |
| 82 urlLoader.start(urlRequest).then(function(urlLoaderResult) { | |
| 83 core.drainData(urlLoaderResult.response.body).then( | |
| 84 function(drainDataResult) { | |
| 85 // TBD: handle drainData failure | |
| 86 var value = parseGeocodeResponse(drainDataResult.buffer); | |
| 87 resolveRequest({ | |
| 88 status: value.status, | |
| 89 results: value.results.map(Result.fromJSON), | |
| 90 }); | |
| 91 }); | |
| 92 }); | |
| 93 }); | |
| 94 } | |
| 95 | |
| 96 function geocodeURL(key, value, options) { | |
| 97 var url = new URL("https://maps.googleapis.com/maps/api/geocode/json"); | |
| 98 url.query = {}; | |
| 99 url.query[key] = value; | |
| 100 // TBD: add options url.query | |
| 101 return url; | |
| 102 } | |
| 103 | |
| 104 class GeocoderImpl { | |
| 105 addressToLocation(address, options) { | |
| 106 return geocodeRequest(geocodeURL("address", address, options)); | |
| 107 } | |
| 108 | |
| 109 locationToAddress(location, options) { | |
| 110 return geocodeRequest( | |
| 111 geocodeURL("latlng", location.queryString(), options)); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 class GeocoderService extends Application { | |
| 116 initialize() { | |
| 117 netService = this.shell.connectToService( | |
| 118 "mojo:network_service", NetworkService); | |
| 119 } | |
| 120 | |
| 121 acceptConnection(initiatorURL, serviceProvider) { | |
| 122 serviceProvider.provideService(Geocoder, GeocoderImpl); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 return GeocoderService; | |
| 127 }); | |
| 128 | |
| OLD | NEW |