2017-05-02 92 views
0

我創建了我的第一個應用程序,顯示用戶位置信息以及谷歌地圖上顯示的位置。目前的應用程序/地圖完美的作品,因爲我想它除了一個小iggle,地圖不斷跳回到用戶的位置,並放大回到設定值中心用戶在谷歌地圖上的位置,但允許自由移動

我所追求的是更新用戶位置和放大的地圖,但只有當它連接到GPS時纔會這樣做。完成此操作後,用戶已移動地圖或放大或縮小位置仍會顯示,但不會再次居中,或放大/縮小(每更改一次位置),類似於Google地圖應用可以。

目前我在onLocationChanged方法中有以下代碼,我相信我需要移動它或創建一些邏輯來完成我之後的操作。

public void onLocationChanged(Location location) { 

// Update Location On Map 
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 16.0F)); 
} 

這是使用最新的GoogleAPIClient。我看了很多這裏和網上的教程,我似乎無法弄清楚。我相信這將是簡單的修復。

回答

1

只需使用一個布爾標誌,並設置一次地圖有真的後在縮放用戶位置

boolean isFirstLocation=false; 
public void onLocationChanged(Location location) { 

    // Set user marker on the map on every location change with this code 
     LatLng currentLatLng = new LatLng(location.getLatitude(),location.getLongitude()); 
     MarkerOptions markerOptions = new MarkerOptions().position(currentLatLng); 
     mMap.addMarker(markerOptions); 

    // Update Location On Map 
     if(!isFirstLocation) 
    { 
     mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLatLng , 16.0F)); 
    isFirstLocation=true; 
    } 

} 

無需在每個位置更改

+0

謝謝你完美的作品。 –

+0

歡迎@DarrenCashmore – FaisalAhmed

+0

看我已經更新我的答案請使用這個 – FaisalAhmed

0
Marker mCurrLocationMarker; 

@Override 
    public void onLocationChanged(Location location) { 

     mLastLocation = location; 
     if (mCurrLocationMarker != null) { 
      mCurrLocationMarker.remove(); 
     } 

     //Place current location marker 
     LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 
     MarkerOptions markerOptions = new MarkerOptions(); 
     markerOptions.position(latLng); 
     markerOptions.title("Current Position"); 
     markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA)); 
     mCurrLocationMarker = mMap.addMarker(markerOptions); 

     //move map camera 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng)); 
     mMap.animateCamera(CameraUpdateFactory.zoomTo(11)); 

     //stop location updates 
     if (mGoogleApiClient != null) { 
      LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
     } 

    } 

,如果你的位置發生變化,那麼這將自動對焦和變更標記您的當前位置.. 希望這將解決您的問題

+0

謝謝你的回覆動畫的攝像頭,但是這是我使用的示例開始。除了不斷更新位置和居中位置之外,這項工作很好。之後的什麼是它在連接時執行此操作,然後允許地圖自由移動而不會重新居中。 –

相關問題