2016-07-20 6 views
2

複数のマーカーでGoogleマップを作成しています。このマーカーを使用して車のアイコンを移動したいのですが。Move markers in google map v2 androidGoogleマップの場所のリストを持っている場合、マーカーをある場所から別の場所に移動する方法

私は車のアイコンを最初のポイントから2番目のポイントまで動かすことができます。しかし、それは2番目から3番目に移動していません。私はこのためにループのために使っていました。それは最終的にポイントになります。遅れも加えました。 。

ありがとうございます。

これは私のコードです::

public void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint) { 

     anim_map = myMap; 


     anim_marker = myMap.addMarker(new MarkerOptions() 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_icon)) 
       .position(directionPoint.get(0)) 
       .flat(true)); 


     myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10)); 

     if (directionPoint.size() >= 2) { 

      for (int i = 0; i < directionPoint.size() - 1; i++) { 
       h.post(new Runnable() { 
        @Override 
        public void run() { 
         animateMarker(anim_map, anim_marker, directionPoint, false); 



        } 
       }); 


      } 

     } 


    } 

private void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint, 
           final boolean hideMarker) { 

     final long start = SystemClock.uptimeMillis(); 
     final long duration = 350000; 
     final Interpolator interpolator = new LinearInterpolator(); 


     h.post(new Runnable() { 
      int i = 0; 

      @Override 
      public void run() { 
       long elapsed = SystemClock.uptimeMillis() - start; 
       float t = interpolator.getInterpolation((float) elapsed 
         /duration); 

       // Log.e("T Location", t + ""); 


       double lng = t * directionPoint.get(i + 1).longitude + (1 - t) * directionPoint.get(i).longitude; 
       double lat = t * directionPoint.get(i + 1).latitude + (1 - t) * directionPoint.get(i).latitude; 
       marker.setPosition(new LatLng(lat, lng)); 

       if (t < 1.0) { 
        h.postDelayed(this, 1); 
       } else { 
        if (hideMarker) { 
         marker.setVisible(false); 
        } else { 
         marker.setVisible(true); 
        } 
       } 


      } 
     }); 


    } 

答えて

0

あなたはより多くの詳細については、私の答えを通じて

private Marker mCurrentMarker; 
    private float ZOOMLEVEL=18.0f; 
    private LatLng previousLatLon; 
    private Handler mLocalHandler; 
    private GoogleMap mGoogleMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     mLocalHandler = new Handler(); 
     previousLatLon=new LatLng(45.320372, 2.460161); 
     //Initialize Marker once Google Map object is created 
     mMarkerOptions = new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.custom_marker_icon)); 
     mMarkerOptions.position(previousLatLon); 
     mCurrentMarker = mGoogleMap.addMarker(mMarkerOptions); 


    } 

    /** 
    * Call this method to move marker in map to new location in map with updated location 
    * @param marker 
    * @param toPosition 
    * @param fromPosition 
    */ 
    public void animateMarker(final Marker marker, final LatLng toPosition,final LatLng fromPosition) { 


     final long duration = 500; 
     final Interpolator interpolator = new LinearInterpolator(); 

     mLocalHandler.post(new Runnable() { 
      @Override 
      public void run() { 
       long elapsed = SystemClock.uptimeMillis() - mStartTime; 
       float t = interpolator.getInterpolation((float) elapsed 
         /duration); 
       marker.setPosition(toPosition); 
       marker.setAnchor(Constants.MAPANCHOR, Constants.MAPANCHOR); 
       mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(toPosition, ZOOMLEVEL)); 
       if (t < 1.0) { 
        // Post again 16ms later. 
        mLocalHandler.postDelayed(this, 16); 
       } else { 
        marker.setVisible(true); 
       } 
       } 
      } 
     }); 
     previousLatLon=toPosition;// reassign the previous location to current location 
    } 

移動を追跡方向を駆動生きるに似GoogleMapにマーカーの動きを作るためにこの方法を試して行くことができますhere

あなたのケースでは、forループの後に数ミリ秒間スリープします

for (int i = 0; i < directionPoint.size() - 1; i++) { 
try { 
Thread.sleep(5000); 
} catch (InterruptedException e) 
{ 
e.printStackTrace(); 
} 
// rest of your code 
+0

私はi値をインクリメントすると目的地点に移動しますが、私の問題はこれがゆっくりと動きません。私が最初のループそのものが終了しました。私はそれを見ることはできません。 – Punithapriya

+0

私が理解する限り、値をハードコーディングしている場合は、ループで遅れをとる必要があります。例:10秒。その後、あなたは見ることができます。 – Stallion

+0

はい私も遅延を追加しましたが、動作しません。 – Punithapriya

0

最後に、私はこの答えを思いついた。 arraylistのサイズに関してすべての値を更新する必要があります。

public void setAnimation(GoogleMap myMap, final List<LatLng> directionPoint) { 

     anim_map = myMap; 


     anim_marker = myMap.addMarker(new MarkerOptions() 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_icon)) 
       .position(directionPoint.get(0)) 
       .flat(true)); 


     myMap.animateCamera(CameraUpdateFactory.newLatLngZoom(directionPoint.get(0), 10)); 


     animateMarker(anim_map, anim_marker, directionPoint, false); 


    } 

    private void animateMarker(GoogleMap myMap, final Marker marker, final List<LatLng> directionPoint, 
           final boolean hideMarker) { 

     final long start = SystemClock.uptimeMillis(); 
     long duration = 3500; 
     final Interpolator interpolator = new LinearInterpolator(); 

     float t = 0; 

     for (int j = 0; j < directionPoint.size() - 1; j++) { 
      Log.e("Location Value", j + " " + directionPoint.get(j).latitude + ", " + directionPoint.get(j).longitude + " : " + directionPoint.get(j + 1).latitude + ", " + directionPoint.get(j + 1).longitude); 
      while (t < j + 1) { 
       t = t - j; 
       double lng = t * directionPoint.get(j + 1).longitude + (1 - t) * directionPoint.get(j).longitude; 
       double lat = t * directionPoint.get(j + 1).latitude + (1 - t) * directionPoint.get(j).latitude; 
       LatList.add(new LatLng(lat, lng)); 
       long elapsed = SystemClock.uptimeMillis() - start; 
       t = interpolator.getInterpolation((float) elapsed/duration); 
      } 
     } 
     Log.e("LatList value", LatList.size() + ""); 
     h.post(new Runnable() { 
      int i = 0; 

      @Override 
      public void run() { 
       marker.setPosition(LatList.get(i)); 
       if ((LatList.size() - 1) > i) 
        h.postDelayed(this, 1); 
       Log.e("I value", i + ""); 
       i++; 
      } 
     }); 


    } 
関連する問題