Titanium Community Questions & Answer Archive

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

Using Routes in Titanium to calculate distance when driving from point A to point B

Hi
I have created an app for a company to handle their working time, projects and shoppinglists. Now I am adding the ability to track their cars and travels. I am letting them go from point A to point B and to solve this they enter their destination address and I lookup their current location and use Google Maps API to lookup the GPS coordinates for the address they enter. This works great, BUT!

When I add a route between these two points it is the birds way from point A to point B that is drawn and I want to be able to calculate and get the points into the map as if they drive a car not fly by helicopter :)

This is my code so far:

function getGPSfromAddress(address){
    var locLat;
    var locLng;
    var url="http://maps.google.com/maps/api/geocode/json?address="+address+"&sensor=true";
    xhr = Titanium.Network.createHTTPClient();
    xhr.open('GET',url);
            Ti.API.info('>>> go get data for Rgeocode! ...URL: '+url);
    xhr.onload = function(){
        var json = this.responseText;
        var gotitems = eval('(' + json + ')');
        Ti.API.info('>GPS found:' + gotitems.results[0].geometry.location.lat);
        locLat = gotitems.results[0].geometry.location.lat;
        locLng = gotitems.results[0].geometry.location.lng;

        var startPosition = Titanium.Map.createAnnotation({
            latitude: latitude,
            longitude: longitude,
            title: 'Startposition',
            subtitle: 'Adress: ' + lbDisplayAddress.text,
            pincolor: Titanium.Map.ANNOTATION_RED,
            animate: true
        });
        var destPosition = Titanium.Map.createAnnotation({
            latitude: locLat,
            longitude: locLng,
            title: 'Slutposition',
            subtitle: 'Adress: ' + address,
            pincolor: Titanium.Map.ANNOTATION_GREEN,
            animate: true
        });
        travelMapView.addAnnotation(startPosition);
        travelMapView.addAnnotation(destPosition);

        var points = [];
        points[0] = {latitude:latitude,longitude:longitude};
        points[1] = {latitude:locLat,longitude:locLng};

        var route = {
           name:'Trip',
           points:points,
           color:'blue',
          width:4
        };
        travelMapView.addRoute(route);

        travelMapView.setLocation({
            longitude: longitude, 
            latitude: latitude,
            regionFit: true,
            animate: true,
            latitudeDelta:0.001, 
            longitudeDelta:0.001
        });
    };

xhr.send(); 
}

So how can I access some mapinfo like Google to get all the current points which will show them the driving path not the birds path? Any ideas?

— asked October 25th 2010 by Andreas Kviby
  • map
  • mapview
  • routes
0 Comments

5 Answers

  • No, routes still don't work on Android unfortunately. Quite a lot of people waiting for an ETA on that (including me).

    — answered September 12th 2011 by Boydlee Pollentine
    permalink
    0 Comments
  • I need the same thing, to find the coordinates for the curves in the route.
    If you put this address in the browser, you will get a kml file that has a tag Placemark at the end with those points, but adding a output=json attribute at the end will change the result of the get.

    Using a xhr from titanium, the response to that link is a html page. Can someone point us in the right directions with this? I need it badly.

    Thanks.

    — answered October 28th 2010 by Vali Filip
    permalink
    0 Comments
  • Hey Vali
    I just cracked it I think. We have to use Googles Direction Services to get all the steps from point A to point B. Look at the function I created to get all the correct data into an XML list.

    function loadGPSInformation(origin,destination){
    
        data = [];
        var url="http://maps.googleapis.com/maps/api/directions/xml?origin="+origin+"&destination="+destination+"&sensor=true";
        xhr = Titanium.Network.createHTTPClient();
        xhr.open('GET',url);
                Ti.API.info('>>> go get data for Rgeocode! ...URL: '+url);
        xhr.onload = function(){
    
              // Now parse the XML 
            var xml = this.responseXML;
    
            // Find the steps in response
            var itemList = xml.documentElement.getElementsByTagName("step");
            Ti.API.info('found '+itemList.length+' items in the step xml');
        };
        xhr.send();
    

    After this you will have to loop through and parse the result but within this XML all steps are present with all their directions and GPS coordinates fo you to draw your correct route within Titanium.

    Small loop through code below

    for (var cc=0;cc < itemList.length;cc++){    
    
    var title = null;
    var distance = 0;        
    // Item title
    title = itemList.item(cc).getElementsByTagName("html_instructions").item(0).text;
    distance = itemList.item(cc).getElementsByTagName("distance").item(0).firstChild.text + " m";
    }
    

    This way you can get all directions but also all driving instructions. Happy coding and please share any ideas and code with me ak[at]gkviby.com

    — answered October 28th 2010 by Andreas Kviby
    permalink
    2 Comments
    • It's perfect ;) Do you know where I could find some documentation on the parameters that can be passed to that link? I would be interested in a via option, and how the points should be defined.

      Thanks again.

      — commented October 28th 2010 by Vali Filip
    • Hi andreas i copied the above code but it showed me the annotation at unknown point with map stating infinite loop one. Can you send me the code at my email … ranjanmanu[at]yahoo.com

      thanks ..

      — commented July 23rd 2011 by Manu ranjan
  • I haven't tested this yet, but seems to be a valid way to do this. Great find Andreas.. ;) thank you very much :D and code strong :P

    — answered October 28th 2010 by Vali Filip
    permalink
    0 Comments
  • will this work on Android?

    — answered July 9th 2011 by Brian Dittmer
    permalink
    0 Comments
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.