移动地图时如何更新标记?

时间:2017-10-07 03:21:54

标签: android google-maps algolia

我正在使用Algolia的InstantSearch Android库。

这是我的查询:

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

但是当我移动地图时,标记会保留在原位。移动地图位置视图时,如何在地图上显示新标记?

1 个答案:

答案 0 :(得分:4)

所以基本上你想听地图的变化,每次用户拖动地图并重新加载结果?这是你应该做的:

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))
    }
}