2017-05-09 19 views
0

我使用谷歌地圖標記爲我當前的位置我能夠檢索當前的經度和緯度的位置,但我怎樣才能得到更新的位置,當它被更改。我編寫了一個跟蹤當前位置的單獨課程,並在主要Activity中調用課程,其中我使用googleApi客戶端來顯示標記,現在問題是如何在此主要活動課程中更改位置時如何更改標記OnLocation更改與谷歌地圖API在Android

+1

分享您的代碼。 – Piyush

+1

你到目前爲止嘗試過什麼? –

+0

您設置了requestLocationChange監聽器和onLocationChange,您可以更新您的標記。 – Meenal

回答

3

就像你正在跟蹤來自不同班級的您的位置。你可以創建自己的監聽器,說來聽聽在主類的位置變化

首先創建位置變化監聽

public interface LocationListener { 
    public void onLocationReceived(Location location); 
} 

在你的電話一樣,

public class MainActivity extends Activity implement LocationListener{ 
    public void onLocationReceived(Location location){ 
    // update your map pin here 
    } 
} 

實現這個監聽器調用您的位置時從mainactivity類通過位置更改的收聽者類似 那樣

LocationClass class = new LocationClass(this); 

在LocationClass添加costructor

LocationListener listener; 
public LocationClass (LocationListener listener){ 
this.listener = listener; 
} 

現在,每當你得到新的位置調用這個函數

this.listener.onLocationReceived(location); 

它會在你的情況下工作。

您有其他的選擇一樣可以實現谷歌的位置監聽器在自己的活動

0

@ user1335906您可以參考以下鏈接以獲得頻繁的位置更新...

https://github.com/googlesamples/android-play-location/tree/master/LocationUpdates 

1)首先您必須使用以下方法連接Google服務:

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(this) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API) 
      .build(); 
} 

2)成功連接到Google服務後,爲您的locationRequest對象設置了usi下面納克法:

public void onConnected(@Nullable Bundle bundle) { 
    mLocationRequest = new LocationRequest(); 
    mLocationRequest.setInterval(1500); // this will give location update after 1500 miliseconds, set it according to your requirement 
    mLocationRequest.setSmallestDisplacement(0.5f); // this will calculate distance between your previous and current location, if it is greater then 0.5 meter this method will be triggered. 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); // set location accuracy level as per app requirement. 
} 

2)您將獲得以下方法頻繁的位置更新:

public void onLocationChanged(Location location) { 
// Here you will get your current location every time when it gets changed, 
// put log here and see current location updates 
} 

所以給它一個嘗試,讓我知道,如果你沒有得到它...

0

通常,當您添加標記時,您可以參考標記。

Marker marker = mGoogleMap.addMarker(...) 

所以,當你收到的位置變化,則只是單純地改變它的位置

marker.setPosition(new LatLng(...)); 
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngBounds(...)); 

這將動畫地圖移動到的位置變化,但不會隨着標記動畫。如果距離很遠,標記從一個地方跳到另一個地方。所以,如果你想動畫標記,那麼使用ValueAnimator動畫標記。