Routing - useless?

You must Login before you can answer or comment on any questions.

In my app, I'm letting people add address they plan on visiting and want to be able to have it route from the current location to the destination.

I have it working fine in that I can click the annotation and it will open the link in the Maps app and plot from there, but I would like it in-app if possible, as opening it in the Maps app is kind of a hack.

The kitchen sink just has them plotted in a csv file manually, and that seems kind of a bad example to me.

If nothing else, can I get the distance between the points based on directions, and not straight line (which it does now)?

— asked 4 years ago by Josh Lewis
1 Comment
  • I've been wondering this myself

    — commented 4 years ago by Chris Nelson

9 Answers

Josh,

Here is an example. Mapquest can be twitchy so set your referer URL in the settings to just * and allow empty referers. Paste in your API key into the app.js sample and give it a run.

This class has several functions but the only ones that matter are shown in app.js.

The first is init and it takes your key and mapview as parameters.

The next is route and you can send it the location parameter in the form that Mapquest expects which means you can do lat, lng or send full addresses and it will geocode them - the sample shows the correct form.

The last method actually draws the route on the map and you can specify the title, color and width of the route on the map.

BTW - I made the 2nd and 3rd methods separate so you can choice between just calling and getting routing information and being able to draw it. I do not know what your goal is so making them two calls allowed for extra flexibility.

A second BTW is that each method takes more parameters than I demonstrate and you can look inside the class and see them.

If you look inside the class you will see that I also expose the distance and time to travel and you can tell it things like quickest, shortest, pedestrian and other options. This is a crude example but it'll get you started.

Be careful with the API key. If you paste it in and it has % symbols in it, the key is URL encoded and it shouldn't be. You will need to URL decode the key and use it raw or you will get an error from mapquest saying the key is invalid.

I plan on putting this code on GitHub and refining it so please put feedback here and I'll try to incorporate it into the GitHub repository.

Sample Code

GitHub Repository

You could try to make the route fetching some data from google maps. But take care with the google TOS, they don't allow driving directions inside an app( if you don't pay for).

I agree. I used googles api for requesting directons (i.e. point to point).

— answered 4 years ago by Steve Clark
answer permalink
1 Comment
  • Do you have an example of how you do that? I'm not using webviews or anything like that.

    — commented 4 years ago by Josh Lewis

Does anyone think if I just modify the routes.csv code in kitchen sink to have a starting and ending point, it will route, or do I NEED to have every point in-between?

var f = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory,'examples','route.csv');
var csv = f.read();
var points = [];
var lines = csv.toString().split("\n");
for (var c=0;c<lines.length;c++)
{
    var line = lines[c];
    var latlong = line.split(",");
    if (latlong.length > 1)
    {
        var lat = latlong[0];
        var lon = latlong[1];
        var entry = {latitude:lat,longitude:lon};
        points[c]=entry;
    }
}
 
// route object
var route = {
    name:"boston",
    points:points,
    color:"red",
    width:4
};
 
// add a route
mapview.addRoute(route);

The problem with adding routing directly to the API is the moment you do someone will complain that you didn't use the right one.

There are many different providers of direction services, several of which are free, and they each have different licenses and terms. Do you know there are some that will not let you route over a Google map because they work exclusively with Microsoft? There are others that will also discriminate against the platform you target. I had to work with a company that would let you do mapping and directions on Android but not iPhone. Because of all of these screwball licenses and terms it would be difficult for Appcelerator to add this to the API and still target multiple platforms. And all of this doesn't even take into account that a direction provider may not even support the country you are in!

If you are in the US and are targeting the US I would use the mapquest API. It is clean, easy to use and returns a very nice JSON response that is easy to parse and very complete. They are also free if you fit their criteria. Yes, I have used them in several projects and they work great.

Here is the main portal for the Mapquest API http://developer.mapquest.com/

This is the directions API http://www.mapquestapi.com/directions/

John,

Do you have a code sample? I basically have a starting and ending lat an Lon and just want to get in-app directions from there.

Let me put together a basic example from some code that I have. It will require a Mapquest API key so you may want to sign up and get your key and check out their API while I get the code ready.

Your Answer

Think you can help? Login to answer this question!