1

我如何才能將標記當前位置動畫標記當前位置上的位置改變

我已經有兩個固定的地緣位置

這裏經歷是我用什麼:

private void autoAnimateMarker() { 

    mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 

    mMap.getUiSettings().setZoomControlsEnabled(true); 

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return; 
    } 

    mMap.setMyLocationEnabled(true); 

    LatLng fromLocation = new LatLng(-33.904438, 151.249852); 
    LatLng toLocation = new LatLng(-33.905823, 151.252422); 
    Marker marker = mMap.addMarker(new MarkerOptions().position(fromLocation)); 
    MarkerAnimation.animateMarkerToICS(marker, toLocation, new LatLngInterpolator.Spherical()); 

    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(fromLocation, 17F)); 
} 

然後稱之爲onMapReady(...)

@Override 
public void onMapReady(GoogleMap googleMap) { 
    mMap = googleMap; 

    autoAnimateMarker(); 

} 

,以顯示當前位置

我已經走過了 - https://stackoverflow.com/a/34582595/3585072

目前onLocationChanged(位置定位)方法是這樣的,我需要的基於位置的變化放到這裏,動態地移動我的標記:

@Override 
public void onLocationChanged(Location location) 
{ 
    Toast.makeText(this, "Location Changed " + location.getLatitude() 
      + location.getLongitude(), Toast.LENGTH_LONG).show(); 

    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 = mGoogleMap.addMarker(markerOptions); 

    //move map camera 
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,11)); 
} 
+0

什麼你的意思是「只有當我動」你的意思是當光標移動,還是點擊其他地理位置? –

+0

@KingReload「只有當我移動時」意味着根據我現在的位置它應該移動,就像我駕駛一輛汽車並從位置A開始並且目標是到達位置B ...所以我希望這個飛機標記應該隨着移動我...現在清楚了嗎? – Sophie

+0

定期收聽您的當前位置,並在您的位置發生變化時移動標記。我無法理解你到底想做什麼。 –

回答

2

這是我所提供的final solution獲取,更新和動畫標記current location

LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude()); 

if(ourGlobalMarker == null) { // First time adding marker to map 
    ourGlobalMarker = mGoogleMap.addMarker(new MarkerOptions().position(latLng) 
      .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_ROSE))); 
    MarkerAnimation.animateMarkerToICS(ourGlobalMarker, latLng, new LatLngInterpolator.Spherical()); 
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18)); 
} else { 
    MarkerAnimation.animateMarkerToICS(ourGlobalMarker, latLng, new LatLngInterpolator.Spherical()); 
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 18)); 
} 

Feel free to use this code and let me know if you need any update on this.

+0

我剛剛更新了您的問題的標題...請接受這些更改。所以我希望你能在這裏看到更多的觀點和答案,因爲我猜想目前的標題混淆。 –

+0

但我想等一些其他答案 – Sophie

+0

我不確定animateMarkerToICS會做什麼。爲什麼在這裏的每個位置更新添加一個新標記?如果您更改onLocationChanged中已經初始化的marker-in-onMapReady的位置,該怎麼辦?是否有必要出於某種原因?我認爲它會在你的軌跡上創建多個標記。 –

0

做一件事情把一個後臺線程,每2秒重複一次。刪除前一個標記並在此線程內設置標記。這是我認爲的一種可能的方式。如果您移動的經度和緯度也發生變化,標記會每2秒移動一次。

0

當您的位置發生變化並在地圖上再次添加標記時,無需移除標記。您可以如下設置標記的位置。

首先,請參閱: https://gist.github.com/broady/6314689

如果位置更新間隔大於〜3秒

public void onLocationChanged(Location location) { 
    LatLng toLocation = new LatLng(location.getLatitude(), location.getLongitude()); 
    if (fromLocation != null) { 
     mAnchorMarker.setPosition(fromLocation); 
     MarkerAnimation.animateMarkerToICS(mAnchorMarker, toLocation, new LatLngInterpolator.Spherical()); 
    } 
    fromLocation = toLocation; 
} 

如果位置更新間隔太短(不要動畫,只是舉標記):

public void onLocationChanged(Location location) { 
    LatLng toLocation = new LatLng(location.getLatitude(), location.getLongitude()); 
    mAnchorMarker.setPosition(toLocation); 
} 

Yo你應該在onMapReady而不是onLocationChanged初始化標記。