1 | package org.greenstone.android.tipple.base;
|
---|
2 |
|
---|
3 |
|
---|
4 | import org.mapsforge.core.GeoPoint;
|
---|
5 | import android.content.Context;
|
---|
6 | import android.graphics.Canvas;
|
---|
7 | import android.graphics.Paint;
|
---|
8 | import android.view.Display;
|
---|
9 | import android.view.View;
|
---|
10 |
|
---|
11 |
|
---|
12 | public class ArDirection extends View {
|
---|
13 |
|
---|
14 | private final float x;
|
---|
15 | private final float y;
|
---|
16 | private final float r;
|
---|
17 | private static double angle;
|
---|
18 |
|
---|
19 |
|
---|
20 | private final Paint mBlack = new Paint(Paint.LINEAR_TEXT_FLAG);
|
---|
21 | private final Paint mGray = new Paint(Paint.ANTI_ALIAS_FLAG);
|
---|
22 |
|
---|
23 | Canvas cox;
|
---|
24 | private static double lon;
|
---|
25 | private static double lat;
|
---|
26 | private static double piLong;
|
---|
27 | private static double piLat;
|
---|
28 |
|
---|
29 | public ArDirection(Context context, Display params){
|
---|
30 | super(context);
|
---|
31 | mGray.setColor(0xff888888);
|
---|
32 |
|
---|
33 | this.r = params.getHeight()/10;
|
---|
34 | this.x = params.getWidth() - r- 10;
|
---|
35 | this.y = params.getHeight() - r -10;
|
---|
36 | this.angle = 0;
|
---|
37 | }
|
---|
38 |
|
---|
39 | protected void onDraw(Canvas canvas){
|
---|
40 | super.onDraw(canvas);
|
---|
41 | this.cox = canvas;
|
---|
42 |
|
---|
43 | float endX = getEndX(angle);
|
---|
44 | float endY = getEndY(angle);
|
---|
45 |
|
---|
46 | mGray.setStyle(Paint.Style.STROKE);
|
---|
47 | canvas.drawCircle(x, y, r, mGray);
|
---|
48 |
|
---|
49 | canvas.drawLine(x, y, endX, endY, mBlack);
|
---|
50 |
|
---|
51 | drawLine(cox, -dircAngle(piLong, piLat, lon, lat)-15, mGray);//
|
---|
52 | drawLine(cox, -dircAngle(piLong, piLat, lon, lat)+15, mGray);//
|
---|
53 |
|
---|
54 | drawNlables(cox);
|
---|
55 | }
|
---|
56 |
|
---|
57 | private void drawNlables(Canvas canvas){
|
---|
58 |
|
---|
59 | float endX = getEndX(0);
|
---|
60 | float endY = getEndY(0);
|
---|
61 | canvas.drawText("N", endX-2, endY, mGray);
|
---|
62 |
|
---|
63 | endX = getEndX(90);
|
---|
64 | endY = getEndY(90);
|
---|
65 | canvas.drawText("E", endX+1, endY, mGray);
|
---|
66 |
|
---|
67 | endX = getEndX(180);
|
---|
68 | endY = getEndY(180);
|
---|
69 | canvas.drawText("S", endX-2, endY+9, mGray);
|
---|
70 |
|
---|
71 | endX = getEndX(270);
|
---|
72 | endY = getEndY(270);
|
---|
73 | canvas.drawText("W", endX-10, endY, mGray);
|
---|
74 | }
|
---|
75 |
|
---|
76 | private float getEndX(double angle){
|
---|
77 | angle = (angle-90)* Math.PI/180;
|
---|
78 | return (float) ((x) + (r*Math.cos(angle)));
|
---|
79 | }
|
---|
80 | private float getEndY(double angle){
|
---|
81 | angle = (angle-90)* Math.PI/180;
|
---|
82 | return (float) ((y) + (r*Math.sin(angle)));
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 |
|
---|
87 | private float dircAngle(double aLong , double alat , double blong, double blat){
|
---|
88 | alat = Math.toRadians(alat);
|
---|
89 | blat = Math.toRadians(blat);
|
---|
90 |
|
---|
91 | // Calculate Difference
|
---|
92 | double longitudeDifference = Math.toRadians(blong - aLong);
|
---|
93 |
|
---|
94 | // Returns the Sine and the Cosine of the specified angle
|
---|
95 | double y = Math.sin(longitudeDifference) * Math.cos(blat);
|
---|
96 | double x = Math.cos(alat) * Math.sin(blat) - Math.sin(alat) * Math.cos(blat)
|
---|
97 | * Math.cos(longitudeDifference);
|
---|
98 |
|
---|
99 | // Return Bearing
|
---|
100 | return (float) ((Math.toDegrees(Math.atan2(y, x))+ 360) % 360);
|
---|
101 | }
|
---|
102 |
|
---|
103 | protected void drawLine(Canvas canvas, float angl,Paint paint){
|
---|
104 |
|
---|
105 | float endX = getEndX(angl);
|
---|
106 | float endY = getEndY(angl);
|
---|
107 | canvas.drawLine(x, y, endX, endY, paint);
|
---|
108 | }
|
---|
109 | public static void setChanges(float ang){
|
---|
110 | angle = ang;
|
---|
111 | }
|
---|
112 |
|
---|
113 | public static void setGPSchanges(GeoPoint point){
|
---|
114 | lon = point.getLongitude();
|
---|
115 | lat = point.getLatitude();
|
---|
116 | }
|
---|
117 | public static void setGPSpidouble (GeoPoint place){
|
---|
118 | piLat = place.getLatitude();
|
---|
119 | piLong = place.getLongitude();
|
---|
120 | }
|
---|
121 | } |
---|