Created
June 10, 2016 18:08
-
-
Save wackyapps/f6c1dc9c62e78124bc8707ae64f02440 to your computer and use it in GitHub Desktop.
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
Ti.Geolocation.getCurrentPosition(function(evt) { | |
var origin = String(evt.coords.latitude + ',' + evt.coords.longitude), | |
travelMode = 'walking', | |
destination = String(yourLatitude + ',' + yourLongitude), | |
url = "http://maps.google.com/maps/api/directions/xml?mode=" | |
+ travelMode + "&origin=" | |
+ origin + "&destination=" | |
+ destination +"&sensor=false"; | |
xhr = Titanium.Network.createHTTPClient(); | |
xhr.open('GET',url); | |
Ti.API.info('>>> go get data for Rgeocode! ...URL: ' + url); | |
xhr.onload = function(e){ | |
var xml = this.responseXML, | |
points = [], | |
steps = xml.documentElement.getElementsByTagName("step"), | |
totalSteps = steps.length; | |
for (var i=0; i < totalSteps; i++) { | |
var startLocation = steps.item(i).getElementsByTagName("start_location"); | |
startLatitude = startLocation.item(0).getElementsByTagName("lat").item(0).text, | |
startLongitude = startLocation.item(0).getElementsByTagName("lng").item(0).text; | |
points.push({latitude:startLatitude, longitude:startLongitude}); | |
} | |
// Get last point and add it to the array, as we are only parsing <start_location> | |
var finalLocation = steps.item(totalSteps - 1).getElementsByTagName("end_location"), | |
finalLatitude = finalLocation.item(0).getElementsByTagName("lat").item(0).text, | |
finalLongitude = finalLocation.item(0).getElementsByTagName("lng").item(0).text; | |
points.push({latitude:finalLatitude, longitude:finalLongitude}); | |
// Create route and annotations | |
var route = { | |
name:"bonVoyage", | |
points:points, | |
color:"blue", | |
width:6 | |
}, startAnnotation = Ti.Map.createAnnotation({ | |
pincolor: Ti.Map.ANNOTATION_RED, | |
latitude: points[0].latitude, | |
longitude: points[0].longitude, | |
title: 'Current location' | |
}), endAnnotation = Ti.Map.createAnnotation({ | |
pincolor: Ti.Map.ANNOTATION_RED, | |
latitude: points[points.length - 1].latitude, | |
longitude: points[points.length - 1].longitude, | |
title: 'Destination' | |
}); | |
// Add elements | |
mapView.addRoute(route); | |
mapView.addAnnotation(startAnnotation); | |
mapView.addAnnotation(endAnnotation); | |
}; | |
xhr.send(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment