Created
January 30, 2016 20:38
-
-
Save naderio/bc7041a6cd786f6d0e2f to your computer and use it in GitHub Desktop.
NativeScript/Android: drawing polyline for Google Directions API response on Google Map
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const debug = require('nativescript-debug')(__filename); | |
const application = require('application'); | |
const observable = require('data/observable'); | |
const LatLng = com.google.android.gms.maps.model.LatLng; | |
const PolylineOptions = com.google.android.gms.maps.model.PolylineOptions; | |
const LatLngBounds = com.google.android.gms.maps.model.LatLngBounds; | |
const CameraUpdateFactory = com.google.android.gms.maps.CameraUpdateFactory; | |
const polyline = require('polyline'); | |
var model = new observable.Observable(); | |
model.set('constloading', false); | |
model.set('directionsShown', false); | |
var directionsPolyline; | |
function clearDirections(args) { | |
debug('clearDirections'); | |
if (directionsPolyline) { | |
directionsPolyline.remove(); | |
model.set('directionsShown', false); | |
} | |
} | |
exports.clearDirections = clearDirections; | |
function drawDirections(args) { | |
debug('drawDirections'); | |
clearDirections(); | |
model.set('loading', true); | |
fetch('https://maps.googleapis.com/maps/api/directions/json', { | |
query: { | |
origin: 'somewhere ...', | |
destination: 'somewhere else ...', | |
mode: 'driving', | |
key: 'your google service key here', | |
}, | |
}).then(function(result) { | |
debug('drawDirections', result); | |
var route = result.routes[0]; | |
var polylineData = polyline.decode(route.overview_polyline.points); | |
debug('drawDirections', route.overview_polyline.points, polylineData); | |
if (application.android) { | |
var polylineOptions = new PolylineOptions(); | |
polylineOptions.width(8); | |
polylineOptions.geodesic(true); | |
polylineOptions.color(CONST.ROUTE_COLOR.android); | |
polylineData.forEach(function(latlng) { | |
polylineOptions.add(new LatLng(latlng[0], latlng[1])); | |
}); | |
directionsPolyline = mapView.gMap.addPolyline(polylineOptions); | |
var bounds = new LatLngBounds.Builder(); | |
bounds.include(new LatLng(route.bounds.northeast.lat, route.bounds.northeast.lng)); | |
bounds.include(new LatLng(route.bounds.southwest.lat, route.bounds.southwest.lng)); | |
mapView.gMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds.build(), 50)); | |
model.set('directionsShown', true); | |
} else { | |
// TODO: iOS | |
} | |
}) | |
.catch(function(error) { | |
debug('drawDirections', error); | |
snackbar.simple("Échec d'opération"); | |
}).finally(function() { | |
model.set('loading', false); | |
}); | |
} | |
exports.drawDirections = drawDirections; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment