-
Notifications
You must be signed in to change notification settings - Fork 1
/
Patient_Emergency.java
255 lines (233 loc) · 9.65 KB
/
Patient_Emergency.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
package com.example.loginpage;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.PatternMatcher;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
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.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
public class Patient_Emergency extends AppCompatActivity {
Spinner spType;
Button btFind;
SupportMapFragment supportMapFragment;
GoogleMap map;
FusedLocationProviderClient fusedLocationProviderClient;
double currentLat = 0, currentLong = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_patient_emergency);
// ActivityCompat.requestPermissions(this,
// new String[ACCESS_CO]);
//Assign variable
spType = findViewById(R.id.sp_type);
btFind = findViewById(R.id.bt_find);
supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.google_map);
//Initilize array of place type
String[] placeTypeList = {"blood_bank", "cemetery", "clinic", "hospital"};
//Initilize array of place name
String[] placeNameList = {"Blood Bank", "Cemetery", "Clinic", "Hospital"};
//set adapter on spinner
spType.setAdapter(new ArrayAdapter<>(Patient_Emergency.this
, android.R.layout.simple_spinner_dropdown_item, placeNameList));
//Initialize fused location provider client
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
//check permission
if (ActivityCompat.checkSelfPermission(Patient_Emergency.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
//when permission granted
//call method
getCurrentLocation();
} else {
//when permission denied
//request paemission
ActivityCompat.requestPermissions(Patient_Emergency.this
, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
}
btFind.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Get selected position of spinner
int i = spType.getSelectedItemPosition();
//Initialize url
String url = "https://maps.googleapis.com/maps/api/..." + "?location=" + currentLat + "," + currentLong + "&radius=5000" + "&type=" + placeTypeList[i] + "&sensor=true" + "&key=" + getResources().getString(R.string.google_map_key);
//Execute place task method to download json data
new PlaceTask().execute(url);
}
});
}
private void getCurrentLocation() {
//Initialize task location
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Task<Location> task = fusedLocationProviderClient.getLastLocation();
task.addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if(location != null)
{
//When location is not equal to null
//get current latitude
currentLat = location.getLatitude();
//get current longitude
currentLong = location.getLongitude();
//sync map
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
//when map is ready
map = googleMap;
//zoom current Location on map;
map.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(currentLat,currentLong),10
));
}
});
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 44) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//when permission granted
//Call method
getCurrentLocation();
}
}
}
private class PlaceTask extends AsyncTask<String,Integer,String>
{
@Override
protected String doInBackground(String... strings)
{
String data = null;
//Initialize data
try {
data = downloadUrl(strings[0]);
} catch (IOException e) {
e.printStackTrace();
}
return data;
}
@Override
protected void onPostExecute(String s) {
//Execute parset task
new ParserTask().execute(s);
}
}
private String downloadUrl(String string) throws IOException
{
//Initialize url
URL url = new URL(string);
//Initialize connection
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//Connect connection
connection.connect();
//Initialize input stream
InputStream stream = connection.getInputStream();
//Initialize buffer reader
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
//Initialize string builder
StringBuilder builder = new StringBuilder();
//Initialize string variable
String line = "";
//Use while loop
while ((line = reader.readLine()) != null)
{
//Append line
builder.append(line);
}
//Get append data
String data = builder.toString();
//close reader
reader.close();
//return data
return data;
}
private class ParserTask extends AsyncTask<String,Integer, List<HashMap<String,String>>>
{
@Override
protected List<HashMap<String, String>> doInBackground(String... strings) {
//Create jason parser class
JsonParser jsonParser = new JsonParser();
//Initialize hash map list
List<HashMap<String,String>> mapList = null;
JSONObject object = null;
try {
//Initialize json object
object = new JSONObject(strings[0]);
//Parse jason object
mapList = jsonParser.parseResult(object);
} catch (JSONException e) {
e.printStackTrace();
}
//return maplist
return mapList;
}
@Override
protected void onPostExecute(List<HashMap<String, String>> hashMaps) {
//clear map
map.clear();
//use for loop
for(int i=0;i<hashMaps.size();i++)
{
//Initialize hash map
HashMap<String,String> hashMapList =hashMaps.get(i);
//get latitude
double lat = Double.parseDouble(hashMapList.get("lat"));
//get longitude
double lng = Double.parseDouble(hashMapList.get("lng"));
//get name
String name = hashMapList.get("name");
//contact latitude and longitude
LatLng latLng = new LatLng(lat,lng);
//Initialize marker options
MarkerOptions options = new MarkerOptions();
//set position
options.position(latLng);
//set title
options.title(name);
//add marker on map
map.addMarker(options);
}
}
}
}