-
Notifications
You must be signed in to change notification settings - Fork 0
/
MapsActivity.java
442 lines (400 loc) · 19.3 KB
/
MapsActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
package com.earthquake.archi.earthquake;
import android.app.ActionBar;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
import com.google.maps.android.SphericalUtil;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
public TextView txtView;
double x;
double y;
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#2669d6")));
TextView txtView = (TextView)findViewById(R.id.text);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
Button e;
e = (Button) findViewById(R.id.enter);
e.setOnClickListener(
new View.OnClickListener() {
public void onClick(View view) {
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
EditText edit = (EditText) findViewById(R.id.lat);
EditText edit2 = (EditText) findViewById(R.id.lon);
String content = edit.getText().toString();
String content2 = edit2.getText().toString();
//Toast.makeText(getApplicationContext(), content + content2, Toast.LENGTH_LONG).show();
x = Double.parseDouble(content);
y = Double.parseDouble(content2);
mMap.clear();
riskcalc(x,y);
}
});}
// Add a marker in milipitas earthquake and move the camera
public void riskcalc(double x, double y) {
LatLng quake1 = new LatLng(37.467, -121.820);
LatLng quake2 = new LatLng(37.590, -121.967);
LatLng quake3 = new LatLng(37.471, -121.821);
LatLng quake4 = new LatLng(37.804, -122.211);
LatLng quake5 = new LatLng(37.473, -121.821);
LatLng quake6 = new LatLng(37.467, -121.820);
LatLng quake7 = new LatLng(38.843, -121.165);
LatLng quake8 = new LatLng(37.886, -122.216);
LatLng quake9 = new LatLng(37.326, -122.106);
LatLng quake10 = new LatLng(38.785, -122.765);
LatLng quake11 = new LatLng(37.360, -121.726);
LatLng quake12 = new LatLng(38.754, -122.275);
LatLng quake13 = new LatLng(38.824, -122.797);
LatLng quake14 = new LatLng(38.785, -122.765);
LatLng quake15 = new LatLng(38.036, -122.234);
LatLng quake16 = new LatLng(37.473, -121.821);
LatLng quake17 = new LatLng(37.335, -122.213);
LatLng quake18 = new LatLng(38.793, -122.760);
LatLng quake19 = new LatLng(37.133, -121.528);
LatLng quake20 = new LatLng(38.117, -122.222);
LatLng quake21 = new LatLng(38.771, -122.704);
LatLng quake22 = new LatLng(37.747, -122.145);
LatLng quake23 = new LatLng(38.791, -122.724);
LatLng quake24 = new LatLng(37.901, -121.983);
LatLng quake25 = new LatLng(38.828, -122.785);
LatLng quake26 = new LatLng(38.827, -122.797);
LatLng quake27 = new LatLng(38.792, -122.426);
LatLng quake28 = new LatLng(38.793, -122.801);
LatLng quake29 = new LatLng(38.071, -122.226);
LatLng quake30 = new LatLng(38.823, -122.808);
LatLng quake31 = new LatLng(38.847, -122.817);
LatLng quake32 = new LatLng(37.901, -121.983);
LatLng quake33 = new LatLng(38.803, -122.766);
LatLng quake34 = new LatLng(37.901, -121.983);
LatLng quake35 = new LatLng(38.071, -122.226);
LatLng quake36 = new LatLng(38.117, -122.222);
LatLng quake37 = new LatLng(37.857, -122.594);
LatLng quake38 = new LatLng(37.130, -121.532);
LatLng quake39 = new LatLng(38.343, -122.627);
LatLng quake40 = new LatLng(37.857, -122.594);
LatLng quake41 = new LatLng(37.75, -122.55);
LatLng test = new LatLng(x, y);
mMap.moveCamera(CameraUpdateFactory.newLatLng(test));
mMap.animateCamera(CameraUpdateFactory.zoomTo(7));
double[] dists = new double[]{
CalculationByDistance(quake1, test) / 1.60934,
CalculationByDistance(quake2, test) / 1.60934,
CalculationByDistance(quake3, test) / 1.60934,
CalculationByDistance(quake4, test) / 1.60934,
CalculationByDistance(quake5, test) / 1.60934,
CalculationByDistance(quake6, test) / 1.60934,
CalculationByDistance(quake7, test) / 1.60934,
CalculationByDistance(quake8, test) / 1.60934,
CalculationByDistance(quake9, test) / 1.60934,
CalculationByDistance(quake10, test) / 1.60934,
CalculationByDistance(quake11, test) / 1.60934,
CalculationByDistance(quake12, test) / 1.60934,
CalculationByDistance(quake13, test) / 1.60934,
CalculationByDistance(quake14, test) / 1.60934,
CalculationByDistance(quake15, test) / 1.60934,
CalculationByDistance(quake16, test) / 1.60934,
CalculationByDistance(quake17, test) / 1.60934,
CalculationByDistance(quake18, test) / 1.60934,
CalculationByDistance(quake19, test) / 1.60934,
CalculationByDistance(quake20, test) / 1.60934,
CalculationByDistance(quake21, test) / 1.60934,
CalculationByDistance(quake22, test) / 1.60934,
CalculationByDistance(quake23, test) / 1.60934,
CalculationByDistance(quake24, test) / 1.60934,
CalculationByDistance(quake25, test) / 1.60934,
CalculationByDistance(quake26, test) / 1.60934,
CalculationByDistance(quake27, test) / 1.60934,
CalculationByDistance(quake28, test) / 1.60934,
CalculationByDistance(quake29, test) / 1.60934,
CalculationByDistance(quake30, test) / 1.60934,
CalculationByDistance(quake31, test) / 1.60934,
CalculationByDistance(quake32, test) / 1.60934,
CalculationByDistance(quake33, test) / 1.60934,
CalculationByDistance(quake34, test) / 1.60934,
CalculationByDistance(quake35, test) / 1.60934,
CalculationByDistance(quake36, test) / 1.60934,
CalculationByDistance(quake37, test) / 1.60934,
CalculationByDistance(quake38, test) / 1.60934,
CalculationByDistance(quake39, test) / 1.60934,
CalculationByDistance(quake40, test) / 1.60934,
CalculationByDistance(quake41, test) / 1.60934,
//divide by 1.60934 to convert to miles from KM
/*1609.34 * SphericalUtil.computeDistanceBetween(quake1, test),
SphericalUtil.computeDistanceBetween(quake2, test),
1609.34 * SphericalUtil.computeDistanceBetween(quake3, test),
1609.34 * SphericalUtil.computeDistanceBetween(quake4, test),
1609.34 * SphericalUtil.computeDistanceBetween(quake5, test),
1609.34 * SphericalUtil.computeDistanceBetween(quake6, test),
1609.34 * SphericalUtil.computeDistanceBetween(quake7, test),
1609.34 * SphericalUtil.computeDistanceBetween(quake8, test),
1609.34 * SphericalUtil.computeDistanceBetween(quake9, test),*/
};
//Toast.makeText(getApplicationContext(),Arrays.toString(dists),Toast.LENGTH_LONG).show();
Arrays.sort(dists);
Polyline line2 = mMap.addPolyline(new PolylineOptions()
.add(quake1, test)
.width(5)
.color(Color.RED));
Polyline line3 = mMap.addPolyline(new PolylineOptions()
.add(quake2, test)
.width(5)
.color(Color.RED));
Polyline line4 = mMap.addPolyline(new PolylineOptions()
.add(quake3, test)
.width(5)
.color(Color.RED));
Polyline line5 = mMap.addPolyline(new PolylineOptions()
.add(quake4, test)
.width(5)
.color(Color.RED));
Polyline line6 = mMap.addPolyline(new PolylineOptions()
.add(quake5, test)
.width(5)
.color(Color.RED));
Polyline line7 = mMap.addPolyline(new PolylineOptions()
.add(quake6, test)
.width(5)
.color(Color.RED));
Polyline line8 = mMap.addPolyline(new PolylineOptions()
.add(quake7, test)
.width(5)
.color(Color.RED));
Polyline line9 = mMap.addPolyline(new PolylineOptions()
.add(quake8, test)
.width(5)
.color(Color.RED));
Polyline line10 = mMap.addPolyline(new PolylineOptions()
.add(quake9, test)
.width(5)
.color(Color.RED));
Polyline line11 = mMap.addPolyline(new PolylineOptions()
.add(quake10, test)
.width(5)
.color(Color.RED));
Polyline line12 = mMap.addPolyline(new PolylineOptions()
.add(quake11, test)
.width(5)
.color(Color.RED));
Polyline line13 = mMap.addPolyline(new PolylineOptions()
.add(quake12, test)
.width(5)
.color(Color.RED));
Polyline line14 = mMap.addPolyline(new PolylineOptions()
.add(quake13, test)
.width(5)
.color(Color.RED));
Polyline line15 = mMap.addPolyline(new PolylineOptions()
.add(quake14, test)
.width(5)
.color(Color.RED));
Polyline line16 = mMap.addPolyline(new PolylineOptions()
.add(quake15, test)
.width(5)
.color(Color.RED));
Polyline line17 = mMap.addPolyline(new PolylineOptions()
.add(quake16, test)
.width(5)
.color(Color.RED));
Polyline line18 = mMap.addPolyline(new PolylineOptions()
.add(quake17, test)
.width(5)
.color(Color.RED));
Polyline line19 = mMap.addPolyline(new PolylineOptions()
.add(quake18, test)
.width(5)
.color(Color.RED));
Polyline line20 = mMap.addPolyline(new PolylineOptions()
.add(quake19, test)
.width(5)
.color(Color.RED));
Polyline line21 = mMap.addPolyline(new PolylineOptions()
.add(quake20, test)
.width(5)
.color(Color.RED));
Polyline line22 = mMap.addPolyline(new PolylineOptions()
.add(quake21, test)
.width(5)
.color(Color.RED));
Polyline line23 = mMap.addPolyline(new PolylineOptions()
.add(quake22, test)
.width(5)
.color(Color.RED));
Polyline line24 = mMap.addPolyline(new PolylineOptions()
.add(quake23, test)
.width(5)
.color(Color.RED));
Polyline line25 = mMap.addPolyline(new PolylineOptions()
.add(quake24, test)
.width(5)
.color(Color.RED));
Polyline line26 = mMap.addPolyline(new PolylineOptions()
.add(quake25, test)
.width(5)
.color(Color.RED));
Polyline line27 = mMap.addPolyline(new PolylineOptions()
.add(quake26, test)
.width(5)
.color(Color.RED));
Polyline line28 = mMap.addPolyline(new PolylineOptions()
.add(quake27, test)
.width(5)
.color(Color.RED));
Polyline line29 = mMap.addPolyline(new PolylineOptions()
.add(quake28, test)
.width(5)
.color(Color.RED));
Polyline line30 = mMap.addPolyline(new PolylineOptions()
.add(quake29, test)
.width(5)
.color(Color.RED));
Polyline line31 = mMap.addPolyline(new PolylineOptions()
.add(quake30, test)
.width(5)
.color(Color.RED));
Polyline line32 = mMap.addPolyline(new PolylineOptions()
.add(quake31, test)
.width(5)
.color(Color.RED));
Polyline line33 = mMap.addPolyline(new PolylineOptions()
.add(quake32, test)
.width(5)
.color(Color.RED));
Polyline line34 = mMap.addPolyline(new PolylineOptions()
.add(quake33, test)
.width(5)
.color(Color.RED));
Polyline line35 = mMap.addPolyline(new PolylineOptions()
.add(quake34, test)
.width(5)
.color(Color.RED));
Polyline line36 = mMap.addPolyline(new PolylineOptions()
.add(quake35, test)
.width(5)
.color(Color.RED));
Polyline line37 = mMap.addPolyline(new PolylineOptions()
.add(quake36, test)
.width(5)
.color(Color.RED));
Polyline line38 = mMap.addPolyline(new PolylineOptions()
.add(quake37, test)
.width(5)
.color(Color.RED));
Polyline line39 = mMap.addPolyline(new PolylineOptions()
.add(quake38, test)
.width(5)
.color(Color.RED));
Polyline line40 = mMap.addPolyline(new PolylineOptions()
.add(quake39, test)
.width(5)
.color(Color.RED));
Polyline line41 = mMap.addPolyline(new PolylineOptions()
.add(quake40, test)
.width(5)
.color(Color.RED));
Polyline line42 = mMap.addPolyline(new PolylineOptions()
.add(quake41, test)
.width(5)
.color(Color.RED));
TextView txtView = (TextView) findViewById(R.id.text);
if (dists[0] > 0 && dists[0] < 4) {
txtView.setText("Earthquake Risk: Very High");
txtView.setBackgroundColor(Color.RED);
txtView.setTextColor(Color.WHITE);
//Toast.makeText(getApplicationContext(),"Very High Risk for Earthquakes",Toast.LENGTH_LONG).show();
}
if (dists[0] > 4 && dists[0] < 8) {
txtView.setText("Earthquake Risk: High");
txtView.setBackgroundColor(Color.RED);
txtView.setTextColor(Color.WHITE);
//Toast.makeText(getApplicationContext(),"High Risk for Earthquakes",Toast.LENGTH_LONG).show();
}
if (dists[0] > 8 && dists[0] < 12) {
txtView.setText("Earthquake Risk: Medium");
txtView.setBackgroundColor(Color.YELLOW);
//Toast.makeText(getApplicationContext(),"Medium High Risk for Earthquakes",Toast.LENGTH_LONG).show();
}
if (dists[0] > 12 && dists[0] < 16) {
txtView.setText("Earthquake Risk: Low");
txtView.setBackgroundColor(Color.GREEN);
//Toast.makeText(getApplicationContext(),"Low Risk for Earthquakes",Toast.LENGTH_LONG).show();
}
if (dists[0] > 16) {
txtView.setText("Earthquake Risk: Very Low");
txtView.setBackgroundColor(Color.GREEN);
//Toast.makeText(getApplicationContext(),"Very Low Risk for Earthquakes",Toast.LENGTH_LONG).show();
}
}
public double CalculationByDistance(LatLng StartP, LatLng EndP) {
int Radius = 6371;// radius of earth in Km
double lat1 = StartP.latitude;
double lat2 = EndP.latitude;
double lon1 = StartP.longitude;
double lon2 = EndP.longitude;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
int kmInDec = Integer.valueOf(newFormat.format(km));
double meter = valueResult % 1000;
int meterInDec = Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value", "" + valueResult + " KM " + kmInDec
+ " Meter " + meterInDec);
return Radius * c;
}
}