问题
Am trying to set up an app that would notify the user when his/her location is with in the polygon. I have read about using polyutils library but have not been successful in implementing them.
Here is what i Tried
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
mMap.setMapType(mMap.MAP_TYPE_SATELLITE);
mMap.moveCamera(CameraUpdateFactory.newLatLng(new LatLng(48.146536, 16.291830)));
mMap.animateCamera(CameraUpdateFactory.zoomTo(18));
DrawPolygon();
// Log.i(TAG, "onLocationChanged: location" + latLng);
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this, null);
Log.d(TAG, "onConnected - isConnected ...............: " + mGoogleApiClient.isConnected());
}
private void DrawPolygon() {
mPolygon1 = mMap.addPolygon(new PolygonOptions()
.strokeColor(COLOR_PURPLE_ARGB)
.add(
new LatLng(48.146536, 16.291830),
new LatLng(48.146772, 16.290146),
new LatLng(48.147397, 16.290380),
new LatLng(48.147177, 16.291970)));
// Store a data object with the polygon, used here to indicate an arbitrary type.
mPolygon1.setTag("alpha");
// Style the polygon.
// stylePolygon(polygon1);
mPolygon2 = mMap.addPolygon(new PolygonOptions()
.strokeColor(COLOR_BLUE_ARGB)
.add(
new LatLng(48.146491, 16.290989),
new LatLng(48.146265, 16.290898),
new LatLng(48.146171, 16.291819),
new LatLng(48.146359, 16.291870)));
mPolygon2.setTag("beta");
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
for (Polygon polgon : getPolygons()) {
if (PolyUtil.containsLocation(latLng, polgon.getPoints(), true)) {
// found it open a info window
Log.i(LOG_TAG, "Your Currently in : " + polgon);
return;
}
}
}
public Collection<Polygon> getPolygons() {
Collection<Polygon> polygons = new ArrayList<Polygon>();
((ArrayList<Polygon>) polygons).add(1, mPolygon1);
((ArrayList<Polygon>) polygons).add(2, mPolygon2);
return polygons;
}
When i run the app the IDE throws an error pointing at the lines indexoutofBounds Exception and the app does not run.
来源:https://stackoverflow.com/questions/56868593/how-can-i-set-up-a-listener-that-would-notify-me-when-my-new-location-is-within