Arrow to point to GeoPoint?

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

Arrow to point to GeoPoint?

Postby tonycurrie » Thu Feb 25, 2010 7:26 pm

Hi, This is my first post so please forgive any stupidities.

I am very new to Android development (and Java) coming from an AS3 development background.

I have successfully got a map appearing with my current location overlayed with a marker for my address. I have calculated the distance between my current position and my address, and this updates as you move about. The next step, which I am having loads of trouble just getting my head around is to overlay an image of an arrow, and rotate the image accordingly to always be pointing the direction of my address. So like a compass but rather than pointing north, it points to a static GeoPoint.

I hope that makes sense. The idea is to have an arrow that shows me the direction, and a distance measurement telling me how far in that direction.

Thanks so much for help either by helping me to understand the stages to go through, snippets of code or general useful help.

Again, I am very new to this so probably best to assume I don't know something already.

Thanks

Tony
tonycurrie
Freshman
Freshman
 
Posts: 3
Joined: Thu Feb 25, 2010 7:10 pm

Top

Postby YellowJK » Fri Apr 09, 2010 1:44 am

i have done something similar, i @Override'd the drawCompass(Canvas canvas, float bearing) from MyLocationOverlay. it was the easiest way for me to get the compass bearing.


i hope this helps you, just as much as this site has helped me 8)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4.    @Override
  5.  
  6. protected void drawCompass(Canvas canvas, float bearing){
  7.  
  8.         /**
  9.  
  10.          * compass:
  11.  
  12.          * n=0
  13.  
  14.          * w=90
  15.  
  16.          * s=180
  17.  
  18.          * e=270
  19.  
  20.          */
  21.  
  22.        
  23.  
  24.         /**
  25.  
  26.          * override bearing so compass will point to the geoopint
  27.  
  28.          */
  29.  
  30.         if (gp1 != null && gp2 != null){
  31.  
  32.                 Double d = GeoUtils.bearing(gp1, gp2);
  33.  
  34.                 bearing = bearing - d.floatValue();
  35.  
  36.         } else if (bearing != 0){
  37.  
  38.         //we subtract bearing from 360 to zero adjust
  39.  
  40.                 bearing = 360 - bearing;
  41.  
  42.         }
  43.  
  44.        
  45.  
  46.         //pass modified bearing to on screen compass
  47.  
  48.         super.drawCompass(canvas, bearing);
  49.  
  50. }
  51.  
  52.  
  53.  
  54.  
Parsed in 0.042 seconds, using GeSHi 1.0.8.4


and heres the bearing method() that i used from google

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. /*
  5.  
  6.  * Copyright (C) 2008 Google Inc.
  7.  
  8.  *
  9.  
  10.  * Licensed under the Apache License, Version 2.0 (the "License");
  11.  
  12.  * you may not use this file except in compliance with the License.
  13.  
  14.  * You may obtain a copy of the License at
  15.  
  16.  *
  17.  
  18.  *      http://www.apache.org/licenses/LICENSE-2.0
  19.  
  20.  *
  21.  
  22.  * Unless required by applicable law or agreed to in writing, software
  23.  
  24.  * distributed under the License is distributed on an "AS IS" BASIS,
  25.  
  26.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27.  
  28.  * See the License for the specific language governing permissions and
  29.  
  30.  * limitations under the License.
  31.  
  32.  */
  33.  
  34.  
  35.  
  36. import java.util.ArrayList;
  37.  
  38. import java.util.List;
  39.  
  40.  
  41.  
  42.  
  43.  
  44. import com.google.android.maps.GeoPoint;
  45.  
  46.  
  47.  
  48. /**
  49.  
  50.  * Library for some use useful latitude/longitude math
  51.  
  52.  */
  53.  
  54. public class GeoUtils {
  55.  
  56.    
  57.  
  58.     private static int EARTH_RADIUS_KM = 6371;
  59.  
  60.     public static double MILLION = 1000000;    
  61.  
  62.  
  63.  
  64.     /**
  65.  
  66.      * Computes the bearing in degrees between two points on Earth.
  67.  
  68.      *
  69.  
  70.      * @param lat1 Latitude of the first point
  71.  
  72.      * @param lon1 Longitude of the first point
  73.  
  74.      * @param lat2 Latitude of the second point
  75.  
  76.      * @param lon2 Longitude of the second point
  77.  
  78.      * @return Bearing between the two points in degrees. A value of 0 means due
  79.  
  80.      *         north.
  81.  
  82.      */
  83.  
  84.     public static double bearing(double lat1, double lon1, double lat2, double lon2) {
  85.  
  86.         double lat1Rad = Math.toRadians(lat1);
  87.  
  88.         double lat2Rad = Math.toRadians(lat2);
  89.  
  90.         double deltaLonRad = Math.toRadians(lon2 - lon1);
  91.  
  92.  
  93.  
  94.         double y = Math.sin(deltaLonRad) * Math.cos(lat2Rad);
  95.  
  96.         double x = Math.cos(lat1Rad) * Math.sin(lat2Rad) - Math.sin(lat1Rad) * Math.cos(lat2Rad)
  97.  
  98.                 * Math.cos(deltaLonRad);
  99.  
  100.         return radToBearing(Math.atan2(y, x));
  101.  
  102.     }
  103.  
  104.    
  105.  
  106.     /**
  107.  
  108.      * Computes the bearing in degrees between two points on Earth.
  109.  
  110.      *
  111.  
  112.      * @param p1 First point
  113.  
  114.      * @param p2 Second point
  115.  
  116.      * @return Bearing between the two points in degrees. A value of 0 means due
  117.  
  118.      *         north.
  119.  
  120.      */
  121.  
  122.     public static double bearing(GeoPoint p1, GeoPoint p2) {
  123.  
  124.         double lat1 = p1.getLatitudeE6() / MILLION;
  125.  
  126.         double lon1 = p1.getLongitudeE6() / MILLION;
  127.  
  128.         double lat2 = p2.getLatitudeE6() / MILLION;
  129.  
  130.         double lon2 = p2.getLongitudeE6() / MILLION;
  131.  
  132.  
  133.  
  134.         return bearing(lat1, lon1, lat2, lon2);
  135.  
  136.     }
  137.  
  138.    
  139.  
  140.    
  141.  
  142.     /**
  143.  
  144.      * Converts an angle in radians to degrees
  145.  
  146.      */
  147.  
  148.     public static double radToBearing(double rad) {
  149.  
  150.         return (Math.toDegrees(rad) + 360) % 360;
  151.  
  152.     }
  153.  
  154.  
  155.  
  156. }
  157.  
  158.  
  159.  
  160.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
YellowJK
Freshman
Freshman
 
Posts: 9
Joined: Fri Apr 09, 2010 12:19 am

Re: Arrow to point to GeoPoint?

Postby pedroteixeira07 » Wed Sep 22, 2010 10:07 am

I have my compass done by reading compass sensor readings which gives me the Azimuth, pitch and roll of my device in relation to the north... I want to do the same as you mentioned.. pointing my compass to a geopoint and not to the north.. but keep it updated.. whenever I change my device orientation ...the ''needle'' on the compass would point always to that geopoint.. this is turining out to be a big headache.. can you help? I need a formula that uses my devices sensor so it keeps updating the direction .. I can't use any algorithm because it givesme static measurments..
pedroteixeira07
Experienced Developer
Experienced Developer
 
Posts: 74
Joined: Sun Oct 18, 2009 6:06 pm
Location: Funchal

Re: Arrow to point to GeoPoint?

Postby J_R » Thu Oct 07, 2010 4:28 am

If I understand this right you actually need two things:
1. A location listener
2. An orientation listener

In both of these listeners you want a a routine that updates your compass needle.

Here's what I did for my Signal Radar app:

Store the direction of north and the bearing of the geopoint as variables in your activity. Adding them together gives the correct relative direction.

You'll need to recalculate the geopoint bearing whenever you move. If the geopoint is an instance of Location you can use Location.getBearing(). Otherwise you'll need a formula like:

degrees = 90-(float) (Math.atan2(pAfter.y - pBefore.y, pAfter.x
- pBefore.x) * 180 / (3.14159162));

Where pBefore and pAfter are graphics points here, to get a point form a geopoint use:
Point point = new Point((int) (geopoint.getLongitudeE6()), (int) (geopoint.getLatitudeE6()));

Hope that makes sense
http://www.staircase3.com
J_R
Developer
Developer
 
Posts: 32
Joined: Fri Jan 22, 2010 12:37 am

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 2 guests