Do something when I'm inside the circle radius

I am using the Google Maps API.

What I want is when I move to a certain place, and there is a circle that I already have on the map, it will go to another action.

It was:

  • my position: a = (X, Y)

  • The position of the circle is b = (X, Y) radius = 10

  • When I am inside radius b, it will go into another action or do something.

Any ideas?

I already use distanceTo() but still failed.

Edit: here is updated code that still does not work for me:

 public void onLocationChanged(Location location) { float [] distance = new float[]; Location.distanceBetween(location.getLatitude(),location.getLongitude(),-6.xxxxxxx,106.xxxxxxx,distance); if (distance[0] < 50) { Intent i = new Intent(student_activity.this,indoor.class); student_activity.this,startActivity(i); } } 

This setUpMapIfNeeded () method is called in the onCreate method:

 public void circle() { Circle circle = mMap.addCircle (new CircleOptions() .center(new LatLng(-6.xxxxxxx,106.xxxxxxx)) .radius(50) .strokeColor(Color.RED) .strokeWidth(1) .fillColor(Color.RED) } 
+6
source share
2 answers

You can use a geo object, but if you are just trying to determine if your current location is within the circle drawn on the map, you can use this technique.

Just compare the distance from your current location to the center of the circle and see if its radius is smaller.

I answered a similar question here , where you can see more information and example screenshots.

Note that the code below uses the one without onMyLocationChangeListener , but you can use any type of location callback for this, including FusedLocationProviderApi.

 googleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { @Override public void onMyLocationChange(Location location) { float[] distance = new float[2]; Location.distanceBetween( location.getLatitude(), location.getLongitude(), mCircle.getCenter().latitude, mCircle.getCenter().longitude, distance); if( distance[0] < mCircle.getRadius() ){ //current location is within circle //start new activity Intent i = new Intent(ThisActivity.this, OtherActivity.class); ThisActivity.this.startActivity(i); } } }); 

Edit: here is a fully functional and verified class that uses FusedLocationApi to get the current location:

 import android.content.Intent; import android.graphics.Color; import android.location.Location; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; 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.SupportMapFragment; 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.LatLngBounds; import com.google.android.gms.maps.model.MarkerOptions; public class MapsActivity extends ActionBarActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener { private GoogleMap mMap; LocationRequest mLocationRequest; GoogleApiClient mGoogleApiClient; Circle mCircle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_maps); setUpMapIfNeeded(); buildGoogleApiClient(); mGoogleApiClient.connect(); } @Override protected void onResume() { super.onResume(); setUpMapIfNeeded(); } @Override protected void onPause(){ super.onPause(); if (mGoogleApiClient != null) { LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); } } protected synchronized void buildGoogleApiClient() { Toast.makeText(this,"buildGoogleApiClient",Toast.LENGTH_SHORT).show(); mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); } @Override public void onConnected(Bundle bundle) { Toast.makeText(this,"onConnected", Toast.LENGTH_SHORT).show(); mLocationRequest = new LocationRequest(); mLocationRequest.setInterval(10); mLocationRequest.setFastestInterval(10); mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY); mLocationRequest.setSmallestDisplacement(0.1F); LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); } private void setUpMapIfNeeded() { // Do a null check to confirm that we have not already instantiated the map. if (mMap == null) { // Try to obtain the map from the SupportMapFragment. mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) .getMap(); // Check if we were successful in obtaining the map. if (mMap != null) { setUpMap(); } } } private void setUpMap() { mMap.getUiSettings().setMapToolbarEnabled(true); mMap.getUiSettings().setZoomControlsEnabled(true); mMap.setMyLocationEnabled(true); circle(); } public void circle() { double radiusInMeters = 50.0; int strokeColor = 0xffff0000; //red outline int shadeColor = 0x44ff0000; //opaque red fill mCircle = mMap.addCircle (new CircleOptions() .center(new LatLng(37.9614, -122.105)) .radius(radiusInMeters) .fillColor(shadeColor) .strokeColor(strokeColor) .strokeWidth(1)); } @Override public void onConnectionSuspended(int i) { Toast.makeText(this,"onConnectionSuspended",Toast.LENGTH_SHORT).show(); } @Override public void onConnectionFailed(ConnectionResult connectionResult) { Toast.makeText(this,"onConnectionFailed",Toast.LENGTH_SHORT).show(); } @Override public void onLocationChanged(Location location) { Log.d("locationtesting", "lat: " + location.getLatitude() + " lon: " + location.getLongitude()); Toast.makeText(this,"Location Changed",Toast.LENGTH_SHORT).show(); float[] distance = new float[2]; Location.distanceBetween( location.getLatitude(), location.getLongitude(), mCircle.getCenter().latitude, mCircle.getCenter().longitude, distance); if( distance[0] < mCircle.getRadius() ){ //current location is within circle //start new activity Intent i = new Intent(this, OtherActivity.class); startActivity(i); } } } 

Xml layout:

 <fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/map" tools:context=".MapsActivity" android:name="com.google.android.gms.maps.SupportMapFragment" /> 

Dependencies in build.gradle:

 dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.1.1' compile 'com.google.android.gms:play-services:7.0.0' } 

First, I startActivity() test without calling startActivity() to make sure my current location is inside the circle. As you can see in the screenshot, this was:

Map test

Then I added the startActivity() call in the onLocationChanged() , and it launched OtherActivity right after the application started.

+4
source

Sounds like a description of the Google Geolocation API Geofiguration Tutorial

+1
source

Source: https://habr.com/ru/post/987843/


All Articles