質問編集履歴
1
一部のコードだけではなく全文を掲載しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -30,42 +30,101 @@
|
|
30
30
|
```
|
31
31
|
/data/data/[packagename]/files/sample.json のように保存しています。
|
32
32
|
|
33
|
-
```java
|
33
|
+
```Overlayview.java
|
34
|
+
package com.example.secondar6;
|
35
|
+
|
36
|
+
import android.content.Context;
|
37
|
+
import android.graphics.Canvas;
|
38
|
+
import android.graphics.Color;
|
39
|
+
import android.graphics.Paint;
|
40
|
+
import android.graphics.Path;
|
41
|
+
import android.graphics.RectF;
|
42
|
+
import android.view.Display;
|
43
|
+
import android.view.View;
|
44
|
+
import android.view.WindowManager;
|
45
|
+
|
46
|
+
import com.google.android.gms.maps.model.LatLng;
|
47
|
+
|
48
|
+
import org.json.JSONArray;
|
49
|
+
import org.json.JSONException;
|
50
|
+
import org.json.JSONObject;
|
51
|
+
|
52
|
+
import java.io.BufferedReader;
|
53
|
+
import java.io.File;
|
54
|
+
import java.io.FileNotFoundException;
|
55
|
+
import java.io.FileReader;
|
56
|
+
import java.io.IOException;
|
57
|
+
import java.util.ArrayList;
|
58
|
+
|
59
|
+
public class OverlayView extends View {
|
60
|
+
|
61
|
+
// カメラの画角を指定する
|
62
|
+
private final int ANGLE = 71;
|
63
|
+
// ARテキストの見える範囲を指定する
|
64
|
+
// ここでは3kmほどに指定する
|
65
|
+
private final float VIEW_LIMIT = 3000;
|
66
|
+
|
67
|
+
//赤道半径
|
68
|
+
private final double RADIUS = 6378137;
|
69
|
+
|
70
|
+
// ディスプレイサイズ
|
71
|
+
private int displayX;
|
72
|
+
// コンパスの描画位置を指定する
|
73
|
+
private final float POS_COMPASSX = 100;
|
74
|
+
private final float POS_COMPASSY = 100;
|
75
|
+
|
76
|
+
// 向きを保持する変数
|
77
|
+
float direction;
|
78
|
+
|
79
|
+
// 現在地を保持する変数
|
80
|
+
float posx;
|
81
|
+
float posy;
|
82
|
+
|
83
|
+
// ARテキストの情報を保持するオブジェクト
|
84
|
+
private ArrayList<readJson.GPSdata> glist;
|
85
|
+
|
86
|
+
public OverlayView(Context context) {
|
87
|
+
super(context);
|
88
|
+
// 画面サイズの取得
|
89
|
+
Display disp = ((WindowManager) context
|
90
|
+
.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
|
91
|
+
displayX = disp.getWidth();
|
92
|
+
}
|
93
|
+
|
34
|
-
public static class Json{
|
94
|
+
public static class readJson {
|
35
|
-
public static Array
|
95
|
+
public static ArrayList<GPSdata> jlist = new ArrayList<>();
|
36
|
-
public static void main(String[] args)throws JSONExp
|
96
|
+
public static void main(String[] args) throws JSONException {
|
37
97
|
String data = readFile();
|
38
98
|
JSONArray jarray = new JSONArray(data);
|
39
99
|
|
40
|
-
for (int n=0; n<jarray.length(); n++){
|
100
|
+
for (int n = 0; n < jarray.length(); n++) {
|
41
|
-
JSON
|
101
|
+
JSONObject jobject = jarray.getJSONObject(n);
|
42
|
-
|
102
|
+
|
43
|
-
String info = (String) jobject.get("info");
|
103
|
+
String info = (String) jobject.get("info");
|
44
|
-
String slatitude = (String) jobject.get("latitude");
|
104
|
+
String slatitude = (String) jobject.get("latitude");
|
45
|
-
String slongitude = (String) jobject.get("longitude");
|
105
|
+
String slongitude = (String) jobject.get("longitude");
|
46
|
-
|
106
|
+
|
47
|
-
double latitude = Double.parseDouble(slatitude);
|
107
|
+
double latitude = Double.parseDouble(slatitude);
|
48
|
-
double longitude = Double.parseDouble(slongitude);
|
108
|
+
double longitude = Double.parseDouble(slongitude);
|
49
|
-
|
109
|
+
|
50
|
-
list.add(new S
|
110
|
+
jlist.add(new GPSdata(info, latitude, longitude));
|
51
|
-
}
|
111
|
+
}
|
52
|
-
}
|
112
|
+
}
|
53
|
-
|
113
|
+
|
54
|
-
public static class S
|
114
|
+
public static class GPSdata {
|
55
115
|
String info;
|
56
116
|
double latitude;
|
57
117
|
double longitude;
|
58
118
|
|
59
|
-
public S
|
119
|
+
public GPSdata(String info, double latitude, double longitude) {
|
60
120
|
this.info = info;
|
61
121
|
this.latitude = latitude;
|
62
122
|
this.longitude = longitude;
|
63
|
-
|
123
|
+
}
|
64
|
-
}
|
124
|
+
}
|
65
|
-
}
|
125
|
+
}
|
66
|
-
|
67
|
-
|
126
|
+
|
68
|
-
private static String readFile() {
|
127
|
+
private static String readFile() {
|
69
128
|
Context context = Myapplication.getAppContext();
|
70
129
|
String fileName = "sample.json";
|
71
130
|
try {
|
@@ -89,21 +148,30 @@
|
|
89
148
|
}
|
90
149
|
}
|
91
150
|
|
151
|
+
|
92
|
-
//描画処理
|
152
|
+
// (1)描画処理
|
93
|
-
@Override
|
153
|
+
@Override
|
94
|
-
protected void onDraw(Canvas canvas) {
|
154
|
+
protected void onDraw(Canvas canvas) {
|
95
155
|
Paint paint = new Paint();
|
96
156
|
paint.setAntiAlias(true);
|
157
|
+
|
97
|
-
|
158
|
+
// 0.1秒休止
|
159
|
+
// 吹き出しの高速移動によるちらつきを抑える
|
160
|
+
try {
|
161
|
+
Thread.sleep(100);
|
162
|
+
} catch (InterruptedException e) {
|
163
|
+
e.printStackTrace();
|
164
|
+
}
|
165
|
+
|
98
|
-
ArrayList<Json.S
|
166
|
+
ArrayList<readJson.GPSdata> glist = readJson.jlist;
|
99
|
-
|
167
|
+
|
100
|
-
// ARテキストの描画
|
168
|
+
// 追記:ARテキストの描画
|
101
|
-
for (int i = 0; i <
|
169
|
+
for (int i = 0; i < glist.size(); i++) {
|
102
170
|
// データの読み込み
|
103
|
-
Json.S
|
171
|
+
readJson.GPSdata gdata = glist.get(i);
|
104
|
-
String info = data.info;
|
172
|
+
String info = gdata.info;
|
105
|
-
double y = data.latitude;
|
173
|
+
double y = gdata.latitude;
|
106
|
-
double x = data.longitude;
|
174
|
+
double x = gdata.longitude;
|
107
175
|
|
108
176
|
// ARテキストとの距離を求める
|
109
177
|
double dx = (x - posx);
|
@@ -116,9 +184,104 @@
|
|
116
184
|
float distance = (float)(RADIUS * Math.acos(Math.sin(yrad)* Math.sin(posyrad)
|
117
185
|
+ Math.cos(yrad) * Math.cos(posyrad) * Math.cos(dxrad)));
|
118
186
|
|
187
|
+
|
188
|
+
// ARテキストと現在地のなす角を求めて正規化する
|
189
|
+
double angle = Math.atan2(dy, dx);
|
190
|
+
float degree = (float) Math.toDegrees(angle);
|
191
|
+
degree = -degree + 90;
|
192
|
+
if (degree < 0)
|
193
|
+
degree = 360 + degree;
|
194
|
+
|
195
|
+
// 端末の向きとARテキストとの角度の差を求める
|
196
|
+
float sub = degree - direction;
|
197
|
+
if (sub < -180.0)
|
198
|
+
sub += 360;
|
199
|
+
if (sub > 180.0)
|
200
|
+
sub -= 360;
|
201
|
+
|
202
|
+
// ARテキストが視野に存在すれば描画処理を行う
|
203
|
+
if (Math.abs(sub) < (ANGLE / 2)) {
|
204
|
+
// 距離によってARテキストのサイズを決める
|
205
|
+
float textSize = 60 * (VIEW_LIMIT - distance)
|
206
|
+
/ VIEW_LIMIT;
|
207
|
+
paint.setTextSize(textSize);
|
208
|
+
|
119
|
-
//
|
209
|
+
// ARテキストの描画を描画する
|
210
|
+
String infoDistance = info + " " + (int)distance + "m" ;
|
211
|
+
float textWidth = paint.measureText(infoDistance);
|
212
|
+
float diff = (sub / (ANGLE / 2)) / 2;
|
213
|
+
float left = (displayX / 2 + displayX * diff) - (textWidth / 2);
|
214
|
+
drawBalloonText(canvas, paint, infoDistance, left, 200);
|
120
|
-
}
|
215
|
+
}
|
216
|
+
}
|
217
|
+
|
218
|
+
// コンパスを描画する
|
219
|
+
drawCompass(canvas, paint);
|
220
|
+
}
|
221
|
+
|
222
|
+
private void drawBalloonText(Canvas canvas, Paint paint, String text,
|
223
|
+
float left, float top) {
|
224
|
+
// 文字列の幅を取得
|
225
|
+
float textWidth = paint.measureText(text);
|
226
|
+
// フォント情報の取得
|
227
|
+
Paint.FontMetrics fontMetrics = paint.getFontMetrics();
|
228
|
+
|
229
|
+
// 文字列の20ポイント外側を囲む座標を求める
|
230
|
+
float bLeft = left - 20;
|
231
|
+
float bRight = left + textWidth + 20;
|
232
|
+
float bTop = top + fontMetrics.ascent - 20;
|
233
|
+
float bBottom = top + fontMetrics.descent + 20;
|
234
|
+
|
235
|
+
// 吹き出しの描画
|
236
|
+
RectF rectF = new RectF(bLeft, bTop, bRight, bBottom);
|
237
|
+
paint.setColor(Color.argb(100,0,255,0));
|
238
|
+
canvas.drawRoundRect(rectF, 50, 50, paint);
|
239
|
+
|
240
|
+
|
241
|
+
// 三角形の描画
|
242
|
+
paint.setStyle(Paint.Style.FILL_AND_STROKE);
|
243
|
+
Path path = new Path();
|
244
|
+
float center = left + textWidth / 2;
|
245
|
+
float triangleSize = paint.getTextSize() / 2;
|
246
|
+
path.moveTo(center, bBottom + triangleSize);
|
247
|
+
path.lineTo(center - triangleSize , bBottom );
|
248
|
+
path.lineTo(center + triangleSize , bBottom );
|
249
|
+
path.lineTo(center, bBottom + triangleSize);
|
250
|
+
canvas.drawPath(path, paint);
|
251
|
+
|
252
|
+
// 文字列の描画
|
253
|
+
paint.setColor(Color.WHITE);
|
254
|
+
canvas.drawText(text, left, top, paint);
|
255
|
+
|
256
|
+
}
|
257
|
+
|
258
|
+
// (2)コンパスの描画
|
259
|
+
private void drawCompass(Canvas canvas, Paint paint) {
|
260
|
+
Path path = new Path();
|
261
|
+
path.moveTo(POS_COMPASSX, POS_COMPASSY - 40);
|
262
|
+
path.lineTo(POS_COMPASSX + 20, POS_COMPASSY + 20);
|
263
|
+
path.lineTo(POS_COMPASSX - 20, POS_COMPASSY + 20);
|
264
|
+
path.moveTo(POS_COMPASSX, POS_COMPASSY - 40);
|
265
|
+
paint.setColor(Color.RED);
|
266
|
+
canvas.rotate(-direction, POS_COMPASSX, POS_COMPASSY);
|
267
|
+
canvas.drawPath(path, paint);
|
268
|
+
canvas.rotate(direction, POS_COMPASSX, POS_COMPASSY);
|
269
|
+
}
|
270
|
+
|
271
|
+
// (3)センサー値の取得と再描画
|
272
|
+
public void drawScreen(float preDirection,LatLng latlng) {
|
273
|
+
// センサーの値から端末の向きを計算する
|
274
|
+
direction = (preDirection + 450) % 360;
|
275
|
+
// 座標情報の取得
|
276
|
+
if(latlng != null){
|
277
|
+
posy = (float) latlng.latitude;
|
278
|
+
posx = (float) latlng.longitude;
|
279
|
+
}
|
280
|
+
// onDrawを呼び出して再描画
|
281
|
+
invalidate();
|
282
|
+
}
|
121
283
|
}
|
284
|
+
|
122
285
|
```
|
123
286
|
|
124
287
|
|