0

如何在Google地圖Android上以具有特定速度(kmph)的數組爲單位設置數組之間的標記。在具有特定速度的LatLng數組之間移動標記

我試過animateMarker在互聯網上廣泛使用的方法。能夠通過一組LatLng點但不能以特定的速度移動。

有人可以幫助我在與KMPH

回答

1

特定的速度移動對象您可能需要在這裏使用Physics。下面是一個類似算法的細節,可能會有所幫助。

  1. 找出從第一個位置到最後一個應消耗的時間。假設Velocity17 km/hr,並且第一個和最後一個位置之間的距離是10 km那麼時間將是3600 sec3600000 ms
  2. 現在在Array中劃分每個位置的時間。讓我們,如果你有在Array 100元說,每個位置之間的時間將是36 sec36000 ms
  3. 現在定義與持續時間設置一個Handler36 sec36000 ms並嘗試這個時間內動畫標記。爲此,您可以使用另一個Handler或由他提供的默認方法API。回想一下,您每次都有下一個位置的陣列元素。

注意

我發現了一個例子太多。原文請參考here

public void animateMarker(final Marker marker, final LatLng toPosition, 
     final boolean hideMarker) { 
    final Handler handler = new Handler(); 
    final long start = SystemClock.uptimeMillis(); 
    Projection proj = mGoogleMapObject.getProjection(); 
    Point startPoint = proj.toScreenLocation(marker.getPosition()); 
    final LatLng startLatLng = proj.fromScreenLocation(startPoint); 
    final long duration = 500; 

    final Interpolator interpolator = new LinearInterpolator(); 

    handler.post(new Runnable() { 
     @Override 
     public void run() { 
      long elapsed = SystemClock.uptimeMillis() - start; 
      float t = interpolator.getInterpolation((float) elapsed 
        /duration); 
      double lng = t * toPosition.longitude + (1 - t) 
        * startLatLng.longitude; 
      double lat = t * toPosition.latitude + (1 - t) 
        * startLatLng.latitude; 
      marker.setPosition(new LatLng(lat, lng)); 

      if (t < 1.0) { 
       // Post again 16ms later. 
       handler.postDelayed(this, 16); 
      } else { 
       if (hideMarker) { 
        marker.setVisible(false); 
       } else { 
        marker.setVisible(true); 
       } 
      } 
     } 
    }); 
} 

希望它的任何幫助。

0

我修改了相同的方法來實現這一點。

我們需要調用這個方法

animateMarker(googleMap, driver, arrayPoints, distance(arrayPoints.get(0), arrayPoints.get(1)) * msForKMTravel, 0); 

arrayPoints是經緯度陣列。 msForKMTravel是用於運送知識管理的微安全技術。

方法:::

public void animateMarker(final GoogleMap mMap, final Marker marker, final List<LatLng> arrayLications, final double duration, final int step) { 
     Log.d(TAG, "Moving from:" + step + "To:" + (step + 1) + "duration" + duration); 
     final Handler handler = new Handler(); 
     final long start = SystemClock.uptimeMillis(); 
     LatLng startPosition = arrayLications.get(step); 
     final LatLng toPosition = arrayLications.get(step + 1); 
     Projection proj = mMap.getProjection(); 
     Point startPoint = proj.toScreenLocation(startPosition); 
     final LatLng startLatLng = proj.fromScreenLocation(startPoint); 

     final Interpolator interpolator = new LinearInterpolator(); 

     handler.post(new Runnable() { 
      @Override 
      public void run() { 
       long elapsed = SystemClock.uptimeMillis() - start; 
       float t = interpolator.getInterpolation((float) (elapsed 
         /duration)); 
       Log.d(TAG, "interpolator t:" + t + "start :" + start + " elapsed :" + elapsed); 
       double lng = t * toPosition.longitude + (1 - t) 
         * startLatLng.longitude; 
       double lat = t * toPosition.latitude + (1 - t) 
         * startLatLng.latitude; 
       LatLng newLatLng = new LatLng(lat, lng); 
       Log.d(TAG, "Moving to" + newLatLng); 
       marker.setPosition(newLatLng); 

       if (t < 1.0) { 
        handler.postDelayed(this, 100); 
       } else { 
        if (arrayLications.size() > step + 2 && isVisible()) { 
         double distance = distance(arrayLications.get(step + 1), arrayLications.get(step + 2)); 
         double time = distance * msForKMTravel; 
         Log.d(TAG, "Go for distance" + distance + " in time " + time); 
         animateMarker(mMap, marker, arrayLications, time, step + 1); 
        } 
       } 
      } 
     }); 
    } 
+0

因爲我不知道是否有這個問題提供一個更好的解決辦法,我不能記住這作爲一個答案。 – Pavandroid

相關問題