# Use compass to point to specific direction using coordinates

1494 views

### TonyMONANA

Jun 10, 2014, 7:15:19 PM6/10/14
Hi I've just started with App Inventor.

I would like to make a compass which point to Central Station of Amsterdam

I use the following website when making the compass.

http://puravidaapps.com/snippets.php#2compass

MVG,
Tony

### SteveJG

Jun 10, 2014, 7:48:47 PM6/10/14
You are out of luck.  You can not use the OrientationSensor to point to anything other than North.

To point to your train station, you will need the LocationSensor, use it to determine current location and calculate the bearing from the current location to the train station using the Math blocks.
c
You will probably use the Haversine algorithm to calculate distance from the station (if you need that too) and use the algorithms at the Aviation Formulary to calculate the bearing    find it here:   http://williams.best.vwh.net/avform.htm

You probably can use the Compass rose to do the pointing but you will have to use the  forumlas at the Aviation Formulary algorithm to calculate the bearing.
The Where is My Car and Location Sensor tutorials will get you started.

Try some blocks, if you get stuck, return here and someone might be able to give you advice.

Good luck.
Regards,
Steve

### Taifun

Jun 10, 2014, 8:34:51 PM6/10/14
you might be interested in this App Inventor Classic snippet https://groups.google.com/d/msg/programming-with-app-inventor/gW1Y0JGz-GI/qyLMqcL1NokJ
Taifun

Trying to push the limits of App Inventor! Snippets and Tutorials from Pura Vida Apps by Taifun.

Message has been deleted

### TonyMONANA

Jun 13, 2014, 1:42:19 PM6/13/14
Hi.

I tried the results of Engberg into App Inventor 2.

Converting I have tried almost all the options I could think of.
Is there anyone who can help here with me.

I added print screen of how far I am.
The compass will run, but not in the right direction.

But I do not know what I'm doing wrong
Bearing.jpg
azimuth.jpg
bearingpoint.png

### SteveJG

Jun 13, 2014, 1:57:59 PM6/13/14
to
Perhaps try

to be compatible with the example .... or was there a reason you added instead of subtracting as in the example?

Also, be aware, if your device is near your PC or other source of magnetism or a large metal object, the Orientation sensor will provide erroneous readings... it is a type of compass after all affected by metallic objects etc.

Did that help?

Regards,
Steve

### TonyMONANA

Jun 13, 2014, 2:16:07 PM6/13/14
that's a mistake.
Because I have tried so many possibilities that I've Overlooked that i added.

y wil try, can you see if my varibales are oke?

### TonyMONANA

Jun 13, 2014, 2:29:57 PM6/13/14
Y changed it in substracting.
it stil only points to the north.

when y use fixed values then it points to the right direction.
when y use varibles it's like if not responding to calculation.

### SteveJG

Jun 13, 2014, 3:19:04 PM6/13/14
when y use fixed values then it points to the right direction.

OK, then it works, just using simple trig.

when y use variables it's like if not responding to calculation.

I can not see how you enter those variables.  One possibility is your latitude and or longitude require a negative sign (-) and you forgot to include it in the list or whatever.
Can you show an image of how you are changing the latitude/longitude variable for the 'fixed' points.  Since using fixed values works correctly, you have an issue in
your list picker or  however you are selecting the fixed points.    You may have mixed up the lat and long for example .

-- Steve

### Taifun

Jun 13, 2014, 3:19:54 PM6/13/14
where do you call your procedure?
Taifun

### SteveJG

Jun 13, 2014, 3:22:15 PM6/13/14
to
Also, it looks like you inverted the code for the x and y values in the atan2 block.   So, I am not sure how you got this to point in the 'right direction.

### TonyMONANA

Jun 13, 2014, 4:06:17 PM6/13/14
when y call the procedure then the phone does not response any longer

### TonyMONANA

Jun 13, 2014, 4:09:24 PM6/13/14
Y have used this

atan2( sin(long2-long1)*cos(lat2),cos(lat1)*sin(lat2) â sin(lat1)*cos(lat2)*cos(long2-long1))

cos(lat1)*sin(lat2) â sin(lat1)*cos(lat2)*cos(long2-long1)

is this wrong?

### Taifun

Jun 13, 2014, 4:11:28 PM6/13/14
well, without calling the procedure everything you did was useless?
you might want to fix the mentioned errors and provide another screenshot?
Taifun

### SteveJG

Jun 13, 2014, 4:23:46 PM6/13/14
No, not wrong, just misplaced.

again, look at the example in classic AI      and compare it with the x blocks in your code and the x blocks in the example.

As Taifun said,   you MUST call the procedure.    Possibly it was not working because of coding errors.   Fix things, try again.   :)

Regards,
Steve

### TonyMONANA

Jun 13, 2014, 5:10:13 PM6/13/14
nou y have called tehe procedure en switched everything to place.

Y am not shure if y have made variable like it should be.
blocks.png
Naamloos.png
2.png

### TonyMONANA

Jun 13, 2014, 5:17:33 PM6/13/14
I suspect that I should use. a clock

### SteveJG

Jun 13, 2014, 5:21:56 PM6/13/14
The error statement says that where you multiply 4.899   (your longitude perhaps)   x    a value that is an empty string     implies that  targetlatitude.text   or targetlongitude.text    contains no value.

Where do you assign the values for targetlatitude.text  and targetlongitude.text?       It appears, among many issues, this is the current problem Tony.   So I ask again, where are the blocks where you put the values from a list or listpicker into the latitude/longitude text boxes?  The latest problem is probably there.

Regards,
Steve

### SteveJG

Jun 13, 2014, 5:22:40 PM6/13/14
No, you do not need a clock.

### TonyMONANA

Jun 13, 2014, 5:47:50 PM6/13/14
Y attached a printscreen of the label.

Naamloos.png

### SteveJG

Jun 13, 2014, 7:37:08 PM6/13/14
Thanks Tony.   It appears you have hard-coded the latitude in   targetlat and it is 52,379189 .   That appears to be OK.

What value did you hard-code in the longitude in targetlong label?   What I am trying to establish is if the targetlong label is empty or if it has a numerical value.   I do not need an image,
just the value. Thanks.

If you have a value in targetlong, then there is another issue that may involve the trig functions.

### TonyMONANA

Jun 13, 2014, 7:42:53 PM6/13/14
Y finally now what the problem was.
Thank u both Steve and Taifun.

It wil not do eny calculation because of the comma it uses. I changed it using "change al text" from "," to "."

it gives an error in the first instance because there is no value. The value needs to be first located by GPS before making the calculation.
Here I have some more to come up with a solution, because it's annoying every time an error.

I am very happy because this is my first app ever.

### SteveJG

Jun 13, 2014, 7:49:28 PM6/13/14
to
OK Tony.    I would have suspected the comma decimal versus the North American period decimal separator would have sorted themselves out.  Evidently not.

Very good detective work.  So changing the decimal value input from a , to a . fixed it?

And yes, the GPS first has to have a 'fix' before it will provide a value for currentlatitude1.text and currentlongitude1.text .      The coding could be fixed to check for a value in both of those before doing the calculation or you could set each to a dummy value when you initialize the screen.    In the Screen1.Initialize block, put the following:
set currentlatitude1.text to 1
set currentlongitude1.text to 1          or another value close to your latitude/longitude  and it should help to fix the issue regarding waiting for the GPS to get the fix.  The example code, evidently does not tell users to do that I assume.    However, it is good error avoidance coding.

-- Steve

### TonyMONANA

Jun 14, 2014, 8:58:11 AM6/14/14
Y used a dummy as you told and works great.

y stil do not have the right calculation.
The values that come out are not good.

this sould have been different values.
Now y am looking if y can find an easier way to calculate.

### SteveJG

Jun 14, 2014, 11:46:40 AM6/14/14
Regarding  Y used a dummy as you told and works great.
I assume this means you are no longer getting the error message.

Regarding  y stil do not have the right calculation.
The values that come out are not good.

Could you show an example?  What values are not good?  The bearing?   The way the arrow points or what?

this sould have been different values.
Regarding:  Now y am looking if y can find an easier way to calculate.

Easier, well, the formulas you have are about the easiest (at the expense of not being accurate over long distances).  They are simple trig assuming a flat Earth.

Tony, earlier, I mentioned the haversine algorithm:  http://www.movable-type.co.uk/scripts/latlong.html

Here is another discussion of how to calculate bearing:  http://mathforum.org/library/drmath/view/55417.html

Neither of the above examples use a compass rose or arrow, they just calculate the bearing in degrees.  Also, they are not AI2 blocks....you have to write the blocks yourself.

Regards,
Steve

### TonyMONANA

Jun 14, 2014, 12:04:19 PM6/14/14
Y am taking a break from it.

Next week i wil get into it again.

### Taifun

Jun 14, 2014, 5:44:11 PM6/14/14
you can now find my solution here https://puravidaapps.com/snippets.php#2bearing
Taifun

### TonyMONANA

Jun 14, 2014, 7:08:43 PM6/14/14
Thank y so mutch.
Y am very thankfull.

Y will try this tommorrow after work.

### TonyMONANA

Jun 15, 2014, 10:59:05 AM6/15/14
yes works perfect

### Peter

Jul 24, 2019, 5:45:04 AM7/24/19
to MIT App Inventor Forum
Hi guys, Sadly this doesn't work for me. It's a great base example for the start of what I am trying to do but can't get it to work.
The bearing stays the same at 180 no matter where i place the Long / Lat. Not sure where to go from here or where I am wrong? I read something about changing the + to - or vice versa but in Australia so don't think this changes for me I downloaded direct from your snippet link above. I have only changed the long / Lat.
blocks.png

### SteveJG

Jul 24, 2019, 9:08:43 AM7/24/19
@ Peter.... the behavior you see might be just a matter of how the Orientation sensor works.  Please walk away from you computer screen and other magnetic objects when you test. The Orientation sensor is affected by magnetic fields.

Here is a way to use the MIT Map Component to do something similar  https://groups.google.com/forum/#!searchin/mitappinventortest/bearing%7Csort:date/mitappinventortest/S5hZz3SegDE/gdue-PEuAwAJ

if you use the compass that is part of the Map Component.   It might help to build something similar to Taifun's example... but it does not use the OrientationSensor..

A long time ago, I discovered Taifun's example does not work in all situations ,
you might try the following algorithm

Does either of these work for you?

Regards,
Steve