Created
November 9, 2011 10:56
-
-
Save joscandreu/1351119 to your computer and use it in GitHub Desktop.
Get a kml from Google and add it to a Ti.Map view
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
//Credits to Vali Filip | |
var win=Titanium.UI.currentWindow; | |
Titanium.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST; | |
var origin1 = Titanium.UI.createSearchBar({ | |
barColor:'#000', | |
showCancel:true, | |
height:40, | |
top:0, | |
}); | |
win.add(origin1); | |
var dest1 = Titanium.UI.createSearchBar({ | |
barColor:'#000', | |
showCancel:true, | |
height:40, | |
top:40, | |
}); | |
win.add(dest1); | |
dest1.addEventListener('return',function(){ | |
addr=origin1.value; | |
Titanium.Geolocation.forwardGeocoder(addr,function(evt) | |
{ | |
var latitude=evt.latitude; | |
var longitude=evt.longitude; | |
var boston={latitude:latitude, longitude:longitude,latitudeDelta:0.1, longitudeDelta:0.1}; | |
var anno = Ti.Map.createAnnotation({ | |
latitude:latitude, | |
longitude:longitude, | |
pincolor:Ti.Map.ANNOTATION_GREEN, | |
animate:true, | |
}); | |
var mapview = Titanium.Map.createView({ | |
mapType: Titanium.Map.STANDARD_TYPE, | |
region: boston, | |
animate:true, | |
//regionFit:false, | |
regionFit:true, | |
userLocation:true, | |
top:90, | |
height:301, | |
annotations:[anno] | |
}); | |
dest1.addEventListener('cancel',function(){ | |
dest1.blur(); | |
}); | |
zoomout = Titanium.UI.createButton({ | |
title:'-', | |
style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED, | |
//top:390, | |
}); | |
win.setToolbar([zoomout]); | |
zoomout.addEventListener('click',function() { | |
mapview.zoom(-1); | |
}); | |
dest1.blur(); | |
var origin=origin1.value; | |
var destination=dest1.value; | |
data = []; | |
var url = "http://maps.google.com/?saddr=" | |
+ origin + "&daddr=" | |
+ destination + "&doflg=ptk&hl=en&output=kml"; | |
xhr = Titanium.Network.createHTTPClient(); | |
xhr.open('GET',url); | |
Ti.API.info('URL: '+url); | |
xhr.onload = function(){ | |
// Now parse the XML | |
var xml = this.responseXML; | |
var points = []; | |
var coords = xml.documentElement.getElementsByTagName("LineString"); | |
for(var cc=0; cc < coords.length; cc++) { | |
var line = coords.item(cc); | |
var str = line.firstChild.text.split(" "); | |
for(dd = 0; dd < str.length; dd++) { | |
var loc = str[dd].split(','); | |
if(loc[0] && loc[1]) { | |
points.push({latitude: loc[1], | |
longitude: loc[0]}); | |
} | |
} | |
} | |
var route = { | |
name:"boston", | |
points:points, | |
color:"blue", | |
width:2 | |
}; | |
// add a route | |
mapview.addRoute(route); | |
}; | |
xhr.send(); | |
win.add(mapview); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment