#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep Power
    0

    Calculating the distance in mile from Point A to Point B


    Hi, My cousin have a trucking business and he wants me to a page in his website that the people can type from to address and base on the miles between the address they get a quote. I bet there is a easy way to do this by using widget, but since I'm learning Python this will be a good project. Can you guys show me the right way to start this project. Thanks in advance.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480
    I don't know if the geo-mapping databases are free. Therefor, and this might not be a global solution, I'd just have a program that accesses mapquest and parses its output.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    The Google maps API can probably do everything you need.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep Power
    0
    I just finish to figure out. I'm using the google api with simplejson.Here is my code:

    Code:
    import simplejson, urllib
    
    GEOCODE_BASE_URL = 'http://maps.googleapis.com/maps/api/directions/json'
    
    def geocode(start_address,end_address, sensor, **geo_args):
        geo_args.update({
            'origin': start_address,
            'destination': end_address,
            'sensor': sensor  
        })
    
        url = GEOCODE_BASE_URL + '?' + urllib.urlencode(geo_args)
    
        result = simplejson.load(urllib.urlopen(url))
        print url
        result2= result['routes'][0]['legs'][0]['distance']['text']
        print ('This is the driving distance: ' + result2)
        result2=result2.split(" ")
        result2=result2[0]
        print result2
        result2=float(result2)
        result2= float (result2*1.35)
        print round(result2)
    if __name__ == '__main__':
        geocode(start_address="11207",end_address="07410", sensor="false")
    Now I have to figure out how to take the information provide by costumer and how to put it in the website. If you guys can help me with that part.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0
    You are assuming that the trucks drive in a straight line like a bird - Unfortunately they dont, so the miles calculation will only give you a radius of a circle like some of the stores use to calculate their delivery fee. "If you live within 5 miles of the store the delivery fee is 5 dollars, if you live more than 5 and less than 10, it is 10 dollars" - that kind of thing.

    If you need to work out the actual milage the truck will have to cover, and so work out the gas used, you may have to plug into a GPS or route database like they have in your car Garmin device etc
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    25
    Rep Power
    0
    No, that script on the top give you the driving distance not the straight line.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    San Francisco Bay
    Posts
    1,939
    Rep Power
    1313
    Originally Posted by ocpaul20

    If you need to work out the actual milage the truck will have to cover, and so work out the gas used, you may have to plug into a GPS or route database like they have in your car Garmin device etc
    ...or Google maps.

IMN logo majestic logo threadwatch logo seochat tools logo