Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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?


— asked March 23rd 2010 by Todd Tompkins
  • directions
  • google
  • iphone
  • maps

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.


    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

    — answered June 24th 2011 by Adrien Mas
    • Anrien, I just wanna thank you since it helped me too. But Ti.App.openURL has been changed tp Ti.Platform.openURLin in Titanium SDK 1.6.

      — commented June 26th 2011 by Mel Maxwell
    • Yes, you're absolutely right Nigel.
      It's Ti.Platform.openURL now.

      — commented June 26th 2011 by Adrien Mas
    • Hi Anrien, since you're at it. I have a question about currention to ask.
      I have tried the geolocation demostration from the kitchen sink. I noticed that it geolocation module relies on the GPS.
      My concern is to get the user current location faster by 3G or wifi since I don't need a very precise coordinate to use your code. Any idea?


      Geo code from kitchen sink

          if (e.error)
              alert('HFL cannot get your current location');
          var longitude = e.coords.longitude;
          var latitude = e.coords.latitude;
          var altitude = e.coords.altitude;
          var heading = e.coords.heading;
          var accuracy = e.coords.accuracy;
          var speed = e.coords.speed;
          var timestamp = e.coords.timestamp;
          var altitudeAccuracy = e.coords.altitudeAccuracy;
          //CREATE MAP VIEW
          var mapview = Titanium.Map.createView({
              mapType: Titanium.Map.STANDARD_TYPE,
              region: {latitude: latitude, longitude: longitude, latitudeDelta:0.01, longitudeDelta:0.01},

      — commented June 26th 2011 by Mel Maxwell
  • Using the official Google Directions API works as well. I prefer a compressed JSON response instead of XML ;)

    Can publish an example on demand.

    — answered December 26th 2011 by Danny Pham
    1 Comment
    • Please do publish

      — commented February 5th 2012 by Noble Edward
  • it's not currently possible. i'm not sure what you mean by your second question. can you be more specific?

    — answered March 23rd 2010 by Nolan Wright
  • 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.


    — answered March 23rd 2010 by Todd Tompkins
  • 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.

    — answered March 23rd 2010 by Nolan Wright
  • 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?

    — answered March 23rd 2010 by Todd Tompkins
  • 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.

    — answered March 24th 2010 by Glenn Tillemans
  • 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;
    — answered June 27th 2011 by Adrien Mas
  • 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
            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.

    — answered December 19th 2011 by Justin Toth
    1 Comment
    • i love the get to show route hack!

      setTimeout(function () { mapView.regionFit = true; }, 700);//hack to get route to show


      — commented January 10th 2012 by Aizil Akmar Omar
  • 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.

    — answered March 24th 2010 by Todd Tompkins
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.