How to update markers when moving a map?

I am using Algolia InstantSearch for Android.

This is my request:

searcher.setQuery(new Query().setAroundLatLngViaIP(true).setAroundRadius(5000)). 

But when I move the map, the markers stay in place. How can I display new markers on a map when I move the map location view?

+5
source share
1 answer

So basically you want to listen to the map changes and every time the user drags the map and reloads the results? Here is what you should do:

 public class AwesomeCameraActivity extends FragmentActivity implements OnCameraIdleListener, OnMapReadyCallback { private GoogleMap mMap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_camera); SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() .findFragmentById(R.id.map); mapFragment.getMapAsync(this); } @Override public void onMapReady(GoogleMap map) { mMap = map; mMap.setOnCameraIdleListener(this); // Show the initial position mMap.moveCamera(CameraUpdateFactory .newLatLngZoom(new LatLng(YOUR_LATITUDE, YOUR_LONGITUDE), 10)); } @Override public void onCameraIdle() { // The camera / map stopped moving, now you can fetch the center of the map GeoPoint center = mMap.getProjection().fromPixels(mMap.getHeight() / 2, mMap.getWidth() / 2); double centerLat = (double)center.getLatitudeE6() / (double)1E6; double centerLng = (double)center.getLongitudeE6() / (double)1E6; // Now fire a new search with the new latitude and longitude searcher.setQuery(new Query().setAroundLatLng(new AbstractQuery.LatLng(centerLat, centerLng)).setAroundRadius(5000)) } } 
+4
source

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


All Articles