Later in this chapter, we will add in a feature to allow the app user to add a set of favorite locations. For the moment, we'll just try out the basic functions. Location doesn't work in the simulators; you'll have to try this on a real device:
Get Location
button and a location
field. Make sure that the location
field is as wide as the card window; it will show three long numbers.on mouseUp mobileStartTrackingSensor "location", true put mobileSensorReading("location", false) into field "location" mobileStopTrackingSensor "location" end mouseUp
true
value in the second line is the one that defines a loosely value saying that we don't need the precision of GPS. The false
value in the third line says that we don't need detailed information to be returned.location
field:As you can see, there is very little code needed to read a location! If this was a tracking app, you would need to keep the tracking open and have functions to respond to the change of location messages, but for our app, we just need to know where you are at the time you take a look at your list of reminders.
The numbers that are shown in the location field are the latitude, longitude, and elevation of the position of the device. However, how will we use these numbers…?
The plan is to make your app able to sort your reminders list in order of distance from where you are right now. Let's say you really use this app a lot and have dozens of reminders. The reminder you created about buying some bread may be at the bottom of the list, but if you have assigned the location of the supermarket to that reminder, sorting the list while you're outside the supermarket should bring the shopping list items to the top.
When faced with a problem such as this one, how will you come to know the distance between two points on Earth; Google is a good starting place for you to find that out! It takes very little research and time to find:
http://www.movable-type.co.uk/scripts/latlong.html
The article in this URL discusses the original formula for calculating this and then shows a JavaScript function. If you find it hard to convert the equation into LiveCode handlers, you ought to be able to convert the JavaScript, line by line, into the LiveCode equivalent.
No need to type this in just yet, we'll integrate it later; however, if you want to have a play, put these lines into the stack script:
function distance lat1,lon1,lat2,lon2 put 6371 into r put toRad((lat2-lat1)) into dLat put toRad((lon2-lon1)) into dLon put toRad(lat1) into lat1 put toRad(lat2) into lat2 put sin(dLat/2) * sin(dLat/2) + sin(dLon/2)*sin(dLon/2) * cos(lat1)*cos(lat2) into a put 2*atan2(sqrt(a),sqrt(1-a)) into c put r*c into d return d end distance function toRad pAngle return pAngle/180*PI end toRad
Try this in the message box:
put distance(40,-74,51,0)
This appears as shown in the following screenshot:
As shown in the preceding screenshot, you should see a value of 5645.48065. The two locations are somewhere near New York and London, and that value would be the distance in kilometers between the two along the surface of the Earth.
Examine the screenshot (the one timed "6:53 PM", which precedes the previous screenshot), and given the clue that the building I live in is not much above sea level, which floor do I live on?
Answer: 3
The numbers coming back from the location sensor return as latitude, longitude, and elevation. This would make the elevation on where the device was at that time be about 37.5 meters, which is much too low to be the 40th floor. There is enough information in the screenshot for you to know exactly when it was taken and where on Earth!