Location Detection

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
williamsc84
Posts: 7
Joined: Sun Aug 06, 2017 1:54 pm

Location Detection

Post by williamsc84 » Fri Aug 25, 2017 11:59 am

Hi everyone.

I have a question about detecting current locations and also highlighting locations nearest my location.

I think I actually have two questions here but I will start with just 1 and see where I can get to :)

I have a list of locations around the UK, for each location I have it's name, it's latitude and it's longitude

I have a card on my stack that currently shows every single location, just by name in a list field and the user can either search that field using my search box or scroll through the field to find the right location and make their selection.

What I would like to try and do is that each time the card is loaded, by default enter the three nearest locations to their current location at the top of my list (or in a different field on top of my list)

Can anyone help point me in the right direction here? I feel like there is probably a way to identify the users current location (on mobile), and to then identify the three closest locations to them by searching through the latitude and longitude for each location and then displaying them at the top of my field. I am just not quite sure where to start with all of this. Any tips / advice?

Many Thanks

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Location Detection

Post by jmburnod » Fri Aug 25, 2017 10:26 pm

Hi William,
If i understand you correctly
The function below which return the distance between two objects using locations should be useful (Thanks Hermann)
I think you may use it to get the distance between user's location and each location of your list in a loop.

Code: Select all

--•• the distance between two controls
function DistanceBetweenTwoObj pObj1,pObj2 
   put the loc of control pObj1 into tLoc1
   put the loc of control id pObj2 into tLoc2
   put abs(item 1 of tLoc1-item 1 of tLoc2) into difH1
   put abs(item 2 of tLoc1-item 2 of tLoc2) into difV1
   put sqrt(difH1^2 + difV1^2) into tL
   put round(tL) into hypoLength
   return hypoLength
end DistanceBetweenTwoObj
Best regards
Jean-Marc
https://alternatic.ch

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9665
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Location Detection

Post by dunbarx » Sat Aug 26, 2017 2:24 am

There was a thread about three years ago that dealt with distances between two points on the globe. ("Math functions in LiveCode"_

Here is a handler that might do. You enter latitude and longitude;

Code: Select all

on mouseUp
   put toRadians(item 1 of fld "startPoint") into lat1
   put toRadians(item 2 of fld "startPoint") into long1
   put toRadians(item 1 of fld "endPoint") into lat2
   put toRadians(item 2 of fld "endPoint") into long2
   put abs(long1 - long2) into d2
   put 3959 * aCos(sin(lat1) * sin(lat2) + cos(lat1) * cos(lat2) * cos(d2)) into fld "distance"
end mouseUp

function toRadians var
   return var/(180/pi)
end toRadians

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Location Detection

Post by [-hh] » Tue Aug 29, 2017 8:53 pm

Hi all.

Two remarks.
  1. Craig's formula yield miles by using the factor earthRadius=3961 (mi) for a result in miles.
    We may use also the factor earthRadius=6371 (km) for a result in kilometers.
    These are the values recommended by the International Union of Geodesy and Geophysics.
  2. Minor: For frequent usage it may be better to multiply with a constant rather than using a function for the degrees-to-radians conversion.
So let me add this slightly modified script.

Code: Select all

#### Compute the distance between two points given by GEO-locations
constant pi0=0.017453293 --> = pi/180
constant earthRadius=6371 --> average earthRadius (km) --> result is km
-- constant earthRadius= 3961 --> average earthRadius (mi) --> result is miles

## laX and loX are latitude and longitude of location X as decimal numbers
function lawOfCosineDistance la1,lo1,la2,lo2
  put la1*pi0 into l1
  put la2*pi0 into l2
  put (lo2-lo1)*pi0 into dlo
  put acos(sin(l1)*sin(l2)+cos(l1)*cos(l2)*cos(dlo)) into x
  return format("%.3f", earthRadius*x) -- "accuracy" down to 1 meter
end lawOfCosineDistance
shiftLock happens

williamsc84
Posts: 7
Joined: Sun Aug 06, 2017 1:54 pm

Re: Location Detection

Post by williamsc84 » Thu Aug 31, 2017 6:39 pm

Thanks everyone. This is giving me plenty to try out. I will let you know how it goes.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”