Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Javascript to calculate possible routes between two points

    Hi,

    I need a help with a project I am working on.
    I am not a developer so not so familiar with javascript.
    Suppose I have a map with ten junctions and based on that map,
    I need to calculate all the possible routes between one junction to another.
    How do I represent the map in javascript and how do I code the function to calculate all the possible routes?? :-(
    many thanks if someone can help me

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,922
    Thanks
    56
    Thanked 548 Times in 545 Posts
    I would look at something like the google maps directions service - I think they have done most of the heavy lifting for you.

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    thanks for the suggestion :-)
    I actually did try that earlier, but their code was going above my head though.
    I am not even as half technical as the others on this forum, which is why I needed a simple javascript code :-(

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,922
    Thanks
    56
    Thanked 548 Times in 545 Posts
    fair enough, but the problem is that it's not really a simple thing that you are trying to do.

    if they are real junctions and real roads (or whatever the routes will be) some sort of maps API will be the simplest way to do this.

    the problem with looking for "all the possible routes" is partially to do with logic - all the possibilities amount to a near infinite number, considering that going from your house to your next door neighbor's via China is one possible route.

    so narrowing it down to "all the best routes" or even "the one best route per start and end point" is definitely going to help

    if on the other hand the junctions are chosen by you (and will never change) and you can dictate the routes as well, it's as simple as drawing some lines on the map.

  • #5
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    you are quite right :-)
    My actual aim is to find all the "best routes" and in my case, the junctions are going to be defined by me and not going to change (may be if I want to add a new junction to the existing links, then probably I might have to add the routes too) and the routes are limited too !!

    so how do I write a javascript function for this then

  • #6
    New Coder
    Join Date
    Apr 2011
    Posts
    49
    Thanks
    0
    Thanked 13 Times in 12 Posts
    It's been quite a while since I read it, but chapter 7 of http://eloquentjavascript.net/contents.html explains a pathfinding algorythm seems to fit your aim.

  • Users who have thanked Goos for this post:

    itzombie (10-19-2011)

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,192
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    If you really want to write this yourself, the FIRST thing you should do is sit down with pencil and paper, draw the node map, and then figure out how you would get the answer by hand.

    *THEN* convert your mental methodology to code.

    If you can't express the solution in human terms, you can't write the code.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,922
    Thanks
    56
    Thanked 548 Times in 545 Posts
    some examples that may be of use:

    this displays two route options between the same start and end points

    this is a point and click tool that creates code that you can use in google maps to show lines on the map (note that one option that you can select is "directions"

    if you have your junctions defined (and know which routes you want to display), it would seem to me to be easier to use the second one and hardcode the lines onto your page (or in an external DB) rather than making repeated calls to the directions service

  • Users who have thanked xelawho for this post:

    itzombie (10-19-2011)

  • #9
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,192
    Thanks
    75
    Thanked 4,341 Times in 4,307 Posts
    xelawho: I could be wrong, but I strongly suspect this is homework. An exercise in program design and problem solving. I doubt that he needs any graphical representation (that is, any map) at all. Don't be surprised if he uses prompt() and document.write() to present the answer.

    But we shall see.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #10
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,922
    Thanks
    56
    Thanked 548 Times in 545 Posts
    Quote Originally Posted by Old Pedant
    I could be wrong, but I strongly suspect this is homework.
    well, if that's true it would make me feel fine about continuing to be vaguely useful on his one...

  • #11
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Quote Originally Posted by Old Pedant View Post
    xelawho: I could be wrong, but I strongly suspect this is homework.
    In this case either he is enrolled in a high level programming class (which I doubt) or his teacher uses to give his students laborious homework to do.

    The problem is rather intricate, even if the paths are straight lines. But if they are curves, one might need to use interpolations and integral calculations to solve it. I doubt it can be solved with a couple of prompts and several elementary lines of codes.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #12
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I am going through the tutorial given by GOOS.
    Very helpful indeed :-)
    yea this project is for my final semster in college / school whatever you call it
    thanks everyone..
    will bug you all again if I get stuck somewhere :P


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •