Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I update the markers when moving the map?

Tags:

I am using Algolia's InstantSearch Android library.

This is my Query:

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

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

like image 530
Thofiq Ahmed Avatar asked Oct 07 '17 03:10

Thofiq Ahmed


People also ask

How do you refresh a marker on Google Maps?

To reload the markers, when you create then, push them to an array. Then create a function where you iterate through the array, setting the markers map as null. After this, erase the array.

How do I move a marker smoothly on Google Maps?

The initialize() function creates a Google Map with a marker. The transition() and moveMarker() are used to move marker smoothly on click on the Google map.

How do you move a map under a marker?

There is an app named UBER and they provide this feature, you don't drag and drop the marker you just move the map and the marker remained it's position. As you can see green marker is on the center of screen when we move the map it remained its place but shows the new position when the user stop moving the map.


1 Answers

So basically you want to listen to the map changes and every time the user is dragging the map and reload the results? Here's 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))
    }
}
like image 193
Robert D. Mogos Avatar answered Oct 14 '22 02:10

Robert D. Mogos