Directions From Maps
Is it possible to give directions in a Ti mapview from one annotation to another? If not, is it possible to send the coordinates to the maps app and open it that way?
Thanks
Todd
10 Answers
-
You can't do this easily in a mapview.
But you can open the native Maps app and give it start coordinates and destination coordinates.Ti.App.openURL('http://maps.google.com/maps?saddr="+StartLatitude+","+StartLongitude+"&daddr='+DestLatitude+','+DestLongitude);
To do this in a mapview, you have to find a way to get the coordinates of all the points composing your route. And then to draw the route in your mapview using
mapview.addroute()
.
You can find an example in KitchenSink, in map_view2.js -
Using the official Google Directions API works as well. I prefer a compressed JSON response instead of XML ;)
Can publish an example on demand.
-
it's not currently possible. i'm not sure what you mean by your second question. can you be more specific?
-
Sorry for the confusion. Is there a way to open the native Maps app and send the coordinates for the starting location and ending location? I have seen this done in other apps.
Thanks
Todd -
we don't have a way to show a route right now. you can place start/end annotations on a map view though - but it sounds like you need the former.
-
correct, it would be nice to be able to do that in my app. I would be ok if I could send the data to the Google Maps app and open that application and view the map and directions from there though. Is that possible?
-
Are you planning to make a route planner? because i've also looked into that, and Google will not allow that (only in their own maps.app).
But you can calculate the distance between points, and in what direction you need to walk. And its also possible to draw a straight line from one annotation to another one.
-
I haven't made any tests about it, but when you use Geolocation, there are two parameters about precision.
Ti.Geolocation.preferredProvider
// For GPS Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS; // or, for Network geolocation Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_NETWORK;
Ti.Geolocation.accuracy
// Supported values : // Titanium.Geolocation.ACCURACY_BEST // Titanium.Geolocation.ACCURACY_NEAREST_TEN_METERS // Titanium.Geolocation.ACCURACY_HUNDRED_METERS // Titanium.Geolocation.ACCURACY_KILOMETER // Titanium.Geolocation.ACCURACY_THREE_KILOMETERS Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
-
This will give you the coordinates for the route, which you can then pass along to the mapView.addRoute function:
Ti.Geolocation.reverseGeocoder(sourceLatitude, sourceLongitude, function (evt) { var places = evt.places; if (places && places.length > 0) { //get points from google maps. var sourceAddress = places[0].address; Ti.API.info("sourceAddress: " + sourceAddress); var url = "http://maps.google.com/?saddr=" + sourceAddress + "&daddr=" + destinationAddress + "&doflg=ptk&hl=en&output=kml" utils.ajaxXml(url, function (xml) { //add points to route. 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] }); } } } //add route to map. var route = { name: "My Route", points: points, color: "#7c74d4", width: 4 }; mapView.addRoute(route); setTimeout(function () { mapView.regionFit = true; }, 700);//hack to get route to show }); } });
ajaxXml is just a helper method that makes a basic http call and returns back this.responseXML.
-
it is an feature request for an app that already has a map and shows different locations.
I have seen other apps that have their own map and points and then they can pass the data and open up the maps app with that point plotted on the map. The name of it is Whole Foods.