Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Calculate Driving Distance

  1. #1
    Join Date
    Nov 2007

    Calculate Driving Distance

    I'm building a website for my client with an e-commerce component. She wants to offer free delivery if the address of the buyer is within 30 driving miles of her home. I know if I go to the URL:


    with A being the starting address and B being the ending address it will return the data in which that information is encoded (as some subset of xml I think). Is there a way to do something like this within the backend so the consumer never sees anything and just save the driving distance number to a JS or PHP variable?

  2. #2
    Join Date
    Nov 2008
    I have no experience of this stuff but I would do something along the lines of:
    - Submit A and B to your server
    - Request the url in php
    - Parse the XML on the server however you want
    - Respond to client with distance or error message

    If its as simple as it sounded there then it shouldn't take too long :-D
    I dont know how you would do that but if I get a minute I might try it out myself :-P

    EDIT: Do what the guy below said :-P
    Last edited by mintedjo; 02-17-2009 at 09:54 AM.

  3. #3
    Join Date
    Feb 2009
    Allentown, Buffalo, NY
    You would do an AJAX call to that url with the addresses, parse the responseXML and get the pull the distance out. If you need a more specific example, let me know and I'll type something up quickly. In the meantime, have a look at

    Scott Prelewicz
    Senior Web Developer
    COMAND Solutions

  4. #4
    Join Date
    Nov 2007
    Thanks guys, conceptually Ajax is what i was thinking, and I have used it extensively but I'm having some issues with how that would work since when you visit the URL it prompts you to save the file; it's not actually a page rather a downloadable file. Scott, if you have time could you explain or give an example. I've also never parsed something out like this.

  5. #5
    Join Date
    Nov 2008
    Remember that for security reasons (that is preventing cross-site scripting attacks) Ajax requests can only be made to URLs of the same protocol, host and port of the page containing the Ajax request. Some browsers might allow arbitrary URLs, but you shouldn't rely on support for this.
    Wouldn't that pose a problem as well?
    Obviously your website wont share the same host as google, or you would be google?

  6. #6
    Join Date
    Feb 2009
    Allentown, Buffalo, NY
    Yes, it would. You will need a proxy script, in php or whatever, that does the work of getting the data for you and returning it to you. I will try to write up an thorough example sometime later today.
    Scott Prelewicz
    Senior Web Developer
    COMAND Solutions

  7. #7
    Join Date
    Nov 2007
    Yeah, i guess that would through a roadblock in things. You guys are great for helping me out

  8. #8
    Join Date
    Nov 2008
    I'm having a very boring day at work so I've done this just to see if it would work the way i first proposed...
    $a = "manchester";
    $b = "liverpool";
    //Create the url
    $myurl = "http://maps.google.com/maps?q=from+{$a}+to+{$b}&output=kml";
    //Open the url
    $f = fopen ($myurl, "r");
    //Read the contents
    $str = stream_get_contents($f);
    //String match the distance
    preg_match("/Distance: ([0-9.-]+)/", $str, $distance);
    echo "$a is ".($distance[1]<30?"less":"more")." than 30miles from $b by car";
    If you set that up on your own server, you could use bog standard ajax that you are already familiar with to send the start place and destination, run it through that code i posted and reply again with bog standard ajax.

    I used a preg_match rather than parse the xml because:
    1) I didnt know how to parse the element containing the distance
    2) It works

    EDIT: I meant to quote this snippet from php.net
    If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.
    Last edited by mintedjo; 02-17-2009 at 10:52 AM.

  9. #9
    Join Date
    Apr 2006
    Parsing the XML would not have worked anyway, because it looks like Distance is only used as CDATA in the <description> tag. Not a separate XML tag itself.

    This will work fine for cfgcjm -- until they change the description.

  10. #10
    Join Date
    Feb 2009
    Allentown, Buffalo, NY
    In this instance, much better to just use preg_match, but as slaughters said, you'll have to keep track of the xml to see if they change the return format. Being google, I'd feel somewhat safe they'll keep it consistant, but obviously there's no guarantee.

    Anyway, here's some client side code (using Prototype):

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
      <TITLE> New Document </TITLE>
      <META NAME="Generator" CONTENT="EditPlus">
      <META NAME="Author" CONTENT="">
      <META NAME="Keywords" CONTENT="">
      <META NAME="Description" CONTENT="">
      <script src="/js/prototype.js"></script>
      function getDistance() {
    		new Ajax.Request('/distanceProxy.php', {
    			parameters: $('distanceForm').serialize(true),
    			onSuccess: function(transport){
    				var response = transport.responseText || "no response text";
    			onFailure: function(){ alert('Something went wrong...') }
    		return false;
    	<form action="" method="post" id="distanceForm" onSubmit="getDistance(); return false;">
    		<input type="text" name="start" id="start">
    		<input type="text" name="end" id="end">
    		<input type="submit" name="submit" id="submit">
    Last edited by ScottPrelewicz; 02-17-2009 at 11:01 AM. Reason: Change code
    Scott Prelewicz
    Senior Web Developer
    COMAND Solutions

  11. #11
    Join Date
    Nov 2008
    Quote Originally Posted by slaughters View Post
    Parsing the XML would not have worked anyway, because it looks like Distance is only used as CDATA in the <description> tag. Not a separate XML tag itself.
    Thats why I didnt know how to parse the element :-D

  12. #12
    Join Date
    Nov 2007
    Ok, houston, we have a problem. It works great with your inputs of manchester and liverpool but i went to change them (because i need to eventually input addresses) I changed them to New York NY and Philadelphia PA and it does not return a result. I altered the code slightly but it works for Manchester and liverpool:

    manchester is 34.3 miles from liverpool by car
    new york ny is miles from philadelphia pa by car

    PHP Code:
    "new york ny";
    $b "philadelphia pa";

    //Create the url
    $myurl "http://maps.google.com/maps?q=from+{$a}+to+{$b}&output=kml";
    //Open the url
    $f fopen ($myurl"r");
    //Read the contents
    $str stream_get_contents($f);
    //String match the distance
    preg_match("/Distance: ([0-9.-]+)/"$str$distance);
    echo "$a is ".$distance[1]." miles from $b by car";
    If i use the url and input
    http://maps.google.com/maps?q=from+n... pa&output=kml
    it works but the code doesn't

    any ideas?

  13. #13
    Join Date
    Nov 2007
    Hmmmmm if i make the inputs:

    $a = "new%20york%20ny";
    $b = "philadelphia%20pa";

    it works!, anyway to automatically escape code a string?

  14. #14
    Join Date
    Feb 2009
    Allentown, Buffalo, NY
    You probably need to format those addresses beter. Use commas before the state and put in zipcodes if you can. I didn't test this, just guessing from past experience.
    Scott Prelewicz
    Senior Web Developer
    COMAND Solutions

  15. #15
    Join Date
    Nov 2008
    If you are using things with spaces in them (i didnt expect that :-P)
    $newA = urlencode($a)
    before you insert the string into the url

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.14022 seconds
  • Memory Usage 3,018KB
  • Queries Executed 15 (?)
More Information
Template Usage (38):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (3)bbcode_code
  • (1)bbcode_php
  • (3)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (15)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (15)postbit
  • (15)postbit_onlinestatus
  • (15)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (72):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates