2016-07-20 1 views
2

여러 마커가있는 Google지도를 만듭니다. 이제 해당 마커 사이에서 자동차 아이콘을 이동하고 싶습니다.이 Move markers in google map v2 androidGoogle지도의 위치 목록이있는 경우 한 위치에서 다른 위치로 마커를 이동하는 방법 android

나는 첫 번째 지점에서 두 번째 지점 사이에서 자동차 아이콘을 움직일 수있다. 그러나 두 번째에서 세 번째로 움직이지 않는다. 내가 이것을 위해 루프에 사용했을 때 그것은 최종 지점으로 간절히 간다. 나는 지연도 추가했지만 다른 것은 나를 위해 일했다. .

미리 감사드립니다.

내 코드 ::이다

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

당신은 자세한 내용 here을 내 대답을 통해 이동 방향 추적을

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에 마커 이동하려면이 방법을 시도 갈 수있다

루프의 몇 밀리 초 동안 잠자기를 넣으십시오.

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

난 내가 inc를 할 때 내 문제는,이 천천히 움직이지됩니다 point.But는 목적지로 이동합니다 내가 값을 증가 올바른 positions.If를 얻을 난 내가 처음부터 응용 프로그램을 열 value.When 수 있습니다 시간 자체가 끝났어. 나는 그것을 볼 수 없다. – 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++; 
      } 
     }); 


    } 
관련 문제