Time for action – trying out native location tracking

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:

  1. Use the test-rig stack from the preceding steps and add a 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.
  2. Set the script of the button to the following:
    on mouseUp
      mobileStartTrackingSensor "location", true
      put mobileSensorReading("location", false) into field "location"
      mobileStopTrackingSensor "location"
    end mouseUp
  3. The 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.
  4. Go to Standalone Application Settings and choose your target device as iOS or Android.
  5. For iOS, set the Display Name, Internal App ID, and Profile. Choose your device and SDK version):
    Time for action – trying out native location tracking
  6. Also, in the iOS Requirements and Restrictions section, select Location Services and GPS as required. In the app, you will also have to accept Location Services when iOS prompts for it.
  7. For Android, set the Label, Identifier, and Minimum Android Version fields:
    Time for action – trying out native location tracking
  8. Additionally, in the Application Permissions part of the Android settings, make sure that you have asked permission to get the Coarse Location permission:
    Time for action – trying out native location tracking
  9. Select Save Standalone Application and install the app onto your device. Follow the description given in Chapter 2, Getting Started with LiveCode Mobile if you need a reminder on how to do that!
  10. In the app, try the Pick Date and Pick Time buttons to see how they bring up the native controls and then click on the Get Location button. Three long numbers should appear in the location field:
    Time for action – trying out native location tracking

What just happened?

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

Calculating the distance between two points on the Earth

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:

Calculating the distance between two points on the Earth

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.

Pop quiz – what floor is my apartment on?

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?

  1. 40th floor
  2. 73 floors below ground
  3. 11th floor
  4. I'm homeless

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!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.103.8