2017-04-05 11 views
4

以下のコードから私は地図上にマーカーを追加しています.15秒ごとにデータベースから新しい緯度と経度を取得しています。マーカー(バス画像)が地図上に追加され、道路が車に乗っているようにある位置から別の位置にスムーズに移動しています。今私が望むことは、バスマーカーを方向に沿って回転させたいということです。どうすればこれを達成できますか?私はtoRotationとstの価値は何か分かっていないのですか?方向に応じてマーカー(バス)を回転させる方法は?

public Runnable runLocation = new Runnable() { 
     @Override 
     public void run() { 
      gps = new GPSTracker(MapActivity.this); 
      MyLocation1.clear(); 
      if (gps.CanGetLocation()) { 
       double latitude = gps.getLatitude(); 
       double longitude = gps.getLongitude(); 
       LatLng mylocation = new LatLng(latitude, longitude); 
       if (marker != null) { 
        marker.remove(); 
       } 
       if (circle != null) { 
        circle.remove(); 
       } 
       if (busMarker != null){ 
        lastBusPos = busMarker.getPosition(); 
       } 

       marker = mMap.addMarker(new MarkerOptions() 
         .icon(BitmapDescriptorFactory.fromResource(R.drawable.location)) 
         .title("My Location") 
         .position(mylocation)); 

       circle = mMap.addCircle(new CircleOptions() 
         .center(mylocation) 
         .radius(1000) 
         .strokeColor(0x10000000) 
         .fillColor(0x10000000)); 
      } else { 
       // gps.showSettingsAlert(); 
      } 

      String tag_json_obj = "json_obj_req"; 
      String url = AppConfig.RouteData + "i=1&" + "y=1"; 

      JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        try { 

         JSONObject jObj = new JSONObject(String.valueOf(response)); 
         JSONArray json_user = jObj.getJSONArray("Message"); 
         for (int i = 0; i < json_user.length(); i++) { 

          try { 

           JSONObject obj = json_user.getJSONObject(i); 

           final Double currLat = obj.getDouble("actual_lat"); 
           final Double currLong = obj.getDouble("actual_long"); 
           final LatLng hcmus = new LatLng(currLat, currLong); 


           List<LatLng> latList = new ArrayList<LatLng>(); 
           latList.add(hcmus); 

           if (mMarkers.size() != json_user.length()) { 
            busMarker = mMap.addMarker(new MarkerOptions() 
              .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus)) 
              .title("Bus No" + obj.getString("bus_id")) 
              .position(hcmus)); 
            mMarkers.add(busMarker); 
           } else { 
            //busMarker.setPosition(hcmus); 
            animateMarker(hcmus, false); 
            rotateMarker(busMarker, 45.0f, 45.0f); 
           } 

          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 

       @Override 
       public void onErrorResponse(VolleyError error) { 
        Toast.makeText(MapActivity.this, "Sorry something went wrong..Try again", Toast.LENGTH_SHORT).show(); 
       } 
      }); 

      // Adding request to request queue 
      AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj); 

      MapActivity.this.handler.postDelayed(MapActivity.this.runLocation, 15000); 
     } 

    }; 

    /*-------------------------------Animation----------------------------*/ 
    public void rotateMarker(final Marker marker, final float toRotation, final float st) { 
     final Handler handler = new Handler(); 
     final long start = SystemClock.uptimeMillis(); 
     final float startRotation = st; 
     final long duration = 1555; 

     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); 

       float rot = t * toRotation + (1 - t) * startRotation; 

       marker.setRotation(-rot > 180 ? rot/2 : rot); 
       if (t < 1.0) { 
        // Post again 16ms later. 
        handler.postDelayed(this, 16); 
       } 
      } 
     }); 
    } 

    public void animateMarker(final LatLng toPosition,final boolean hideMarke) { 
     final Handler handler = new Handler(); 
     final long start = SystemClock.uptimeMillis(); 
     Projection proj = mMap.getProjection(); 
     Point startPoint = proj.toScreenLocation(lastBusPos); 
     final LatLng startLatLng = proj.fromScreenLocation(startPoint); 
     final long duration = 5000; 

     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; 
       busMarker.setPosition(new LatLng(lat, lng)); 

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

    /*--------------------------------------------------------------------*/ 

答えて

5

このrelated threadを参照してください。 Locationオブジェクトのbearingを使用し、CameraPositionに設定します。

ユーザーの位置を特定するGPSを使用する場合は、あなたがonLocationChangedで取得 Locationオブジェクトがbearingが含まれています。

あなたは2点のだけの座標(あなたが唯一持っているなど、ネットワークロケーションプロバイダから を座標)がある場合は、次の2つの座標のベアリング計算 にLocation.bearingTo()を使用することができます:あなたは、ベアリングを持っている場合は

Location prevLoc = ... ; 
Location newLoc = ... ; 
float bearing = prevLoc.bearingTo(newLoc) ; 

を、あなたは MarkerOptions.rotation()を使用して、マーカーの回転を設定することができます。

mMap.addMarker(new MarkerOptions() 
        .position(markerLatLng) 
        .icon(BitmapDescriptorFactory.fromResource(R.drawable.map_marker)) 
        .anchor(0.5f, 0.5f) 
        .rotation(bearing) 
        .flat(true)); 

あなたがしたいポイントにanchorを設定する必要があります を周りに回して、マーカーに を設定した位置にしたい点にもなります。 (0.5,0.5)は画像の中心です。

:ここ

も役立つかもしれないいくつかの投稿です

関連する問題