2017-10-07 50 views
5

我正在使用Algolia的InstantSearch Android庫。如何在移動地圖時更新標記?

這是我的查詢:

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

但是,當我移動地圖,標記留在原地。當我移動地圖位置視圖時,如何在地圖上顯示新標記?

+0

嗨Thofiq,歡迎來到堆棧溢出!請記住接受/提出解決問題的答案,獎勵需要時間幫助您的人,並讓其他人知道這有幫助;) – PLNech

回答

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

顯示錯誤:無法解析符號GeoPoint –

+0

我的不好。將變量'geoPoint'更改爲'center'。並導入'import com.google.maps.GeoPoint;'我在代碼中進行了更改,現在應該可以工作 –

+0

謝謝,但標記是連續閃爍的。並且我添加了Toast消息(對於lat lng值)也連續顯示。我正在循環,然後如何阻止它 –

相關問題