You need to activate the location sensor.
You will get the user’s coordinates through it.
There is no direct function for calculating the distance between points. you can write the calculation formula yourself (look at Google for this and get acquainted with the calculation methods) or use additional extensions that perform these calculations. For example:
I don’t understand what you mean by that. it doesn’t matter where you store your data. and this is generally about geolocation.You’re asking how to measure the distance between points…
Let’s put it in place. because I don’t understand the essence of your problem anymore. You asked how to find out where the user is, and I’m telling you to connect a location sensor to your app. it will give you the coordinates of the user. 2. you are asking how to calculate the distance between points - use the extension where, among the functions, insert the coordinates of one and the second point and get the distance between them in meters. if you are not familiar with the editor’s toolkit or working with additional extensions, then this is a completely different topic.
there is no point in using 14 decimal places it is enough to use 5 characters after the dot (the maximum possible accuracy of the definition physically) or at least 7 characters , but not 14
It would really help if you provided a screenshot your relevant blocks, so we can see what you are trying to do, and where the problem may be.
To get an image of your blocks, right click in the Blocks Editor and select “Download Blocks as Image”. You might want to use an image editor to crop etc. if required. Then post it here in the community.
@Taifun I never created something like this before, so all I need is your guidance, I can tell they the same method I was asking, @Still-learning I guess you can know get what I was saying, so please help
I want to display the distance between the user’s current location and a location stored in a database (for example, an item location). I want the distance to be shown in actual units like kilometers or meters.
I’m not using the when LocationSensor.LocationChanged block, so instead I’ll trigger the distance calculation manually (for example, using a button click or screen initialize).
Here’s the process I’m aiming for:
Get the user’s current latitude and longitude using LocationSensor1.CurrentLatitude and CurrentLongitude.
Get the item’s location (latitude & longitude) from the database.
Use LocationSensor1.DistanceToPoint(lat, lng) to calculate the distance.
Convert and display the result as meters or kilometers based on the value.
If anyone has a better way of doing this without relying on LocationChanged, or if you see any issue with this approach, I’d appreciate your suggestions!
➤ Use a Math-based procedure to calculate distance manually.
You can implement the Haversine formula in Kodular using the built-in math blocks.
Haversine Formula in Pseudocode
function distance(lat1, lon1, lat2, lon2):
R = 6371000 // Earth radius in meters
dLat = radians(lat2 - lat1)
dLon = radians(lon2 - lon1)
a = sin(dLat/2)^2 + cos(radians(lat1)) * cos(radians(lat2)) * sin(dLon/2)^2
c = 2 * atan2(sqrt(a), sqrt(1-a))
distance = R * c
return distance
How to Build This in Kodular
You’ll need to:
Create a procedure called CalculateDistance with 4 inputs: lat1, lon1, lat2, lon2.
Inside the procedure:
Use math blocks to implement the formula above.
Return the distance (in meters).
Example of What to Do in Blocks (Described Step-by-Step):
Step 1: Setup a procedure block
Name it CalculateDistance
Inputs: lat1, lon1, lat2, lon2
Step 2: Inside the procedure:
Set R to 6371000
Set dLat to radians(lat2 - lat1)
Set dLon to radians(lon2 - lon1)
Set a to (sin(dLat / 2))^2
+ cos(radians(lat1)) * cos(radians(lat2))
* (sin(dLon / 2))^2
Set c to 2 * atan2(sqrt(a), sqrt(1 - a))
Set distance to R * c
Return distance
You can implement this step-by-step with Kodular’s Math blocks (sin, cos, atan2, sqrt, radians, etc.).
Example Usage
In your Button Click event:
Set userLat to LocationSensor1.CurrentLatitude
Set userLng to LocationSensor1.CurrentLongitude
Set itemLat and itemLng from database
Call CalculateDistance with (userLat, userLng, itemLat, itemLng)
→ Result is in meters
→ If < 1000, show in meters; else convert to kilometers