I am currently developing an app that makes you run a certain distance in 30 seconds. I have code to gather the starting longitude and latitude, but I got stuck with comparing them with the current latitude and longitude. Is there any way to check if the distance they travelled is at least a certain number? I would like to have them travel a certain radius so no matter which direction they run in, and as long as they have ran the target distance, they will complete the game. Any ideas?
You need to calculate the distance. Most people use the Haversine formula for that. It’s a pain to set up but it works great.
See this: Calculate distance travelled
Note that that will calculate the distance between two points “as the crow flies.” If you want actual road distance, you’d probably need to use an API call with the Google Maps API.
Thanks! The only problem is I don’t really know how to make it apply to what I’m doing. My idea is once a player hits a button to start, then everything activates. Any way you could help me?
What kind of help do you need?
I recommend first writing an algorithm that contains the main steps you’re going to need. So when you say “then everything activates”, that should be broken down into maybe 4-5 separate steps. That way you can ask questions about specific steps that you’re having trouble with. You started to do that in your first post so it’s just getting a little more specific and putting things in a specific order (e.g. numbered steps).
I have it so when the “game starts” screen opens, it gathers the current latitude and longitude and stores them in their own variables. I then have a function called “check location” where the comparing will happen.