-
Notifications
You must be signed in to change notification settings - Fork 1
/
MapsActivity.java
396 lines (306 loc) · 14.7 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
package com.example.loginregister;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;
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.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.Circle;
import com.google.android.gms.maps.model.CircleOptions;
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 org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
public class MapsActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
GoogleMap mMap;
private static final int ERROR_DIALOG_REQUEST = 9001;
//private static final double SAN_MARCOS_LAT =29.892515, SAN_MARCOS_LNG = -97.940393;
private GoogleApiClient mLocationClient;
Marker marker;
Circle shape;
String placesSearchStr;
private static String browserKeyString = "AIzaSyAwTLnPRHf46vE5vnWFLvj_tIWGZbGdLF8";
private static String keyString = "AIzaSyAwTLnPRHf46vE5vnWFLvj_tIWGZbGdLF8";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (servicesOK()) {
setContentView(R.layout.activity_maps);
if (initMap()) {
mLocationClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mLocationClient.connect();
} else {
Toast.makeText(MapsActivity.this, "Map not connected", Toast.LENGTH_SHORT).show();
}
} else {
setContentView(R.layout.activity_main);
}
}
public boolean servicesOK() {
GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
int isAvailable = googleAPI.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (googleAPI.isUserResolvableError(isAvailable)) {
Dialog dialog = googleAPI.getErrorDialog(this, isAvailable, ERROR_DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(MapsActivity.this, "Can't Connect to mapping service", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
mMap.getUiSettings().setZoomControlsEnabled(true);
}
return (mMap != null); // Tells whether the initialization process was successful
}
private void gotoLocation(double lat, double lng, float zoom) {
LatLng latLng = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
mMap.animateCamera(update);
}
private void removeEverything() {
marker.remove();
marker = null;
shape.remove();
shape = null;
}
public void updateHospitals(double lat, double lng) {
String types = "hospital|doctor|health";
try {
types = URLEncoder.encode(types, "UTF-8");
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
placesSearchStr = "https://maps.googleapis.com/maps/api/place/nearbysearch/" +
"json?location=" + lat + "," + lng +
"&radius=5000&sensor=true" +
"&types=" + types +
"&key=" + browserKeyString;
new GetPlaces().execute(placesSearchStr);
}
public void hideSoftKeyboard(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
Drawable drawable = menu.findItem(R.id.currentLocation).getIcon();
if (drawable != null) {
drawable.mutate();
drawable.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_ATOP);
}
return true;
}
public void showCurrentLocation(MenuItem item) throws IOException {
//noinspection ResourceType
Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(mLocationClient);
if (currentLocation == null) {
Toast.makeText(MapsActivity.this, "Couldn't connect!", Toast.LENGTH_SHORT).show();
} else {
LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, 15);
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);
if (list.size() > 0) {
Address ad = list.get(0);
String locality = ad.getLocality();
Toast.makeText(this, "Found " + locality, Toast.LENGTH_SHORT).show();
mMap.animateCamera(update);
MarkerOptions options = new MarkerOptions()
.title(locality)
.snippet("Your current location")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.position(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()));
CircleOptions circleOptions = new CircleOptions()
.strokeWidth(3)
.fillColor(0x330000FF)
.strokeColor(Color.BLUE)
.center(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()))
.radius(5000);
mMap.addMarker(options);
shape = mMap.addCircle(circleOptions);
}
updateHospitals(currentLocation.getLatitude(), currentLocation.getLongitude());
}
}
public void geoLocate(View v) throws IOException {
hideSoftKeyboard(v);
TextView tv = (TextView) findViewById(R.id.editText1);
String searchString = tv.getText().toString();
Geocoder gc = new Geocoder(this);
List<Address> list = gc.getFromLocationName(searchString, 1);
if (list.size() > 0) {
Address ad = list.get(0);
String locality = ad.getLocality();
Toast.makeText(this, "Found " + locality, Toast.LENGTH_SHORT).show();
double lat = ad.getLatitude();
double lng = ad.getLongitude();
gotoLocation(lat, lng, 15);
MarkerOptions options = new MarkerOptions()
.title(locality)
.snippet("Your location")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.position(new LatLng(lat, lng));
CircleOptions circleOptions = new CircleOptions()
.strokeWidth(3)
.fillColor(0x330000FF)
.strokeColor(Color.BLUE)
.center(new LatLng(lat, lng))
.radius(5000);
mMap.addMarker(options);
shape = mMap.addCircle(circleOptions);
updateHospitals(lat, lng);
}
}
@Override
public void onConnected(Bundle bundle) {
Toast.makeText(MapsActivity.this, "Ready to Map", Toast.LENGTH_SHORT).show();
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
private class GetPlaces extends AsyncTask<String, Void, String> {
private Marker[] placeMarkers;
private final int MAX_PLACES = 20;
private MarkerOptions[] places;
@Override
protected String doInBackground(String... placesURL) { //doInBackground method to request and retrieve the data
//fetch places
StringBuilder placesBuilder = new StringBuilder();
//process search parameter string(s)
for (String placeSearchURL : placesURL) {
//execute search
HttpClient placesClient = new DefaultHttpClient();
try {
//try to fetch the data
HttpGet placesGet = new HttpGet(placeSearchURL);
HttpResponse placesResponse = placesClient.execute(placesGet);
StatusLine placeSearchStatus = placesResponse.getStatusLine();
if (placeSearchStatus.getStatusCode() == 200) {
//we have an OK response
HttpEntity placesEntity = placesResponse.getEntity();
InputStream placesContent = placesEntity.getContent(); // retrieving the JSON string content
InputStreamReader placesInput = new InputStreamReader(placesContent);
BufferedReader placesReader = new BufferedReader(placesInput);
String lineIn;
while ((lineIn = placesReader.readLine()) != null) {
placesBuilder.append(lineIn);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return placesBuilder.toString();
}
//Implement the onPostExecute method to parse the JSON string returned from doInBackground
protected void onPostExecute(String result) {
//parse place data returned from Google Places
placeMarkers = new Marker[MAX_PLACES];
//remove any existing markers on the map
if (placeMarkers != null) {
for (int pm = 0; pm < placeMarkers.length; pm++) {
if (placeMarkers[pm] != null)
placeMarkers[pm].remove();
}
}
try {
//parse JSON
JSONObject resultObject = new JSONObject(result);
//the places are contained within an array named "results"
JSONArray placesArray = resultObject.getJSONArray("results");
places = new MarkerOptions[placesArray.length()]; //This should give us a MarkerOptions object for each place returned
//loop through places
for (int p = 0; p < placesArray.length(); p++) {
//parse each place
//If any of the values are missing in the returned JSON feed, we will simply not
//display a Marker for that place, in case of Exceptions. To keep track of this, add a boolean flag
boolean missingValue = false;
LatLng placeLL = null;
String placeName = "";
String vicinity = "";
try {
//attempt to retrieve place data values
missingValue = false;
JSONObject placeObject = placesArray.getJSONObject(p);
JSONObject loc = placeObject.getJSONObject("geometry").getJSONObject("location");
placeLL = new LatLng(
Double.valueOf(loc.getString("lat")),
Double.valueOf(loc.getString("lng")));
vicinity = placeObject.getString("vicinity");
placeName = placeObject.getString("name");
} catch (JSONException jse) {
missingValue = true;
jse.printStackTrace();
}
//check that value and set the place MarkerOptions object to null,
// so that we don't attempt to instantiate any Marker objects with missing data
if (missingValue)
places[p] = null;
else
places[p] = new MarkerOptions()
.position(placeLL)
.title(placeName)
.snippet(vicinity);
}
} catch (Exception e) {
e.printStackTrace();
}
if (places != null && placeMarkers != null) {
for (int p = 0; p < places.length && p < placeMarkers.length; p++) {
//will be null if a value was missing
if (places[p] != null)
placeMarkers[p] = mMap.addMarker(places[p]);
}
}
}
}
}