How to calculate distance between user's location and a fixed coordinate?

Hi everyone,

I’m trying to build an app where I want to calculate the distance between the user’s current location and a fixed point with these coordinates:

Latitude: -15.801963919634241  
Longitude: 35.03044490306823

I would like to:

  1. Get the user’s current location.
  2. Calculate the distance in kilometers or meters between the user and the fixed coordinate.
  3. Display that distance in a label.

I’m not sure how to do the math or use the location sensor blocks correctly for this. Can anyone guide me or share example blocks?

Thanks in advance!

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:

Oky, but i will use the long and atut from database for each item location, the convert it to Km or meters. Thats where i stucks

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…

Am bulding eccomece app so i want display distance between user and items,

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.

Let me try with the extension will give you feedback

use this extension after getting co ordinates from the user

Fix the 1 for static 2 for user co ordinate

DistanceCalc : Calculate distance between two places from coordinates i have used this logical code only in my 4yrs old app

there is no point in using 14 decimal places :slight_smile: 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 :wink:

Am trying but i don’t archive it can you share aia file if possible please

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

no need. If you listen to the above screenshot it is well clear that what you should do.

I have been trying to make it as you did but still the same, am on my way home I will show you results from companion.

Also I guess this will work with location change?

@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​:cry:

My aim is to make it like this :backhand_index_pointing_down::backhand_index_pointing_down::backhand_index_pointing_down::backhand_index_pointing_down:

Lat1 should be globsl lat
Long1 should be global long
Lat2 should be lat from the event
Long2 should be long from the event
Taifun

Oky @Taifun if make it as you said, i will can get this result ? How to calculate distance between user's location and a fixed coordinate? - #15 by Ibrahim_Jamar

Because i want to display for each viewed item, as the post above

@Taifun and @Still-learning i think i have explain it little understandable

Here’s what I want to achieve:

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!

Thanks!

try this one. If u have time.

:white_check_mark: What You Can Do in Kodular Instead

➤ Use a Math-based procedure to calculate distance manually.

You can implement the Haversine formula in Kodular using the built-in math blocks.


:straight_ruler: 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

:brick: How to Build This in Kodular

You’ll need to:

  1. Create a procedure called CalculateDistance with 4 inputs: lat1, lon1, lat2, lon2.
  2. Inside the procedure:
  • Use math blocks to implement the formula above.
  • Return the distance (in meters).

:white_check_mark: 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.).


:bullseye: 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

Sound great

Thanks @SyntaxCore i will try it and give you feedback