2016-01-21 72 views
2

我正在構建一個android應用程序,我必須從一個日期/時間跟蹤另一個用戶並部署使用多段線的製造商? 作爲對服務器的響應,我在列表中收到多個緯度/經度。現在我想像他們正在播放的視頻一樣一個一個地部署它們,我知道如何添加標記我只想控制那裏部署.E標記A在部署2秒後部署,標記B應該部署並與具有聚標記C,D也是如此。等我應該如何能夠實現這一目標。計時器,線程?任何幫助或參考,算法,將是偉大的,雖然我已經搜索了這樣的例子,我找不到它。 在此先感謝控制標記部署。

回答

0

您可以使用Handler執行此任務。

我張貼了個例子吧,我還沒有運行它,但我希望它會成功運行:

private final Handler mHandler = new Handler(Looper.getMainLooper()); 
private List<LatLng> mLatLngs; 
private PutMarkerRunnable mRunnable; 
private int mNoOfMarkersPlaced = -1; 

private void showMarkers(List<LatLng> latLngs) { 
    mLatLngs = latLngs; 
    mHandler.post(mRunnable); 
} 

private class PutMarkerRunnable implements Runnable { 
    @Override 
    public void run() { 
     // write code to add marker 
     map.addMarker(); 
     mNoOfMarkersPlaced++; 
     if (mNoOfMarkersPlaced != mLatLngs.size()) { 
      // scheduling next marker to be placed after 3 seconds 
      mHandler.postDelayed(mRunnable, 3000); 
     } 

    } 
} 
+0

感謝Bhawna,但我真的很感激處理程序示例應用上的標記deployment.Since我從服務器端接收數據的較大塊說45對LAT/LNGS的..我將如何應用處理程序。 – Nbi

+0

如果你看到它,請將我的答案標記爲正確。謝謝 – Nbi

0

好,謝謝社區..但我一直在終於能夠通過做到這一點。標記它是正確的,因爲這真的對我有用!

首先我們使用動畫我們的相機..

CameraPosition cameraPosition = 
     new CameraPosition.Builder() 
       .target(new LatLng(0,0)) 
       .bearing(45) 
       .tilt(90) 
       .zoom(googleMap.getCameraPosition().zoom) 
       .build(); 

googleMap.animateCamera(
    CameraUpdateFactory.newCameraPosition(cameraPosition), 
    ANIMATE_SPEEED_TURN, 
    new CancelableCallback() { 

     @Override 
     public void onFinish() { 
     } 

     @Override 
     public void onCancel() { 
     } 
    } 
);/** 
* 
* Callback that highlights the current marker and keeps animating to the next marker, providing a "next marker" is still available. 
* If we've reached the end-marker the animation stops. 
* 
*/ 
CancelableCallback simpleAnimationCancelableCallback = 
    new CancelableCallback(){ 

     @Override 
     public void onCancel() { 
     } 

     @Override 
     public void onFinish() { 

      if(++currentPt < markers.size()){ 

       CameraPosition cameraPosition = 
         new CameraPosition.Builder() 
           .target(targetLatLng) 
           .tilt(currentPt<markers.size()-1 ? 90 : 0) 
           //.bearing((float)heading) 
           .zoom(googleMap.getCameraPosition().zoom) 
           .build(); 


       googleMap.animateCamera(
         CameraUpdateFactory.newCameraPosition(cameraPosition), 
         3000, 
         simpleAnimationCancelableCallback); 

       highLightMarker(currentPt); 

      } 
     } 
}; 

可以2個android.location.Location對象之間進行計算的軸承。由於我們正在使用com.google.android.gms.maps.model.LatLng對象,因此我們首先需要將它們轉換爲Location對象。

private Location convertLatLngToLocation(LatLng latLng) { 
    Location location = new Location("someLoc"); 
    location.setLatitude(latLng.latitude); 
    location.setLongitude(latLng.longitude); 
    return location; 
} 

一旦我們有2點位置的對象,我們可以計算出2之間的軸承這是我們需要把在相機上轉換到目標(EndLocation所)時。

private float bearingBetweenLatLngs(LatLng beginLatLng,LatLng endLatLng) { 
    Location beginLocation = convertLatLngToLocation(beginLatLng); 
    Location endLocation = convertLatLngToLocation(endLatLng); 
    return beginLocation.bearingTo(endLocation); 
} 

我們將使用這個數字來計算的中間點的座標。一旦我們有了這些座標,我們就可以將跟蹤標記設置到新的位置。

long elapsed = SystemClock.uptimeMillis() - start; 
double t = interpolator.getInterpolation((float)elapsed/ANIMATE_SPEEED); 

double lat = t * endLatLng.latitude + (1-t) * beginLatLng.latitude; 
double lng = t * endLatLng.longitude + (1-t) * beginLatLng.longitude; 

LatLng intermediatePosition = new LatLng(lat, lng); 

trackingMarker.setPosition(intermediatePosition); 我們還將使用新的標記位置更新我們的多段線,從而在多段線上創建拖尾效果。

private void updatePolyLine(LatLng latLng) { 
    List<LatLng> points = polyLine.getPoints(); 
    points.add(latLng); 
    polyLine.setPoints(points); 
}