-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMapsActivity.java
More file actions
339 lines (291 loc) · 13.8 KB
/
Copy pathMapsActivity.java
File metadata and controls
339 lines (291 loc) · 13.8 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
package com.commercial.askitloud.japhhi;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface;
import android.location.Location;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
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.BitmapDescriptorFactory;
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.maps.model.Polygon;
import com.google.android.gms.maps.model.PolygonOptions;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.maps.android.PolyUtil;
import java.util.ArrayList;
import static java.lang.Integer.max;
public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback,
GoogleMap.OnMapClickListener,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener,GoogleMap.OnInfoWindowClickListener {
private GoogleMap mMap;
GoogleApiClient mGoogleApiClient;
LatLng station;
int no;
Location mLastLocation;
Marker mCurrLocationMarker;
Marker mPoliceStationMarker;
LocationRequest mLocationRequest;
public static String contactPerson = "Ashutosh Agrawal";
public static String contactNumber = "9454517680";
public ArrayList<LatLng> arrayPoints;
DatabaseReference FireDb;
private boolean checkClick = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
Toolbar mTopToolbar = findViewById(R.id.toolbar);
setSupportActionBar(mTopToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
checkLocationPermission();
}
// 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);
//Firebase Work
FireDb = FirebaseDatabase.getInstance().getReference();
DatabaseReference childRef = FireDb.child("latlngs");
Log.e("tag1","in here"+childRef.toString());
childRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int size = (int)dataSnapshot.getChildrenCount();
Log.e("tag2","in here-"+size);
for (int j = 1; j <= size; j++)
{
arrayPoints = new ArrayList<LatLng>();
final DatabaseReference territoryRef = FireDb.child("latlngs/territory"+j).getRef();
territoryRef.addValueEventListener(new ValueEventListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int length = (int) dataSnapshot.getChildrenCount();
Log.e("tag3", dataSnapshot.getRef().toString() + "-in here-" + length);
for (int k = 1; k < length; k++) {
LatLng value = new LatLng((double) dataSnapshot.child("value" + k).child("x").getValue(), (double) dataSnapshot.child("value" + k).child("y").getValue());
arrayPoints.add(value);
Log.e("value::", value.latitude + "," + value.longitude);
}
Log.e("tag5", "in here");
Double x = (double) dataSnapshot.child("station").child("x").getValue();
Double y = (double) dataSnapshot.child("station").child("y").getValue();
Log.e("tag",x+" , "+y);
station = new LatLng(x,y);
Log.e("tag",station.latitude + "," + station.longitude);
LatLng myLocation = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
Log.e("tagP",String.valueOf(arrayPoints.size()));
if(arrayPoints.size()>3)
checkTerritory(myLocation, station, arrayPoints);
else
Toast.makeText(MapsActivity.this,"Atleast 3 points required to draw a polygon",Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled (@NonNull DatabaseError databaseError){
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//Initialize Google Play Services
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
} else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
mMap.animateCamera(CameraUpdateFactory.zoomTo(30));
mMap.setInfoWindowAdapter(new PopupAdapter(getLayoutInflater()));
mMap.setOnInfoWindowClickListener(this);
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
mLocationRequest = LocationRequest.create();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mCurrLocationMarker != null)
{
mCurrLocationMarker.remove();
}
//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mCurrLocationMarker = mMap.addMarker(markerOptions);
//move map camera
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(30));
//stop location updates
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
public Polygon countPolygonPoints(ArrayList<LatLng> arrayList) {
Polygon polygon = null;
if (arrayList.size() >= 3) {
checkClick = true;
PolygonOptions polygonOptions = new PolygonOptions();
polygonOptions.addAll(arrayList);
polygonOptions.strokeColor(Color.WHITE);
polygonOptions.strokeWidth(4);
polygon = mMap.addPolygon(polygonOptions);
}
else
Toast.makeText(MapsActivity.this,"At least 3 points needed to draw a polygon.",Toast.LENGTH_SHORT).show();
arrayList.clear();
return polygon;
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
}
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 99;
public boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Asking user if explanation is needed
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
}
return false;
} else {
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted. Do the
// contacts-related task you need to do.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (mGoogleApiClient == null) {
buildGoogleApiClient();
}
mMap.setMyLocationEnabled(true);
}
} else {
// Permission denied, Disable the functionality that depends on this permission.
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
}
return;
}
// other 'case' lines to check for other permissions this app might request.
// You can add here other case statements according to your requirement.
}
}
public void checkTerritory(LatLng currLocation,LatLng station,ArrayList<LatLng> arrayList) {
Log.e("TAG_A", "mssg");
LatLng latlng = new LatLng(currLocation.latitude, currLocation.longitude);
if (PolyUtil.containsLocation(latlng,countPolygonPoints(arrayList).getPoints(), true)) {
//show police station of territory
MarkerOptions markerOptions1 = new MarkerOptions();
markerOptions1.position(station);
markerOptions1.title("You are in territory of : ");
markerOptions1.snippet("Admin"+"\n"+"Contact Person :"+contactPerson+"\n"+"Contact No. :"+contactNumber);
markerOptions1.icon(BitmapDescriptorFactory.fromResource(R.drawable.police_img));
mPoliceStationMarker = mMap.addMarker(markerOptions1);
Toast.makeText(this.getBaseContext(), "Click on police icon to get nearest official info.", Toast.LENGTH_LONG).show();
}
}
@Override
public void onInfoWindowClick(Marker marker) {
if(!marker.getTitle().equals("Current Position")) {
Intent phoneIntent = new Intent(Intent.ACTION_DIAL);
phoneIntent.setData(Uri.parse("tel:" +contactNumber));
startActivity(phoneIntent);
}
}
@Override
public void onMapClick(LatLng latLng) {
mMap.setInfoWindowAdapter(null);
}
}