2016-12-04 47 views
0

在我的應用程序中,我使用KSOAP2獲取我的車輛的當前位置並在地圖上顯示它。到目前爲止,我正在清除所有標記(mGoogleMap.clear();),然後爲每個新的位置更新添加一個新標記(mGoogleMap.addMarker(new MarkerOptions().position(buslatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.bus)));Android:動畫標記運動

但是,此方法會使標記突然移動,看起來不太好。因此,我將舊LatLang存儲在一個變量中,並希望標記從舊位置緩慢移動到新位置,然後縮放/調整地圖,以便新位置位於中心位置。使用的代碼:

     activity.runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 

           busMarker = mGoogleMap.addMarker(new MarkerOptions().position(buslatLng).icon(BitmapDescriptorFactory.fromResource(R.drawable.bus))); 

           double[] startValues = LastbuslatLng==null ? new double[]{buslatLng.latitude, buslatLng.longitude} : new double[]{LastbuslatLng.latitude, LastbuslatLng.longitude}; 
           double[] endValues = new double[]{buslatLng.latitude, buslatLng.longitude}; 
           ValueAnimator latLngAnimator = ValueAnimator.ofObject(new DoubleArrayEvaluator(), startValues, endValues); 
           latLngAnimator.setDuration(600); 
           latLngAnimator.setInterpolator(new DecelerateInterpolator()); 
           latLngAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 
            @Override 
            public void onAnimationUpdate(ValueAnimator animation) { 
             double[] animatedValue = (double[]) animation.getAnimatedValue(); 
             busMarker.setPosition(new LatLng(animatedValue[0], animatedValue[1])); 
            } 
           }); 
           latLngAnimator.start(); 
          } 
         }); 
LastbuslatLng = buslatLng; 

我將不得不照顧的情況下,在開始時會有一個單一的位置。但是,我沒有看到標記移動,也沒有縮放。任何人都可以請幫我,我錯在哪裏?

+0

你只是想你的相機動畫到特定的位置? –

+0

我想要的是,當我收到一個新的GPS座標時,標記應該「順利」從舊位置移動到新位置,而不是突然跳躍,之後,攝像機應該將新的位置標記放在地圖的中心。 –

回答

0

爲了動畫相機到特定的位置,我用下面的代碼片段,在下面的例子相機將順利地移動到這個37.4219999,-122.0862462位置

CameraPosition googlePlex = CameraPosition.builder() 
      .target(new LatLng(37.4219999,-122.0862462)) 
      .zoom(16) 
      .bearing(0) 
      .tilt(45) 
      .build(); 

mGooglemap.animateCamera(CameraUpdateFactory.newCameraPosition(googlePlex), 10000, null); 
+1

這使動畫相機不是標記 –