How to calculate which marker of mapview is closest to me(current location) ?

Hello!

I have developed a map with some markers on it… I wonder how could i find which one of them is more close to my current location ? Is there a documentation or a tutorial for this? Can you address me somewhere to read about that case??

THANKS A LOT!

This return the distance between two locations that are nearby, it works fine unless you have markers all over the world. I used this when I played around with proximity notifications (trigger alerts, timers, notifications based on if the user is within distance of a location).

[lua]

local rad = math.rad

local cos = math.cos

local sqrt = math.sqrt

local earthRadiusInMeters = 6378137

– Equirectangular approximation, works for short distances

function distanceApproximation(lat1, long1, lat2, long2)

  if not lat1 or not long1 or not lat2 or not long2 then

    print(" ===> Missing latitudes and longitudes")

    return

  end 

    local l1, lng1 = rad(lat1), rad(long1)

    local l2, lng2 = rad(lat2), rad(long2)

    local x = (lng2-lng1) * cos((l1+l2)/2)

    local y = (l2-l1)

    local d = sqrt(x * x + y * y) * earthRadiusInMeters

    print(" ===> distanceApproximation: “…d…” meters")

    return d

end

[/lua]

Gives you meters in return:

http://forums.coronalabs.com/topic/37827-calculate-distance-between-points-sort-them-works/

This return the distance between two locations that are nearby, it works fine unless you have markers all over the world. I used this when I played around with proximity notifications (trigger alerts, timers, notifications based on if the user is within distance of a location).

[lua]

local rad = math.rad

local cos = math.cos

local sqrt = math.sqrt

local earthRadiusInMeters = 6378137

– Equirectangular approximation, works for short distances

function distanceApproximation(lat1, long1, lat2, long2)

  if not lat1 or not long1 or not lat2 or not long2 then

    print(" ===> Missing latitudes and longitudes")

    return

  end 

    local l1, lng1 = rad(lat1), rad(long1)

    local l2, lng2 = rad(lat2), rad(long2)

    local x = (lng2-lng1) * cos((l1+l2)/2)

    local y = (l2-l1)

    local d = sqrt(x * x + y * y) * earthRadiusInMeters

    print(" ===> distanceApproximation: “…d…” meters")

    return d

end

[/lua]

Gives you meters in return:

http://forums.coronalabs.com/topic/37827-calculate-distance-between-points-sort-them-works/