How to Calculate Current Location and A specific point/marker/pin in GMaps

Hello and Good day to all,

I, again, need your assistance.

As we’ve progressed in our corona app, we’ve encountered a problem which we have no idea on how to implement, the problems consists of 2 parts. 

First, how to calculate the current location of a user and a specific point/pin/marker?

Second, how to display the number of meters under/beside/top of the user’s symbol in the map?

thank you for your generous help. 

 

Generally you need to know the Lat, Long to plot a point on the map, so you should be able to save that data in a table when you create the point.  As for your current location, you can get the Lat, Long from the GPS API calls.  This might get you started in the right way:  http://docs.coronalabs.com/api/event/location/longitude.html

As for drawing things on the map.  The Corona SDK map object is a “Native” object.  It isn’t done in OpenGL like the rest of Corona SDK APIs are.  When you see a native.* in front of an API call, that indicates that it’s separate from the rest.  Native objects always sit on top of the OpenGL objects, therefore you cannot draw your own items over top of the native objects.  You have two choices:  Draw your information outside of the map area or use the built in map marker’s info bubble to show information.

You can look at the business sample app:  https://github.com/coronalabs/business-app-sample

It has customized map markers with information in the info bubble.

Rob

thanks for the advise, actually here is the code.

function print_r ( t )
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent…"*"…tostring(t))
else
print_r_cache[tostring(t)]=true
–if (type(t)==“table”) then
for pos,val in pairs(t) do
if (type(val)==“table”) then
print(indent…"["…pos…"] => “…tostring(t)…” {")
sub_print_r(val,indent…string.rep(" “,string.len(pos)+8))
print(indent…string.rep(” “,string.len(pos)+6)…”}")
else
print(indent…"["…pos…"] => “…tostring(val))
end
end
–else
–print(indent…tostring(t))
–end
end
end
sub_print_r(t,” ")
end

– Locals
local cos = math.cos
local sin = math.sin
local pi = math.pi
local sqrt = math.sqrt
local min = math.min
local asin = math.asin
local abs = math.abs

– Calculate distance
function distance(from, to)
local distance = 0
local radius = 6367000
local radian = pi / 180
local deltaLatitude = sin(radian * (from.latitude - to.latitude) /2)
local deltaLongitude = sin(radian * (from.longitude - to.longitude) / 2)

local circleDistance = 2 * asin(min(1, sqrt(deltaLatitude * deltaLatitude +
cos(radian * from.latitude) * cos(radian * to.latitude) * deltaLongitude * deltaLongitude)))
distance = abs(radius * circleDistance)
return distance
end
    
– Markers
local markers = {
{ title=‘sasa1’, latitude = 7.07220007, longitude = 125.6208795}, – will become position 3
{ title=‘sasa2’, latitude = 7.0723912, longitude = 125.61704414}, – will become position 1
{ title=‘sasa3’, latitude = 7.07362466, longitude = 125.61977253} – will become position 2
}
    --currentLatitude = curentLocation.latitude
    --currentLongitude = currentLocation.longitude
– Center; could be your current GPS location
centerPoint = {latitude = 7.07525189, longitude = 125.61961413} – new york
–centerPoint = {currentLocation}
–local centerPoint = {myMap:getUserLocation()}

– Set distances in markers
for i, Prop in ipairs(markers) do
    Prop[‘distance’] = distance(centerPoint, markers[i])
end

– Sort by distance
function compare(a,b)
return a[‘distance’] < b[‘distance’]
    
    
end
table.sort(markers, compare)
– Print sorted markers

local newText =
    "Nearest is: " print_r(markers[1])
    native.showAlert( “You Are Here”, nearestText, { “OK” } )
    
end

as you can see the above code, the code is working, the output is properly showing in the corona simulator. The problem is that we want to use the output from the simulator to appear in our googlemap like showing what longlat is the output from the simulator.

Generally you need to know the Lat, Long to plot a point on the map, so you should be able to save that data in a table when you create the point.  As for your current location, you can get the Lat, Long from the GPS API calls.  This might get you started in the right way:  http://docs.coronalabs.com/api/event/location/longitude.html

As for drawing things on the map.  The Corona SDK map object is a “Native” object.  It isn’t done in OpenGL like the rest of Corona SDK APIs are.  When you see a native.* in front of an API call, that indicates that it’s separate from the rest.  Native objects always sit on top of the OpenGL objects, therefore you cannot draw your own items over top of the native objects.  You have two choices:  Draw your information outside of the map area or use the built in map marker’s info bubble to show information.

You can look at the business sample app:  https://github.com/coronalabs/business-app-sample

It has customized map markers with information in the info bubble.

Rob

thanks for the advise, actually here is the code.

function print_r ( t )
local print_r_cache={}
local function sub_print_r(t,indent)
if (print_r_cache[tostring(t)]) then
print(indent…"*"…tostring(t))
else
print_r_cache[tostring(t)]=true
–if (type(t)==“table”) then
for pos,val in pairs(t) do
if (type(val)==“table”) then
print(indent…"["…pos…"] => “…tostring(t)…” {")
sub_print_r(val,indent…string.rep(" “,string.len(pos)+8))
print(indent…string.rep(” “,string.len(pos)+6)…”}")
else
print(indent…"["…pos…"] => “…tostring(val))
end
end
–else
–print(indent…tostring(t))
–end
end
end
sub_print_r(t,” ")
end

– Locals
local cos = math.cos
local sin = math.sin
local pi = math.pi
local sqrt = math.sqrt
local min = math.min
local asin = math.asin
local abs = math.abs

– Calculate distance
function distance(from, to)
local distance = 0
local radius = 6367000
local radian = pi / 180
local deltaLatitude = sin(radian * (from.latitude - to.latitude) /2)
local deltaLongitude = sin(radian * (from.longitude - to.longitude) / 2)

local circleDistance = 2 * asin(min(1, sqrt(deltaLatitude * deltaLatitude +
cos(radian * from.latitude) * cos(radian * to.latitude) * deltaLongitude * deltaLongitude)))
distance = abs(radius * circleDistance)
return distance
end
    
– Markers
local markers = {
{ title=‘sasa1’, latitude = 7.07220007, longitude = 125.6208795}, – will become position 3
{ title=‘sasa2’, latitude = 7.0723912, longitude = 125.61704414}, – will become position 1
{ title=‘sasa3’, latitude = 7.07362466, longitude = 125.61977253} – will become position 2
}
    --currentLatitude = curentLocation.latitude
    --currentLongitude = currentLocation.longitude
– Center; could be your current GPS location
centerPoint = {latitude = 7.07525189, longitude = 125.61961413} – new york
–centerPoint = {currentLocation}
–local centerPoint = {myMap:getUserLocation()}

– Set distances in markers
for i, Prop in ipairs(markers) do
    Prop[‘distance’] = distance(centerPoint, markers[i])
end

– Sort by distance
function compare(a,b)
return a[‘distance’] < b[‘distance’]
    
    
end
table.sort(markers, compare)
– Print sorted markers

local newText =
    "Nearest is: " print_r(markers[1])
    native.showAlert( “You Are Here”, nearestText, { “OK” } )
    
end

as you can see the above code, the code is working, the output is properly showing in the corona simulator. The problem is that we want to use the output from the simulator to appear in our googlemap like showing what longlat is the output from the simulator.