-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalOpportunitiesTabMapFragment.java
More file actions
483 lines (413 loc) · 19.9 KB
/
LocalOpportunitiesTabMapFragment.java
File metadata and controls
483 lines (413 loc) · 19.9 KB
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
package com.example.ikoala.ui.social;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Pair;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import com.example.ikoala.R;
import com.example.ikoala.logger.Log;
import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
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.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.libraries.places.api.Places;
import com.google.android.libraries.places.api.net.PlacesClient;
import com.google.android.material.tabs.TabLayout;
import com.google.gson.Gson;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class LocalOpportunitiesTabMapFragment extends Fragment implements OnMapReadyCallback {
private Location mLastKnownLocation;
private CameraPosition mCameraPosition;
private static final String KEY_CAMERA_POSITION = "camera_position";
private static final String KEY_LOCATION = "location";
private ArrayList<LocalOpportunitiesTabMapFragment.MapSearchFormattedOutput> mapResultsList = new ArrayList<>();
private FusedLocationProviderClient mFusedLocationProviderClient;
private static final String PLACES_API_BASE = "https://maps.googleapis.com/maps/api/place/";
private static final String API_KEY = "AIzaSyBBSnAZOqmavii0imK03cioNooWPZT1KZk";
private static final String TYPE_SEARCH = "nearbysearch";
private static final String OUT_JSON = "/json?";
private static String searchType = ""; //request changing for each desired search type
//Map location variables (includes places api)
Integer responseCode = null;
String responseMessage = "";
private GoogleMap mMap;
private PlacesClient mPlacesClient;
private static final int DEFAULT_ZOOM = 15;
private static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
private static final int MAP_SERACH_RADIUS = 1000;
private boolean mLocationPermissionGranted;
private Location lastKnownLocation;
private TableLayout mTableLayout;
private Pair<Double, TableRow> itemPair;
private ArrayList<Pair<Double, TableRow>> itemList;
private FragmentActivity myContext;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
Bundle passedBundle = getArguments();
if(passedBundle != null){
if(passedBundle.containsKey("mapSearchTerm")){
searchType = passedBundle.getString("mapSearchTerm");
}
}
View view = inflater.inflate(R.layout.fragment_tab_localopportunities_map,container,false);
mTableLayout = view.findViewById(R.id.mapListTable);
mTableLayout.setStretchAllColumns(true);
if (savedInstanceState != null) {
mLastKnownLocation = savedInstanceState.getParcelable(KEY_LOCATION);
mCameraPosition = savedInstanceState.getParcelable(KEY_CAMERA_POSITION);
}
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(myContext);
return view;
}
@Override
public void onStart() {
super.onStart();
//Setup map
SupportMapFragment mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(LocalOpportunitiesTabMapFragment.this);
Places.initialize(myContext.getApplicationContext(), API_KEY);
mPlacesClient = Places.createClient(myContext);
}
@Override
public void onAttach(Context context) {
myContext = (FragmentActivity) context;
itemList = new ArrayList<>();
super.onAttach(context);
}
/**
* Google maps api code below
*/
//This method is automatically called when the map is setUp
@Override
public void onMapReady(GoogleMap map) {
mMap = map;
// Use a custom info window adapter to handle multiple lines of text in the
// info window contents.
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
// Return null here, so that getInfoContents() is called next.
public View getInfoWindow(Marker arg0) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
// // Inflate the layouts for the info window, title and snippet.
// View infoWindow = getLayoutInflater().inflate(R.layout.custom_info_contents,
// (FrameLayout) findViewById(R.id.map), false);
//
// TextView title = infoWindow.findViewById(R.id.title);
// title.setText(marker.getTitle());
//
// TextView snippet = infoWindow.findViewById(R.id.snippet);
// snippet.setText(marker.getSnippet());
// return infoWindow;
return null;
}
});
// Prompt the user for permission.
getLocationPermission();
updateLocationUI();
// Get the current location of the device and set the position of the map
// Also make call to places api to get location data
try {
if (mLocationPermissionGranted) {
Task<Location> locationResult = mFusedLocationProviderClient.getLastLocation();
locationResult.addOnCompleteListener(myContext, new OnCompleteListener<Location>() {
@Override
public void onComplete(@NonNull Task<Location> task) {
if (task.isSuccessful()) {
// Set the map's camera position to the current location of the device.
mLastKnownLocation = task.getResult();
//saving the values of the last known location to a global variable
lastKnownLocation = mLastKnownLocation;
if (mLastKnownLocation != null) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mLastKnownLocation.getLatitude(),
mLastKnownLocation.getLongitude()), DEFAULT_ZOOM));
StringBuilder sb;
//Forum the url to send for local opportunities data
sb = new StringBuilder(PLACES_API_BASE);
sb.append(TYPE_SEARCH);
sb.append(OUT_JSON);
sb.append("location=" + mLastKnownLocation.getLatitude() + "," + mLastKnownLocation.getLongitude());
sb.append("&radius=" + MAP_SERACH_RADIUS);
sb.append("&type=" + searchType);
sb.append("&key=" + API_KEY);
URL mapUrl = null;
try {
mapUrl = new URL(sb.toString());
} catch (MalformedURLException e) {
Log.e("localOpportunities", "ERROR converting String to URL " + e.toString());
return;
}
//Execute the async call to the api endpoint to retrieve location data
RetrievePlaces mapsTask = new RetrievePlaces();
mapsTask.execute(mapUrl);
}
}
}
});
}
} catch (SecurityException e) {
android.util.Log.e("Exception: %s", e.getMessage());
}
}
//Async class used to get Map location data
class RetrievePlaces extends AsyncTask<URL, String, ArrayList<LocalOpportunitiesTabMapFragment.MapSearchFormattedOutput> > {
protected ArrayList<LocalOpportunitiesTabMapFragment.MapSearchFormattedOutput> doInBackground(URL... urls) {
StringBuilder jsonResults = new StringBuilder();
HttpURLConnection conn = null;
try {
URL url = urls[0];
conn = (HttpURLConnection) url.openConnection();
responseCode = conn.getResponseCode();
responseMessage = conn.getResponseMessage();
InputStreamReader in = new InputStreamReader(conn.getInputStream());
int read;
char[] buff = new char[1024];
while ((read = in.read(buff)) != -1) {
jsonResults.append(buff, 0, read);
}
}catch (IOException e) {
android.util.Log.e("localOpportunities", "Http connection ERROR " + e.toString());
} finally {
if (conn != null) {
conn.disconnect();
}
}
try{
if(responseCode != null && responseCode == 200){
//Process the returned JSON object
JSONObject jsonObject = new JSONObject(jsonResults.toString());
JSONArray resultsJsonArray = jsonObject.getJSONArray("results");
LocalOpportunitiesTabMapFragment.MapSearchFormattedOutput filteredOutput;
for(int i = 0; i < resultsJsonArray.length(); i++){
Gson g = new Gson();
filteredOutput = g.fromJson(resultsJsonArray.getJSONObject(i).toString(), LocalOpportunitiesTabMapFragment.MapSearchFormattedOutput.class);
mapResultsList.add(filteredOutput);
}
return mapResultsList;
} else{
String errorMsg = "Http ERROR response " + responseMessage + "\n" + "Are you online ? " + "\n";
android.util.Log.e("localOpportunities", errorMsg);
return null;
}
}catch (JSONException e) {
android.util.Log.e("localOpportunities", "Http Response ERROR " + e.toString());
}
return null;
}
protected void onPostExecute(ArrayList<LocalOpportunitiesTabMapFragment.MapSearchFormattedOutput> places) {
if(places != null && places.size() != 0){
places.forEach(item -> {
LocalOpportunitiesTabMapFragment.MapSearchFormattedOutput.LocationGeometry geometry = item.getGeometry();
LatLng latLng = new LatLng(geometry.location.lat, geometry.location.lng);
//Adding the location to a list of locations
String name = item.getName();
double distance = CalculateDistanceToPoint(
lastKnownLocation.getLatitude(),
lastKnownLocation.getLongitude(),
latLng.latitude,
latLng.longitude);
distance = Math.round(distance * 100) / 100.0; //rounding to two dp
String type = searchType;
final TextView facilityName = new TextView(myContext);
facilityName.setMaxWidth(20);
facilityName.setGravity(Gravity.CENTER);
facilityName.setBackgroundColor(Color.parseColor("#FFFFFF"));
facilityName.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
facilityName.setText(name);
final TextView facilityDistance = new TextView(myContext);
facilityDistance.setMaxWidth(10);
facilityDistance.setGravity(Gravity.CENTER);
facilityDistance.setBackgroundColor(Color.parseColor("#f7f7f7"));
facilityDistance.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
facilityDistance.setText(Double.toString(distance));
final TextView facilityType = new TextView(myContext);
facilityType.setGravity(Gravity.CENTER);
facilityType.setLayoutParams(new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
facilityType.setBackgroundColor(Color.parseColor("#FFFFFF"));
facilityType.setText(type);
final TableRow tr = new TableRow(myContext);
tr.addView(facilityName);
tr.addView(facilityDistance);
tr.addView(facilityType);
itemList.add(new Pair<>(distance, tr));
//Adding the location as a marker to Map
mMap.addMarker(new MarkerOptions()
.title(item.getName())
.position(latLng)
.snippet(item.getVicinity()));
});
}
//Once everything is done, add rows to table
itemList.sort(new Comparator<Pair<Double, TableRow>>()
{
@Override
public int compare(Pair<Double, TableRow> o1, Pair<Double, TableRow> o2)
{
if(o1.first < o2.first){
return -1;
} else if(o1.first.equals(o2.first)){
return 0;
}else {
return 1;
}
}
});
if(mTableLayout.getChildCount() == 1)
{
itemList.forEach(item -> {
mTableLayout.addView(item.second);
//separator row
final TableRow trSep = new TableRow(myContext);
TextView tvSep = new TextView(myContext);
TableRow.LayoutParams tvSepLay = new
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT);
tvSepLay.span = 3;
tvSep.setLayoutParams(tvSepLay);
tvSep.setBackgroundColor(Color.parseColor("#000000"));
tvSep.setHeight(2);
trSep.addView(tvSep);
mTableLayout.addView(trSep);
});
}
}
}
@Override
public void onPause(){
super.onPause();
//or try view
// mTableLayout.removeAllViews();
}
//Requesting location permission from mobile device
private void getLocationPermission() {
if (ContextCompat.checkSelfPermission(this.myContext.getApplicationContext(),
android.Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
} else {
ActivityCompat.requestPermissions(myContext,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
//Show user as blue dot on map
private void updateLocationUI() {
if (mMap == null) {
return;
}
try {
if (mLocationPermissionGranted) {
mMap.setMyLocationEnabled(true);
mMap.getUiSettings().setMyLocationButtonEnabled(true);
} else {
mMap.setMyLocationEnabled(false);
mMap.getUiSettings().setMyLocationButtonEnabled(false);
mLastKnownLocation = null;
getLocationPermission();
}
} catch (SecurityException e) {
android.util.Log.e("Exception: %s", e.getMessage());
}
}
//Save state of map when activity is paused
@Override
public void onSaveInstanceState(Bundle outState) {
if (mMap != null) {
outState.putParcelable(KEY_CAMERA_POSITION, mMap.getCameraPosition());
outState.putParcelable(KEY_LOCATION, mLastKnownLocation);
super.onSaveInstanceState(outState);
}
}
//Callback of locationPermission request
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
mLocationPermissionGranted = false;
switch (requestCode) {
case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mLocationPermissionGranted = true;
}
}
}
updateLocationUI();
}
//Classes used for formatting output of MapSearch
public static class MapSearchFormattedOutput {
private String name;
private String vicinity;
private LocalOpportunitiesTabMapFragment.MapSearchFormattedOutput.LocationGeometry geometry;
public String getName() {return name;}
public String getVicinity() {return vicinity;}
public LocalOpportunitiesTabMapFragment.MapSearchFormattedOutput.LocationGeometry getGeometry() {return geometry;}
static class LocationGeometry{
private LocalOpportunitiesTabMapFragment.MapSearchFormattedOutput.LocationGeometry.LocationObject location;
static class LocationObject{
double lat;
double lng;
}
public LocalOpportunitiesTabMapFragment.MapSearchFormattedOutput.LocationGeometry.LocationObject getLocation(){ return location;}
}
}
/**
* Calculate the distance between two points using longitude and latitude
* @return the distance in miles as a double value
* Adaptation of the open source code available here: https://www.geodatasource.com/developers/java
*/
public static double CalculateDistanceToPoint(double lat1, double long1, double lat2, double long2){
if((lat1 == lat2) && (long1 == long2)){
return 0.0;
}
else{
double theta = long1 - long2;
double distance = Math.sin(Math.toRadians(lat1)) * Math.sin(Math.toRadians(lat2)) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(theta));
distance = Math.acos(distance);
distance = Math.toDegrees(distance);
distance = distance * 60 * 1.1515; //distance is in miles
return distance;
}
}
}