Ich versuchte dies, aber es zeigt meine aktuelle Position beim ersten Mal ich bekomme Informationen, dass ich drinnen bin oder nicht. aber wenn ich mich außerhalb des Radius von Punkt 1 bewegte, zeigt es nichts.Wie aktualisiere ich meinen Standort, wenn ich mich bewege und es zeigen muss, dass Sie in Punkt 1 Radius sind oder nicht?
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
//drawing marker
LatLng pointOne = new LatLng(23.10,72.50847684);
mMap.addMarker(new MarkerOptions().position(pointOne).title("Point 1"));
mMap.addMarker(new MarkerOptions().position(new LatLng(0,0)).title("Point 2").snippet("N"));
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mMap.setMyLocationEnabled(true);
// Get LocationManager object from System Service LOCATION_SERVICE
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Create a criteria object to retrieve provider
Criteria criteria = new Criteria();
// Get the name of the best provider
String provider = locationManager.getBestProvider(criteria, true);
// Get Current Location
Location myLocation = locationManager.getLastKnownLocation(provider);
//myLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double latitude = myLocation.getLatitude();
Log.e("latitude", String.valueOf(latitude));
// Get longitude of the current location
double longitude = myLocation.getLongitude();
// Create a LatLng object for the current location
LatLng pointTwo = new LatLng(latitude, longitude);
// Show the current location in Google Map
mMap.moveCamera(CameraUpdateFactory.newLatLng(pointTwo)); Log.e("location", String.valueOf(pointTwo));
//drawing overlay circle on marker
Circle circle = mMap.addCircle(new CircleOptions()
.center(pointOne)
.radius(5000)
.strokeColor(0x40FF0000)
.fillColor(0x35FF0000));
//for Checking marker is inside or not
float[] distance = new float[2];
Location.distanceBetween(pointTwo.latitude, pointTwo.longitude,
circle.getCenter().latitude, circle.getCenter().longitude, distance);
if(distance[0] > circle.getRadius() ){
Toast.makeText(getBaseContext(), "Outside", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getBaseContext(), "Inside", Toast.LENGTH_LONG).show();
}
pointToPosition(pointOne);
}
Vielen Dank für Hilfe im Voraus
Probieren Sie GeoFencing api -> entspricht Ihrem Bedürfnis, einen virtuellen Zaun zu machen, wo, wenn jemand austritt, ein Ereignis ausgelöst wird – Kushan
ich schätze dass aber ich bin etwas verwirrt, wie man das macht? –
Und ein neues Problem Irgendwann funktioniert es und irgendwann bekommt es Nullpointer Excection –