vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   Modification Requests/Questions (Unpaid) (https://vborg.vbsupport.ru/forumdisplay.php?f=112)
-   -   Distance Calculator between two places (https://vborg.vbsupport.ru/showthread.php?t=155916)

iBaker 08-22-2007 07:04 AM

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)
 
    %>


Andy 08-22-2007 11:41 AM

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;
?>


stonyarc 08-22-2007 11:47 AM

I've written something similar for my googlemap app

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


This will be included in the next release.

iBaker 08-22-2007 09:43 PM

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!

stonyarc 08-23-2007 10:10 AM

I'm completely remaking that one


All times are GMT. The time now is 02:19 AM.

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.01006 seconds
  • Memory Usage 1,751KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)bbcode_code_printable
  • (1)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (5)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete