-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTripProgressActivity.java
More file actions
228 lines (195 loc) · 8.75 KB
/
TripProgressActivity.java
File metadata and controls
228 lines (195 loc) · 8.75 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
package com.example.tripprogressmap;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.Polyline;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
public class TripProgressActivity extends AppCompatActivity {
Button shareButton, rateButton, helpButton;
ImageButton tripProgBack;
Bundle tripInfo;
TextView pickup, destin, price, driver, dandt, eta, distance;
MapView mapView;
Polyline routePolyline;
LatLng OL, DL;
GoogleMap gmap;
Marker originMarker, destMarker;
double specificLatitude = 50.112778; // Replace with your desired latitude
double specificLongitude = -120.789719; // Replace with your desired longitude
LatLng specificLatLng = new LatLng(specificLatitude, specificLongitude);
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trip_progress);
OL = specificLatLng;
//OL = TripPassengerFragment.originLatLng;
DL = TripPassengerFragment.destinationLatLng;
shareButton = (Button) findViewById(R.id.shareButton);
rateButton = (Button) findViewById(R.id.rateButton);
helpButton = (Button) findViewById(R.id.helpButton);
pickup = (TextView) findViewById(R.id.pickup);
destin = (TextView) findViewById(R.id.destin);
price = (TextView) findViewById(R.id.price);
driver = (TextView) findViewById(R.id.driver);
dandt = (TextView) findViewById(R.id.dandt);
tripProgBack = (ImageButton) findViewById(R.id.tripProgBack);
eta = (TextView) findViewById(R.id.eta);
distance = (TextView) findViewById(R.id.distance);
tripInfo = getIntent().getExtras();
mapView = (MapView) findViewById(R.id.mapProgress);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync((OnMapReadyCallback) this);
pickup.setText(tripInfo.getString("pickup"));
destin.setText(tripInfo.getString("dest"));
driver.setText(tripInfo.getString("name"));
dandt.setText(tripInfo.getString("dandt"));
price.setText(tripInfo.getString("cost"));
// TODO: set eta, distance
// TODO: make sharebutton and helpbuton do something?
// TODO: edit map
rateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent rateTrip = new Intent(TripProgressActivity.this, RateTripActivity.class);
rateTrip.putExtras(tripInfo);
startActivity(rateTrip);
}
});
shareButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(TripProgressActivity.this, "Trip Info Shared with Saved Contact", Toast.LENGTH_SHORT).show();
}
});
helpButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(TripProgressActivity.this, "SOS sent to Emergency Contact and Police", Toast.LENGTH_SHORT).show();
}
});
tripProgBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(TripProgressActivity.this, MainActivity.class));
}
});
}
//CODE REFERRED FROM https://www.digitalocean.com/community/tutorials/android-google-map-drawing-route-two-points
// ALSO HELP WAS TAKEN FROM https://developers.google.com/maps/documentation/android-sdk/polygon-tutorial API DEVELOPMENT KIT
public void onMapReady(GoogleMap googleMap) {
gmap = googleMap;
originMarker = gmap.addMarker(new MarkerOptions().position(OL).title("Origin"));
destMarker = gmap.addMarker(new MarkerOptions().position(DL).title("Destination"));
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(originMarker.getPosition());
builder.include(destMarker.getPosition());
LatLngBounds bounds = builder.build();
gmap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 100));
String directionsUrl = "https://maps.googleapis.com/maps/api/directions/json" +
"?origin=" + OL.latitude+","+OL.longitude +"&destination=" + DL.latitude+","+DL.longitude + "&key=AIzaSyCGzZ4JWj8C2SMqGkvkuCpbZiIj0lzM9QY";
new FetchDirectionsTask().execute(directionsUrl);
}
private class FetchDirectionsTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
return result.toString();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@SuppressLint("StaticFieldLeak")
@Override
protected void onPostExecute(String directionsData) {
if (directionsData != null) {
Log.d("DirectionsData", directionsData);
drawRoute(directionsData);
}
}
}
private void drawRoute(String directionsData) {
try {
JSONObject jsonObject = new JSONObject(directionsData);
JSONArray routes = jsonObject.getJSONArray("routes");
if (routes.length() > 0) {
JSONObject route = routes.getJSONObject(0);
JSONObject overviewPolyline = route.getJSONObject("overview_polyline");
String encodedPolyline = overviewPolyline.getString("points");
List<LatLng> decodedPolyline = PolyUtil.decode(encodedPolyline);
PolylineOptions options = new PolylineOptions()
.addAll(PolyUtil.decode(encodedPolyline))
.color(Color.BLUE)
.width(10);
routePolyline = gmap.addPolyline(options);
routePolyline = gmap.addPolyline(options);
for (LatLng point : decodedPolyline) {
Log.d("PolylinePoint", "Lat: " + point.latitude + ", Lng: " + point.longitude);
Log.d("Polyline", "Number of points: " + decodedPolyline.size());
}
// Move camera to show both markers and the route
LatLngBounds.Builder builder = new LatLngBounds.Builder();
builder.include(originMarker.getPosition());
builder.include(destMarker.getPosition());
for (LatLng point : decodedPolyline) {
builder.include(point);
}
LatLngBounds bounds = builder.build();
gmap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 200));
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
}