How can I draw heading line from my current location to a specific destination ?

Hi All,

Can someone advice what is the best way to implement app that will help user to see the direction for specific place. it like compass with arrow to show that place…

Can this done by map… compass or what?

I appreciate any advice or samples.

Thanks

Abdul

Well you can’t draw on top of native.newMap() if that’s your intent.  But you can have a compass that points in the direction you need to go to.  We did a tutorial not too long ago about using latitude and longitude to get an angle and distance.

https://coronalabs.com/blog/2015/01/06/tutorial-calculating-the-distance-between-map-points/

Even though it’s using map points, it’s doing so as a pair of Latitude/Longitude values which you do not need native.newMapView to get.  The GPS event can give you those points to work with and you of course can get them in various databases.

Rob

thanks Rob… i am trying to make that work… so far I am able to find the distance by the following code… i am still thinking how can I can make arrow pointing to the desired place …

[lua]

local point1 = {} –  my location coordinates… This point is to find by GPS event in the locationHandler 

local point2 = {} – this is the destination … it is always fixed… i want to get the arrow point to it.

– 21.452763, 39.813573

point2.latitude = 21.45

point2.longitude = 39.81

local distanceBetween  – function to find distances between the two porints


function distanceBetween( point1, point2 )

    local dX = point2.longitude - point1.longitude

    local dY = point2.latitude - point1.latitude

    local distance = math.sqrt( ( dX^2 ) + ( dY^2 ) )

    return distance 

end

-----  variabel to show the distance between  my location and 

local distance = display.newText( “-”, 100, 50, native.systemFont, 16 )

----- handler for location handler

local locationHandler = function( event )

    – Check for error (user may have turned off location services)

    if ( event.errorCode ) then

        native.showAlert( “GPS Location Error”, event.errorMessage, {“OK”} )

        print( "Location error: " … tostring( event.errorMessage ) )

    else

    

        

        point1.latitude = event.latitude – set my lat 

        point1.longitude = event.longitude – set my long

        local  x = distanceBetween(point1,point2) – find differnace…

        local diff = string.format( ‘%.1f’, x )  – format it to one decimal 

        distance.text = diff  

    end

end


– Activate location listener

Runtime:addEventListener( “location”, locationHandler )

[/lua]

:slight_smile:  i am welcoming any hints for the rest of the idea.

thx

Put in some more prints?  What are you seeing in the device’s console log?   You are testing on a device right?

Rob

Yes Rob, 

i am testing in a device ( android now) …

i have added also the logic to calculate the angle as shown in in the new code below … but still I am not sure if it is right and also I dont know how can show arrow from my location or center of the device ( assuming my location at the center ) to the specific location. 

[lua]

local point1 = {} –  my location coordinates… This point is to find by GPS event in the locationHandler 

local arrow

local point2 = {} – this is the destination … it is always fixed… i want to get the arrow point to it.

– 21.452763, 39.813573

point2.latitude = 21.45

point2.longitude = 39.81

local distanceBetween  – function to find distances between the two porints


function distanceBetween( point1, point2 )

    local dX = point2.longitude - point1.longitude

    local dY = point2.latitude - point1.latitude

    local distance = math.sqrt( ( dX^2 ) + ( dY^2 ) )

    return distance 

end

---- draw line 

local line 

local function drawLine(point1,point2) 

    — this a try to draw line or direction between the two points.

    line = display.newLine( point1.latitude,point1.longitude,point2.latitude,point2.longitude  )

    line:setStrokeColor( 1, 0, 0, 1 )

    line.strokeWidth = 4

    — this is to calculate the angle between the two points.

   – based on this thread : http://stackoverflow.com/questions/7586063/how-to-calculate-the-angle-between-a-line-and-the-horizontal-axis

    local dLongitude = point2.longitude - point1.longitude

    local dLatitude = point2.latitude - point1.latitude

     – Localize some math calls

    local sin = math.sin

    local cos = math.cos

    local sqrt = math.sqrt

    local atan2 = math.atan2

    local PI = math.pi

    local angleInDegrees = atan2(dLongitude, dLatitude) * 180 / PI

    print("angleInDegrees = ", angleInDegrees)

    --arrow.rotation = angleInDegrees

end

-----  variabel to show the distance between  my location and 

local distance = display.newText( “-”, 100, 50, native.systemFont, 16 )

----- handler for location handler

local locationHandler = function( event )

    – Check for error (user may have turned off location services)

    if ( event.errorCode ) then

        native.showAlert( “GPS Location Error”, event.errorMessage, {“OK”} )

        print( "Location error: " … tostring( event.errorMessage ) )

    else

    

        

        point1.latitude = event.latitude – set my lat 

        print ("point1.latitude = " … point1.latitude)

        

        point1.longitude = event.longitude – set my long

        print ("point1.longitude = " … point1.longitude)

        print(point1.latitude,point1.longitude)

        local  x = distanceBetween(point1,point2) – find differnace…

        local diff = string.format( ‘%.1f’, x )  – format it to one decimal 

        print (" the distance is " …diff)

        distance.text = diff  

        drawLine(point1,point2)

    end

end


– Activate location listener

Runtime:addEventListener( “location”, locationHandler )

[/lua]

here is the result from console

I/Corona  (32133): point1.latitude = 23.616665470107

I/Corona  (32133): point1.longitude = 58.093582024209

I/Corona  (32133): 23.616665470107 58.093582024209

I/Corona  (32133):  the distance is 18.4

I/Corona  (32133): angleInDegrees = -96.758223300233

I/Corona  (32133): point1.latitude = 23.616665470107

I/Corona  (32133): point1.longitude = 58.093582024209

I/Corona  (32133): 23.616665470107 58.093582024209

I/Corona  (32133):  the distance is 18.4

I/Corona  (32133): angleInDegrees = -96.758223300233

 

 

 

The above result does not look right… also i was not able to draw arrow… also i need the  location to adjust when the user move the device around … it should always be showning the arrow to the right direction…

 

 

thanks

 

local function updateCompass( event ) --print( "Compass magnetic: " .. event.magnetic ) compassHeading = event.magnetic if compassHeading == -1 then print("no heading") return end local deltaAngle = math\_floor(angle - compassHeading) if deltaAngle \< 0 then deltaAngle = deltaAngle + 360 end print("angle:", angle, "compass", compassHeading, "deltaAngle:", deltaAngle, compassHeading - angle) compassPointer.rotation = (deltaAngle) end local function angleBetween( point1, point2 ) local angle = ( math\_deg( math\_atan2( point2.y - point1.y, point2.x - point1.x ) ) + 90 ) return angle % 360 end angle = angleBetween( car, me )

This is code from my Find My Car app that I released a few months ago.   Though I was using it the other day and it was about 90 degrees off (my compass was uncalibrated through).

Now my compass does not point North like a typical compass, but instead points the direction you should be going.

Rob

Thanks Rob for sharing your code… this is appreciated… i have more queries if you dont mind :

1- i still not getting the point of rotating an arrow. where is that happened ? does your code directly change the compass arrow in the build in compass or how that is done… ?

2- how can we make the event running all time and keep the arrow in the right direction even user rotate his device…

3- how did you get the current location of user ? do you use GPS, maps or how  ? does that mean it will not work for all devices ?

4- what does this line do :  compassPointer.rotation = (deltaAngle)

for testing, i have two static points as shown below with your code :

[lua]

local point1 = {} –  my location coordinates… This point is to find by GPS event in the locationHandler 

local point2 = {} – this is the destination … it is always fixed… i want to get the arrow point to it.

– 21.452763, 39.813573

–point2.latitude = 21.45

–point2.longitude = 39.81

point2.x = 21.45

point2.y = 39.81

point1.x = 26.4024

point1.y = 29.41

local function updateCompass( event )

    --print( "Compass magnetic: " … event.magnetic )

    compassHeading = event.magnetic

    if compassHeading == -1 then

        print(“no heading”)

        return

    end

    local deltaAngle = math_floor(angle - compassHeading) 

    if deltaAngle < 0 then

        deltaAngle = deltaAngle + 360

    end

    print(“angle:”, angle, “compass”, compassHeading, “deltaAngle:”, deltaAngle, compassHeading - angle)

    compassPointer.rotation = (deltaAngle)

end


local function angleBetween( point1, point2 )

    local angle = ( math.deg( math.atan2( point2.y - point1.y, point2.x - point1.x ) ) + 90 )

    return angle % 360

end

angle = angleBetween( point1, point2 )

print ( "angle = " … angle)

[/lua]

and this is what I got it from my console .

--------- beginning of /dev/log/main

--------- beginning of /dev/log/system

V/Corona  ( 9180): > Class.forName: network.LuaLoader

V/Corona  ( 9180): < Class.forName: network.LuaLoader

V/Corona  ( 9180): Loading via reflection: network.LuaLoader

I/Corona  ( 9180): Platform: GT-I9300 / ARM Neon / 4.3 / Mali-400 MP / OpenGL ES 2.0 / 2015.2576

V/Corona  ( 9180): > Class.forName: CoronaProvider.licensing.google.LuaLoader

V/Corona  ( 9180): < Class.forName: CoronaProvider.licensing.google.LuaLoader

V/Corona  ( 9180): Loading via reflection: CoronaProvider.licensing.google.LuaLoader

I/Corona  ( 9180): angle = 205.46343060222

i appreciate if you code provide short summary how the process will work and what i need in terms of coding and other components ?

thanks alot. 

I did this update code… but i am not getting the right direction yet 

[lua]

local point1 ={}

local point2     ={}

point2.x = 21.45

point2.y = 39.81

–23.608234, 58.527350

–point1.latitude = 23.608234

–point2.longitude = 58.527350

–26.402426, 29.410834

–point1.x = 26.4024

–point1.y = 29.41

local compassPointer = display.newImage(‘images/pointer.png’)

compassPointer.anchorX = 0.5

compassPointer.anchorY = 0.5

compassPointer.x = display.contentCenterX

compassPointer.y = display.contentCenterY

local function updateCompass( event )

    --print( "Compass magnetic: " … event.magnetic )

    compassHeading = event.magnetic

    if compassHeading == -1 then

        print(“no heading”)

        return

    end

    local deltaAngle = math.floor(angle - compassHeading) 

    if deltaAngle < 0 then

        deltaAngle = deltaAngle + 360

    end

    print(“angle:”, angle, “compass”, compassHeading, “deltaAngle:”, deltaAngle, compassHeading - angle)

    compassPointer.rotation = (deltaAngle)

end


local function angleBetween( point1, point2 )

    local angle = ( math.deg( math.atan2( point2.y - point1.y, point2.x - point1.x ) ) + 90 )

    return angle % 360

end

local distanceBetween  – function to find distances between the two porints


function distanceBetween( point1, point2 )

    local dX = point2.longitude - point1.longitude

    local dY = point2.latitude - point1.latitude

    local distance = math.sqrt( ( dX^2 ) + ( dY^2 ) )

    return distance 

end

-----  variabel to show the distance between  my location and 

local angleNow = display.newText( “-”, 100, 50, native.systemFont, 16 )

local latNow = display.newText( “-”, 100, 100, native.systemFont, 16 )

local LongNow = display.newText( “-”, 100, 150, native.systemFont, 16 )

----- handler for location handler

local locationHandler = function( event )

    – Check for error (user may have turned off location services)

    if ( event.errorCode ) then

        native.showAlert( “GPS Location Error”, event.errorMessage, {“OK”} )

        print( "Location error: " … tostring( event.errorMessage ) )

    else

    

        point1.x = event.latitude

        point1.y = event.longitude

        point1.latitude = event.latitude – set my lat 

        print ("point1.latitude = " … point1.latitude)

        latNow.text = event.latitude

        

        point1.longitude = event.longitude – set my long

        print ("point1.longitude = " … point1.longitude)

        LongNow.text = event.longitude

        --print(point1.latitude,point1.longitude)

        --local  x = distanceBetween(point1,point2) – find differnace…

        --local diff = string.format( ‘%.1f’, x )  – format it to one decimal 

        --print (" the distance is " …diff)

        --distance.text = diff  

        --drawLine(point1,point2)

        angle = angleBetween( point1, point2 )

        angleNow.text = angle

    end

end

–angle = angleBetween( point1, point2 )

Runtime:addEventListener( “location”, locationHandler ) – to get my location

Runtime:addEventListener( “heading”, updateCompass ) – to rotate the compass 

[/lua]

Hi Rob,

sorry to bother you but i really need your help and i need to make this app accurate as much as possible… now i am able to get the right direction after i make the arrow direct to right instead of up at start of the app … Not sure why ?

1_ which one is more accurate GPS or Wi-FI…? should i enable both ?

2_ my logic is like that  :

  a- find my location using location handler (Runtime:addEventListener( “location”, locationHandler )

  b- if successful then find angle between my location and the destination , then start heading with update compass function and animalDial to rotate the arrow

  c- also I stop the location handler if location is gathered…

is this right logic ?  do you have any advices ?

thanks
Abdulaziz

  1. GPS is way more accurate than WiFi.

  2. That logic seems sound!

Rob

Thanks Rob for help and advice., it is always appreciated…   I notice some apps similar to the these once using gyroscope or accelerometer  … is this something i need to consider for such  app to improve accuracy or this is not related to the main goal.

thanks

Abdulaziz

The GPS will always have some inaccuracy.  I think the military can get more accurate measurements because they use twice as many signals as the civilian system.  This is a quote from GPS.gov:

The actual accuracy users attain depends on factors outside the government’s control, including atmospheric effects, sky blockage, and receiver quality. Real-world data from the FAA show that their high-quality GPS SPS receivers provide better than 3.5 meter horizontal accuracy.

So atmospheric conditions, the ability to see multiple satellites and the quality of the receiver affect your overall accuracy.  Even the FAA who’s planes have very high quality GPS’s and don’t have to deal with buildings blocking signals only have a 3.5 meter accuracy (a little over 11 feet).  This means in best case you can be sitting still and the GPS will move you across the room.

In other words, you can’t improve accuracy, you are at the mercy of other factors.

The accelerometer determines if the phone is moving forwards or backwards, tilt and such.  The Gyroscope is an enhancement to the accelerometer that provides information about how the phone is being rotated.  Neither really have any impact on the GPS.

WiFi is useful because when you’re indoors you frequently can’t get to the GPS signals.  There is a huge database of WiFi hot spots that Google maintains.  Google can’t know the exact location of your WiFi router.  They know your address and IP address so they can get you in the neighborhood.   When you first turn location services on (and assuming you have cellular service), the device will try to triangulate its position based on cell phone towers.  The more towers it sees, the more accurate it can place you. If you’ve ever watched the map app, it frequently starts with a large circle and then the circle gets smaller as accuracy improves.  You might start at 5000 meters of accuracy and it will improve as it gets more information.  WiFi may get you into the dozens of feet of accuracy but it will get it quickly.  Finally when you get GPS signals, you will have the most accurate information.

I hope this helps you understand GPS accuracy.

Rob

Great   Thanks Rob for the time spent to write all this explanation … I apprecaite it . it is really useful. 

Abdulaizz

Well you can’t draw on top of native.newMap() if that’s your intent.  But you can have a compass that points in the direction you need to go to.  We did a tutorial not too long ago about using latitude and longitude to get an angle and distance.

https://coronalabs.com/blog/2015/01/06/tutorial-calculating-the-distance-between-map-points/

Even though it’s using map points, it’s doing so as a pair of Latitude/Longitude values which you do not need native.newMapView to get.  The GPS event can give you those points to work with and you of course can get them in various databases.

Rob

thanks Rob… i am trying to make that work… so far I am able to find the distance by the following code… i am still thinking how can I can make arrow pointing to the desired place …

[lua]

local point1 = {} –  my location coordinates… This point is to find by GPS event in the locationHandler 

local point2 = {} – this is the destination … it is always fixed… i want to get the arrow point to it.

– 21.452763, 39.813573

point2.latitude = 21.45

point2.longitude = 39.81

local distanceBetween  – function to find distances between the two porints


function distanceBetween( point1, point2 )

    local dX = point2.longitude - point1.longitude

    local dY = point2.latitude - point1.latitude

    local distance = math.sqrt( ( dX^2 ) + ( dY^2 ) )

    return distance 

end

-----  variabel to show the distance between  my location and 

local distance = display.newText( “-”, 100, 50, native.systemFont, 16 )

----- handler for location handler

local locationHandler = function( event )

    – Check for error (user may have turned off location services)

    if ( event.errorCode ) then

        native.showAlert( “GPS Location Error”, event.errorMessage, {“OK”} )

        print( "Location error: " … tostring( event.errorMessage ) )

    else

    

        

        point1.latitude = event.latitude – set my lat 

        point1.longitude = event.longitude – set my long

        local  x = distanceBetween(point1,point2) – find differnace…

        local diff = string.format( ‘%.1f’, x )  – format it to one decimal 

        distance.text = diff  

    end

end


– Activate location listener

Runtime:addEventListener( “location”, locationHandler )

[/lua]

:slight_smile:  i am welcoming any hints for the rest of the idea.

thx

Put in some more prints?  What are you seeing in the device’s console log?   You are testing on a device right?

Rob

Yes Rob, 

i am testing in a device ( android now) …

i have added also the logic to calculate the angle as shown in in the new code below … but still I am not sure if it is right and also I dont know how can show arrow from my location or center of the device ( assuming my location at the center ) to the specific location. 

[lua]

local point1 = {} –  my location coordinates… This point is to find by GPS event in the locationHandler 

local arrow

local point2 = {} – this is the destination … it is always fixed… i want to get the arrow point to it.

– 21.452763, 39.813573

point2.latitude = 21.45

point2.longitude = 39.81

local distanceBetween  – function to find distances between the two porints


function distanceBetween( point1, point2 )

    local dX = point2.longitude - point1.longitude

    local dY = point2.latitude - point1.latitude

    local distance = math.sqrt( ( dX^2 ) + ( dY^2 ) )

    return distance 

end

---- draw line 

local line 

local function drawLine(point1,point2) 

    — this a try to draw line or direction between the two points.

    line = display.newLine( point1.latitude,point1.longitude,point2.latitude,point2.longitude  )

    line:setStrokeColor( 1, 0, 0, 1 )

    line.strokeWidth = 4

    — this is to calculate the angle between the two points.

   – based on this thread : http://stackoverflow.com/questions/7586063/how-to-calculate-the-angle-between-a-line-and-the-horizontal-axis

    local dLongitude = point2.longitude - point1.longitude

    local dLatitude = point2.latitude - point1.latitude

     – Localize some math calls

    local sin = math.sin

    local cos = math.cos

    local sqrt = math.sqrt

    local atan2 = math.atan2

    local PI = math.pi

    local angleInDegrees = atan2(dLongitude, dLatitude) * 180 / PI

    print("angleInDegrees = ", angleInDegrees)

    --arrow.rotation = angleInDegrees

end

-----  variabel to show the distance between  my location and 

local distance = display.newText( “-”, 100, 50, native.systemFont, 16 )

----- handler for location handler

local locationHandler = function( event )

    – Check for error (user may have turned off location services)

    if ( event.errorCode ) then

        native.showAlert( “GPS Location Error”, event.errorMessage, {“OK”} )

        print( "Location error: " … tostring( event.errorMessage ) )

    else

    

        

        point1.latitude = event.latitude – set my lat 

        print ("point1.latitude = " … point1.latitude)

        

        point1.longitude = event.longitude – set my long

        print ("point1.longitude = " … point1.longitude)

        print(point1.latitude,point1.longitude)

        local  x = distanceBetween(point1,point2) – find differnace…

        local diff = string.format( ‘%.1f’, x )  – format it to one decimal 

        print (" the distance is " …diff)

        distance.text = diff  

        drawLine(point1,point2)

    end

end


– Activate location listener

Runtime:addEventListener( “location”, locationHandler )

[/lua]

here is the result from console

I/Corona  (32133): point1.latitude = 23.616665470107

I/Corona  (32133): point1.longitude = 58.093582024209

I/Corona  (32133): 23.616665470107 58.093582024209

I/Corona  (32133):  the distance is 18.4

I/Corona  (32133): angleInDegrees = -96.758223300233

I/Corona  (32133): point1.latitude = 23.616665470107

I/Corona  (32133): point1.longitude = 58.093582024209

I/Corona  (32133): 23.616665470107 58.093582024209

I/Corona  (32133):  the distance is 18.4

I/Corona  (32133): angleInDegrees = -96.758223300233

 

 

 

The above result does not look right… also i was not able to draw arrow… also i need the  location to adjust when the user move the device around … it should always be showning the arrow to the right direction…

 

 

thanks

 

local function updateCompass( event ) --print( "Compass magnetic: " .. event.magnetic ) compassHeading = event.magnetic if compassHeading == -1 then print("no heading") return end local deltaAngle = math\_floor(angle - compassHeading) if deltaAngle \< 0 then deltaAngle = deltaAngle + 360 end print("angle:", angle, "compass", compassHeading, "deltaAngle:", deltaAngle, compassHeading - angle) compassPointer.rotation = (deltaAngle) end local function angleBetween( point1, point2 ) local angle = ( math\_deg( math\_atan2( point2.y - point1.y, point2.x - point1.x ) ) + 90 ) return angle % 360 end angle = angleBetween( car, me )

This is code from my Find My Car app that I released a few months ago.   Though I was using it the other day and it was about 90 degrees off (my compass was uncalibrated through).

Now my compass does not point North like a typical compass, but instead points the direction you should be going.

Rob

Thanks Rob for sharing your code… this is appreciated… i have more queries if you dont mind :

1- i still not getting the point of rotating an arrow. where is that happened ? does your code directly change the compass arrow in the build in compass or how that is done… ?

2- how can we make the event running all time and keep the arrow in the right direction even user rotate his device…

3- how did you get the current location of user ? do you use GPS, maps or how  ? does that mean it will not work for all devices ?

4- what does this line do :  compassPointer.rotation = (deltaAngle)

for testing, i have two static points as shown below with your code :

[lua]

local point1 = {} –  my location coordinates… This point is to find by GPS event in the locationHandler 

local point2 = {} – this is the destination … it is always fixed… i want to get the arrow point to it.

– 21.452763, 39.813573

–point2.latitude = 21.45

–point2.longitude = 39.81

point2.x = 21.45

point2.y = 39.81

point1.x = 26.4024

point1.y = 29.41

local function updateCompass( event )

    --print( "Compass magnetic: " … event.magnetic )

    compassHeading = event.magnetic

    if compassHeading == -1 then

        print(“no heading”)

        return

    end

    local deltaAngle = math_floor(angle - compassHeading) 

    if deltaAngle < 0 then

        deltaAngle = deltaAngle + 360

    end

    print(“angle:”, angle, “compass”, compassHeading, “deltaAngle:”, deltaAngle, compassHeading - angle)

    compassPointer.rotation = (deltaAngle)

end


local function angleBetween( point1, point2 )

    local angle = ( math.deg( math.atan2( point2.y - point1.y, point2.x - point1.x ) ) + 90 )

    return angle % 360

end

angle = angleBetween( point1, point2 )

print ( "angle = " … angle)

[/lua]

and this is what I got it from my console .

--------- beginning of /dev/log/main

--------- beginning of /dev/log/system

V/Corona  ( 9180): > Class.forName: network.LuaLoader

V/Corona  ( 9180): < Class.forName: network.LuaLoader

V/Corona  ( 9180): Loading via reflection: network.LuaLoader

I/Corona  ( 9180): Platform: GT-I9300 / ARM Neon / 4.3 / Mali-400 MP / OpenGL ES 2.0 / 2015.2576

V/Corona  ( 9180): > Class.forName: CoronaProvider.licensing.google.LuaLoader

V/Corona  ( 9180): < Class.forName: CoronaProvider.licensing.google.LuaLoader

V/Corona  ( 9180): Loading via reflection: CoronaProvider.licensing.google.LuaLoader

I/Corona  ( 9180): angle = 205.46343060222

i appreciate if you code provide short summary how the process will work and what i need in terms of coding and other components ?

thanks alot. 

I did this update code… but i am not getting the right direction yet 

[lua]

local point1 ={}

local point2     ={}

point2.x = 21.45

point2.y = 39.81

–23.608234, 58.527350

–point1.latitude = 23.608234

–point2.longitude = 58.527350

–26.402426, 29.410834

–point1.x = 26.4024

–point1.y = 29.41

local compassPointer = display.newImage(‘images/pointer.png’)

compassPointer.anchorX = 0.5

compassPointer.anchorY = 0.5

compassPointer.x = display.contentCenterX

compassPointer.y = display.contentCenterY

local function updateCompass( event )

    --print( "Compass magnetic: " … event.magnetic )

    compassHeading = event.magnetic

    if compassHeading == -1 then

        print(“no heading”)

        return

    end

    local deltaAngle = math.floor(angle - compassHeading) 

    if deltaAngle < 0 then

        deltaAngle = deltaAngle + 360

    end

    print(“angle:”, angle, “compass”, compassHeading, “deltaAngle:”, deltaAngle, compassHeading - angle)

    compassPointer.rotation = (deltaAngle)

end


local function angleBetween( point1, point2 )

    local angle = ( math.deg( math.atan2( point2.y - point1.y, point2.x - point1.x ) ) + 90 )

    return angle % 360

end

local distanceBetween  – function to find distances between the two porints


function distanceBetween( point1, point2 )

    local dX = point2.longitude - point1.longitude

    local dY = point2.latitude - point1.latitude

    local distance = math.sqrt( ( dX^2 ) + ( dY^2 ) )

    return distance 

end

-----  variabel to show the distance between  my location and 

local angleNow = display.newText( “-”, 100, 50, native.systemFont, 16 )

local latNow = display.newText( “-”, 100, 100, native.systemFont, 16 )

local LongNow = display.newText( “-”, 100, 150, native.systemFont, 16 )

----- handler for location handler

local locationHandler = function( event )

    – Check for error (user may have turned off location services)

    if ( event.errorCode ) then

        native.showAlert( “GPS Location Error”, event.errorMessage, {“OK”} )

        print( "Location error: " … tostring( event.errorMessage ) )

    else

    

        point1.x = event.latitude

        point1.y = event.longitude

        point1.latitude = event.latitude – set my lat 

        print ("point1.latitude = " … point1.latitude)

        latNow.text = event.latitude

        

        point1.longitude = event.longitude – set my long

        print ("point1.longitude = " … point1.longitude)

        LongNow.text = event.longitude

        --print(point1.latitude,point1.longitude)

        --local  x = distanceBetween(point1,point2) – find differnace…

        --local diff = string.format( ‘%.1f’, x )  – format it to one decimal 

        --print (" the distance is " …diff)

        --distance.text = diff  

        --drawLine(point1,point2)

        angle = angleBetween( point1, point2 )

        angleNow.text = angle

    end

end

–angle = angleBetween( point1, point2 )

Runtime:addEventListener( “location”, locationHandler ) – to get my location

Runtime:addEventListener( “heading”, updateCompass ) – to rotate the compass 

[/lua]