2017-08-22 10 views
-1

こんにちは私は5秒ごとに位置を取得しようとしています、速度と総距離または旅行を計算しますが、正確な距離と速度を与えていません。 TIA場所の距離と速度を取得する

public class LocationUpdater extends Service implements GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener{ 

private double latitude ; 
private double longitude ; 
Session session; 
Context context; 
LocationRequest mLocationRequest; 

private static final int MILLISECONDS_PER_SECOND = 1000; 

public static final int UPDATE_INTERVAL_IN_SECONDS = 5; 
private static final long UPDATE_INTERVAL = 
     MILLISECONDS_PER_SECOND * UPDATE_INTERVAL_IN_SECONDS; 

private static final int FASTEST_INTERVAL_IN_SECONDS = 10; 
private static final long FASTEST_INTERVAL = 
     MILLISECONDS_PER_SECOND * FASTEST_INTERVAL_IN_SECONDS; 

private static final int DESIRED_ACCURACY = 10; 
private static final float SMALLEST_DISPLACEMENT = 10f; 

/** 
* Provides the entry point to Google Play services. 
*/ 
protected GoogleApiClient mGoogleApiClient; 

public LocationUpdater() { 
} 

@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 
    context = LocationUpdater.this; 
    session = Session.getSession(context); 
    buildGoogleApiClient(); 
    mGoogleApiClient.connect(); 
    return START_STICKY; 
} 

protected synchronized void buildGoogleApiClient() { 
    mGoogleApiClient = new GoogleApiClient.Builder(context) 
      .addConnectionCallbacks(this) 
      .addOnConnectionFailedListener(this) 
      .addApi(LocationServices.API).build(); 
} 

@Override 
public IBinder onBind(Intent intent) { 
    // TODO: Return the communication channel to the service. 
    throw new UnsupportedOperationException("Not yet implemented"); 
} 

@Override 
public void onConnected(Bundle bundle) { 
    try{ 


     mLocationRequest = LocationRequest.create(); 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
     mLocationRequest.setSmallestDisplacement(SMALLEST_DISPLACEMENT); 

     requestLocationUpdate(); 

    }catch (SecurityException ex){ 
     ex.printStackTrace(); 
    } 


} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(ConnectionResult connectionResult) { 
    mGoogleApiClient.connect(); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    if(session.getTrip_status().equalsIgnoreCase(Utils.trip_status_started)) 
     startService(new Intent(LocationUpdater.this,LocationUpdater.class)); 
} 

は、私はすべての滑らかで正確ではない結果が、更新プログラムを取得していないことができています。それが正しい方法であるかどうか私に教えてください。そうでなければ何かを試してみるべきです。以下は

は私の場所のアップデータコード

private void fetchMyLocation(){ 
    try{ 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, new LocationListener() { 
      @Override 
      public void onLocationChanged(Location location) { 

       if (location != null) { 
        latitude = location.getLatitude(); 
        longitude = location.getLongitude(); 

        Log.wtf("ankit","accuracy "+location.getAccuracy()); 

        if (location.hasAccuracy() && location.getAccuracy() <= DESIRED_ACCURACY) { 
         // This is your most accurate location. 
         Log.wtf("ankit","latitude:::"+latitude); 
         Log.wtf("ankit","longitude:::"+longitude); 

         session.setCurrentLatitude(""+latitude); 
         session.setCurrentLongitude(""+longitude); 

         if(session.getTrip_status().equalsIgnoreCase(Utils.trip_status_started)){ 
          updateDistanceAndSpeed(latitude,longitude); 
         } else { 
          UpdateLocationListner.getInstance().changeState(latitude,longitude,""); 
         } 
        } 

       } 


      } 
     }); 
    }catch (SecurityException ex){ 
     ex.printStackTrace(); 
    } 
} 

であり、これは私が速度と距離を計算する方法であるあなただけの緯度と

public void setLatLng(LatLng origins, LatLng destinations) { 
     url = "http://maps.googleapis.com/maps/api/distancematrix/json?" + 
       "origins=" + origins.latitude + "," + origins.longitude + "" + 
       "&destinations=" + destinations.latitude + "," + destinations.longitude + "" + 
       "&mode=driving" + 
       "&language=en-EN" + 
       "&sensor=false"; 
    } 

ハンドル経度を渡す必要が

// update speed and distance 

private void updateDistanceAndSpeed(double latitude,double longitude){ 

    long currentTimeMills = System.currentTimeMillis(); 

     if (session.getLastLatitude().equalsIgnoreCase("") 
       && session.getLastLongitude().equalsIgnoreCase("")) { 
      session.setLastLatitude(""+latitude); 
      session.setLastLongitude(""+longitude); 
     }else { 
      synchronized (this){ 
       try{ 
        String lat = ""+latitude; 
        String lng = ""+longitude; 
        double speed = 0; 

        if(!lat.equalsIgnoreCase(session.getLastLatitude()) 
          && !lng.equalsIgnoreCase(session.getLastLongitude())){ 

         Location locationA = new Location("point A"); 

         locationA.setLatitude(latitude); 
         locationA.setLongitude(longitude); 

         Location locationB = new Location("point B"); 

         locationB.setLatitude(Double.parseDouble(session.getLastLatitude())); 
         locationB.setLongitude(Double.parseDouble(session.getLastLongitude())); 

         if(session.getDistance().equalsIgnoreCase("")) 
          session.setDistance("0"); 

         Log.wtf("ankit","total distance:"+session.getDistance()); 

         float lastDistance = Float.parseFloat(session.getDistance()); 

         float distance[] = new float[1]; 

         Location.distanceBetween(latitude, longitude, 
           Double.parseDouble(session.getLastLatitude()), 
           Double.parseDouble(session.getLastLongitude()), distance); 



         if(!session.getLastTimeStamp().equalsIgnoreCase("")){ 
          long lastTime = Long.parseLong(session.getLastTimeStamp()); 
          long timeDifference = currentTimeMills - lastTime; 

          long diffInSec = TimeUnit.MILLISECONDS.toSeconds(timeDifference); 
          speed = distance[0]/diffInSec; 
         } else { 
          speed = 0; 
         } 

         speed = speed*3.6; 
         speed = Utils.round(speed,2); 

         float roundDistance = Utils.round(distance[0],2); 
         Log.wtf("ankit","roundDistance"+roundDistance); 
         float final_distance = lastDistance+roundDistance; 
         UpdateLocationListner.getInstance().changeState(latitude,longitude,""+speed+"/"+final_distance); 
         session.setDistance(""+final_distance); 


        } else { 
         UpdateLocationListner.getInstance().changeState(latitude,longitude,"0/0"); 
        } 
        // send speed 


       }catch (Exception ex){ 
        ex.printStackTrace(); 
       } 
      } 
      session.setLastLatitude(""+latitude); 
      session.setLastLongitude(""+longitude); 
      session.setLastTimeStamp(""+currentTimeMills); 
     } 



} 
+0

を使用する速度のため、通常は(少なくとも、多くの要因に応じて、30秒程度)5秒以上の方法時間がかかります私は5秒ごとに位置修正を要求しようとしていないと思う。 –

+0

あなたはコード全体を投稿して修正するように頼むべきではありません。それ。問題をローカライズし、デバッグを使用して、結果をログに出力するなど。 –

+0

@ ZamronyP.Juhara ok 30秒に変換してから、質問と回答をテストして編集します。 – shankey

答えて

1

このURLの応答は、ここでは2つの緯度と経度の間の距離と時間を示しています。その速度を計算する必要があり、GPSデバイス上の位置修正を取得するには

の速度=距離/時間

+0

私はインターネットを消費するので、どんなAPIも呼びたくないが、とにかく感謝する – shankey

関連する問題