Go Back   vb.org Archive > Community Discussions > Modification Requests/Questions (Unpaid)
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 08-22-2007, 07:04 AM
iBaker's Avatar
iBaker iBaker is offline
 
Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 152
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Distance Calculator between two places

Hi
I have the code for a page that calculates the distance as the bird flies between two different places but it is from an asp page. It uses latitude and longitude.

Is there anyone who would like to take the code and convert it to an addon page for use in vBulletin - if so it would be greatly appreciated.

The code is:
Code:
ASP Code,  commented:
 You?ll need to retrieve your start/finish  locations however you wish (recordset from DB), pull your D:M:S data for each  location, and feed into variables listed below.
 If using another language such as PHP, most  of the calculations will remain the same, though there will be differences in  syntax of course.
  
 <%
 'Calculate the distance between the two  selected airports
 Dim lat1, lat2, lon1,  lon2
 Dim lat1D, lat1M, lat1S 'D:M:S values need  to be converted to decimal degrees
 Dim lat2D, lat2M,  lat2S
 Dim lon1D, lon1M,  lon1S
 Dim lon2D, lon2M,  lon2S
 Dim Lat1Dir, Lat2Dir, Lon1Dir,  Lon2Dir
  
 ?rsStart= my recordset which retrieves the  start location/airport
  
 If rsStart.Fields.Item("LatDir").Value =  "N" then
             'do  nothing
              else
 Lat1Dir="-" 'south is  negative
 End If
 If rsStart.Fields.Item("LonDir").Value =  "E" then
             'do  nothing
              else
 Lon1Dir="-" 'west is  negative
 End If
  
 lat1D=rsStart.Fields.Item("LatDeg").Value
 lat1M=rsStart.Fields.Item("LatMin").Value
 lat1S=rsStart.Fields.Item("LatSec").Value
  
 'convert Lat1 to  Decimal
 If Lat1Dir>"" then 'if there is an  operator, then insert it
             Lat1= "-" & Lat1D +  (Lat1M/60) + (Lat1S/3600) 'we should have a decimal number  now
              else
             Lat1=Lat1D + (Lat1M/60) +  (Lat1S/3600)
 end if
  
  
  
 'now get Lon1
  
 lon1D=rsStart.Fields.Item("LonDeg").Value
 lon1M=rsStart.Fields.Item("LonMin").Value
 lon1S=rsStart.Fields.Item("LonSec").Value
  
 'convert Lat1 to  Decimal
 If Lon1Dir > "" then 'if there is an  operator, then insert it
             Lon1= "-" & Lon1D +  (Lon1M/60) + (Lon1S/3600) 'we should have a decimal number  now
              else
             Lon1=Lon1D + (Lon1M/60) +  (Lon1S/3600)
 end if
  
  
  
  
 ?rsDest is my recordset for the destination  location/airport
  
 'We have Lat1 and Lon1 now repeat for  2
 If rsDest.Fields.Item("LatDir").Value = "N"  then
             'do  nothing
              else
 Lat2Dir="-" 'south is  negative
 End If
 If rsDest.Fields.Item("LonDir").Value = "E"  then
             'do  nothing
              else
 Lon2Dir="-" 'west is  negative
 End If
  
 lat2D=rsDest.Fields.Item("LatDeg").Value
 lat2M=rsDest.Fields.Item("LatMin").Value
 lat2S=rsDest.Fields.Item("LatSec").Value
  
 'convert Lat2 to  Decimal
 If Lat2Dir>"" then 'if there is an  operator, then insert it
             Lat2= "-" & Lat2D +  (Lat2M/60) + (Lat2S/3600) 'we should have a decimal number  now
              else
             Lat2=Lat2D + (Lat2M/60) +  (Lat2S/3600)
 end if
  
  
  
 'now get Lon1
  
 lon2D=rsDest.Fields.Item("LonDeg").Value
 lon2M=rsDest.Fields.Item("LonMin").Value
 lon2S=rsDest.Fields.Item("LonSec").Value
  
 'convert Lat2 to  Decimal
 If Lon2Dir > "" then 'if there is an  operator, then insert it
             Lon2= "-" & Lon2D +  (Lon2M/60) + (Lon2S/3600) 'we should have a decimal number  now
              else
             Lon2=Lon2D + (Lon2M/60) +  (Lon2S/3600)
 end if
  
  
 ?DEBUG
 '            response.write(lat1)&  "<br>"
 '            response.write(lon1)&  "<br>"
 '            response.write(lat2)&  "<br>"
 '            response.write(lon2)&  "<br>"
  
  
  
  
 'distance=2*  ASIN(sqr((sin((lat1-lat2)/2))^2 +  cos(lat1)*cos(lat2)*(sin((lon1-lon2)/2))^2))
 'distance=acos(sin(lat1)*sin(lat2)+cos(lat1)*cos(lat2)*cos(lon1-lon2))
  
 %>
 <%
    
     Const pi =  3.14159265358979323846
     function distance(lat1, lon1, lat2,  lon2, unit)
     Dim theta,  dist
     theta = lon1 -  lon2
     dist = sin(deg2rad(lat1)) *  sin(deg2rad(lat2)) + cos(deg2rad(lat1)) * cos(deg2rad(lat2)) *  cos(deg2rad(theta))
     dist =  acos(dist)
     dist =  rad2deg(dist)
     distance = dist * 60 *  1.1515
     Select Case  ucase(unit)
     Case "K"
     distance = distance *  1.609344
     Case "N"
     distance = distance *  0.8684
     End Select
     End function  
      ':::::::::::::::::::::::::::::::::::::::
     '      :::::::::::::::::::::::::::::::
     '::: This function get the arccos  functi
     '     on from arctan  function:::
      ':::::::::::::::::::::::::::::::::::::::
     '      :::::::::::::::::::::::::::::::
     function  acos(rad)
     if Abs(rad) <> 1  Then
     acos = pi/2 - Atn(rad / Sqr(1 - rad *  rad))
     ElseIf rad = -1  Then
     acos = pi
     End if
     End  function
      ':::::::::::::::::::::::::::::::::::::::
     '      :::::::::::::::::::::::::::::::
     '::: This function converts decimal  degr
     '     ees to radians  :::
      ':::::::::::::::::::::::::::::::::::::::
     '      :::::::::::::::::::::::::::::::
     function  deg2rad(Deg)
             deg2rad = cdbl(Deg * pi /  180)
     End  function
      ':::::::::::::::::::::::::::::::::::::::
     '      :::::::::::::::::::::::::::::::
     '::: This function converts radians to  d
     '     ecimal degrees  :::
      ':::::::::::::::::::::::::::::::::::::::
     '      :::::::::::::::::::::::::::::::
     function  rad2deg(Rad)
             rad2deg = cdbl(Rad * 180 /  pi)
     End  function
 ?DEBUG
  '   response.write distance(lat1, lon1,  lat2, lon2, "M") & " Miles<BR>"
  '   response.write distance(lat1, lon1,  lat2, lon2, "K") & " Kilometers<BR>"
  '   response.write distance(lat1, lon1,  lat2, lon2, "N") & " Nautical Miles<BR>"
 ?d = result, using formatnumber call to  ensure consitency of displayed variable.
  d = FormatNumber(distance(lat1, lon1,  lat2, lon2, "N"),2)
  
     %>
Reply With Quote
  #2  
Old 08-22-2007, 11:41 AM
Andy Andy is offline
 
Join Date: Sep 2003
Location: San Francisco
Posts: 138
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Here you go...

PHP Code:
<?php
/********************************

Purpose: To calculate the distance between
two points on a map

$lat1 = latitude of first point
$lng1 = longitude of first point
$lat2 = latitude of second point
$lng2 = longitude of second point

********************************/

$lat1 37.391436;
$lng1 = -121.995964;
$lat2 37.385980;
$lng2 = -121.976737;

$theta $lng1 $lng2
$distance sin(deg2rad($lat1)) * sin(deg2rad($lat2)) +  cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta)); 
$distance acos($distance); 
$distance rad2deg($distance); 
$miles $distance 60 1.1515;
$kilometers $miles 1.609344;
$nautical $miles 0.8684;

echo 
$miles;
?>
Reply With Quote
  #3  
Old 08-22-2007, 11:47 AM
stonyarc stonyarc is offline
 
Join Date: Aug 2005
Location: Leuven (Belgium)
Posts: 930
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I've written something similar for my googlemap app

http://www.xboxlivenation.com/community/gamerprox.php


This will be included in the next release.
Reply With Quote
  #4  
Old 08-22-2007, 09:43 PM
iBaker's Avatar
iBaker iBaker is offline
 
Join Date: Oct 2006
Location: Melbourne, Australia
Posts: 152
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks Guys!
I will see what I can do with it - I am not a coder but learn thru playing around although this one I think is going to be a big ask - especially creating a variable departure and destination lat/Long points in two drop down boxes fed by a table that contains all the places for the user to select from.

Stony - You do brilliant work and having something like this incorporated into your google maps would be great but I am desperatly waiting for the security hole to be fixed in the GoogleMap edition (not the member edition)

Thanks again - your help is greatly appreciated!
Reply With Quote
  #5  
Old 08-23-2007, 10:10 AM
stonyarc stonyarc is offline
 
Join Date: Aug 2005
Location: Leuven (Belgium)
Posts: 930
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I'm completely remaking that one
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:03 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.05483 seconds
  • Memory Usage 2,213KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (5)post_thanks_box
  • (5)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (5)post_thanks_postbit_info
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete