Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4ppc (Windows Mobile) > Code Samples & Tips
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Code Samples & Tips Share your recent discoveries and ideas with other users.

Calculating distance

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-06-2008, 03:56 PM
Newbie
 
Join Date: Mar 2008
Posts: 6
Post Calculating distance

Here is some sample code to calculate distance between two points:

Code:
Sub getDistanceKM(lat1, long1, lat2, long2)

    DEGREES_TO_RADIANS = (
cPI / 180.0)
    EARTH_RADIUS = 
6371.0
    
    rlat1 = DEGREES_TO_RADIANS * lat1
    rlong1 = DEGREES_TO_RADIANS * long1
    rlat2 = DEGREES_TO_RADIANS * lat2
    rlong2 = DEGREES_TO_RADIANS * long2
    
    
'    There is no real reason To break this lot into 
    '    4 statements but I just feel it's a little more 
    '    readable.
    p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
    p2 = 
Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
    p3 = 
Sin(rlat1) * Sin(rlat2)

    ret = p1 + p2 + p3
    
If ret = 1 Then Return 0
    
    ret = 
ACos(ret)
    ret = ret * EARTH_RADIUS
    
    
Return ret
    
End Sub
Can we add this to the gps.dll?
Reply With Quote
  #2 (permalink)  
Old 05-06-2008, 04:05 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Generally speaking, libraries should only include functions that it isn't possible to implement from code.
Reply With Quote
  #3 (permalink)  
Old 05-06-2008, 04:09 PM
Newbie
 
Join Date: Mar 2008
Posts: 6
Default

Quote:
Originally Posted by Erel View Post
Generally speaking, libraries should only include functions that it isn't possible to implement from code.
I was thinking of the processing power. Would complex calculations not run faster in a library?

Just a thought - not based on fact.
Reply With Quote
  #4 (permalink)  
Old 05-06-2008, 04:14 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Calculations will run faster in a library but the difference should not be too significant.
Reply With Quote
  #5 (permalink)  
Old 05-15-2008, 07:11 PM
Junior Member
 
Join Date: Apr 2008
Location: FRANCE
Posts: 13
Default

Hi there,

Just a little correction for your code

" If ret >1 Then Return 0 "

My tests give me sometime 1.0000000002 :-)

Bye,

Bruno.
Reply With Quote
  #6 (permalink)  
Old 01-08-2009, 02:14 PM
MM2forever's Avatar
Senior Member
 
Join Date: Jun 2007
Location: Germany
Posts: 103
Send a message via ICQ to MM2forever Send a message via MSN to MM2forever Send a message via Skype™ to MM2forever
Default

Am I right assuming that a decimal lat/lon input is required?
I think the other (dddmm.mm) format is giving me false distances...
__________________
Regards, Christian


Last edited by MM2forever : 01-08-2009 at 02:24 PM.
Reply With Quote
  #7 (permalink)  
Old 01-08-2009, 03:18 PM
klaus's Avatar
Basic4ppc Expert
 
Join Date: Oct 2007
Location: Fully, Switzerland
Posts: 3,827
Awards Showcase
Forum Contributer Beta Tester Competition Winner 
Total Awards: 3
Default

Yes, the lat and long values must be in decimal format.

You can have a look here for other formulas.
Great-circle distance - Wikipedia, the free encyclopedia

Best regards.
__________________
Klaus
Switzerland
Reply With Quote
  #8 (permalink)  
Old 01-09-2009, 02:04 PM
MM2forever's Avatar
Senior Member
 
Join Date: Jun 2007
Location: Germany
Posts: 103
Send a message via ICQ to MM2forever Send a message via MSN to MM2forever Send a message via Skype™ to MM2forever
Default

Thanks for the info klaus,
I was already searching for the mathematical/physical/geographical basis of this routine, since for me, using existing routines is great, but i like to understand what im using

edit://
Quote:
Originally Posted by Erel View Post
Generally speaking, libraries should only include functions that it isn't possible to implement from code.
arent all functions of the gps lib (at least in the version i have which i guess is the latest/only one) replaceable by string operations that can be done with the b4p standart string functions set?
__________________
Regards, Christian


Last edited by MM2forever : 01-09-2009 at 04:41 PM. Reason: additional question/ idea
Reply With Quote
  #9 (permalink)  
Old 01-10-2009, 09:49 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 13,162
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Quote:
arent all functions of the gps lib (at least in the version i have which i guess is the latest/only one) replaceable by string operations that can be done with the b4p standart string functions set?
Yes.
Every statement has one or two exceptions

If I were to write the GPS library today I would have written it as a module instead of a library. That would have made it easier to enhance it and parse other NMEA sentences.
The GPS library was written before Basic4ppc V6.50 and therefore modules weren't available at that time.
__________________
Basic4android documentation
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calculating a textbox's text? N1c0_ds Questions (Windows Mobile) 2 10-04-2008 02:24 PM
calculating differences in time sunnyboyj Questions (Windows Mobile) 1 02-07-2008 08:36 PM
Distance-test on UTM and LAT-LON Put Claude Questions (Windows Mobile) 1 10-16-2007 02:06 PM
Distance between UTM zones jerryjukem Questions (Windows Mobile) 3 10-10-2007 11:19 AM
Distance Calculator PatrikL Share Your Creations 7 04-30-2007 06:15 PM


All times are GMT. The time now is 12:57 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0