2016-10-31 11 views
0

私はGPSリスナーを適用して、GPS座標、速度、距離をキャプチャする、位置ベースのアンドロイドアプリケーションを作成しています。問題は、私の「onLocationChanged」コールバック関数が2回の代わりに、1。ここonLocationChangedのための私のコードで値を加算させ、すべての間隔で2回呼び出されます:onLocationChangedすべての間隔で2回呼び出される

ここ
`@Override 
    public void onLocationChanged(Location location) { 

    /* currentLatitude = location.getLatitude(); 
     currentLongitude = location.getLongitude(); 

     driverLoc.setDriver_latitude(currentLatitude); 
     driverLoc.setDriver_longitude(currentLongitude); 

     Toast.makeText(this, currentLatitude + " WORKS " + currentLongitude + "" + (location.getSpeed() * 3.6f), Toast.LENGTH_LONG).show(); 
    */ 
     latitude = location.getLatitude(); 
     longitude = location.getLongitude(); 

     driverLoc.setDriver_latitude(latitude); 
     driverLoc.setDriver_longitude(longitude); 

     long elapsedMillis = SystemClock.elapsedRealtime(); 
     //Toast.makeText(JourneyStartActivity.this, "Elapsed milliseconds: " + (elapsedMillis/1000), 
     //  Toast.LENGTH_SHORT).show(); 

     long Milis = previousMiliSecs; 

     previousMiliSecs = elapsedMillis; 

     int hours = (int) (elapsedMillis/3600000); 
     int minutes = (int) (elapsedMillis - hours * 3600000)/60000; 

     long secs = TimeUnit.MILLISECONDS.toSeconds((previousMiliSecs - Milis)); 

     long currentMilis = previousMiliSecs - Milis; 

     if(location.getAccuracy() <=10 && (previousMiliSecs - Milis) >= 10000) { 

      Location temp = mCurrentLocation; 

      mCurrentLocation = location; 

      float[] fArr; 
      fArr = new float[3]; 

      double distance = mCurrentLocation.distanceTo(temp); 

      //location.distanceBetween(temp.getLatitude(), temp.getLongitude(), mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude(), fArr); 

      //double distance = getDistanceFromLatLonInKm(temp.getLatitude(), temp.getLongitude(), mCurrentLocation.getLatitude(), mCurrentLocation.getLongitude()); 
      //double distance = fArr[0]; 

      float journeySpeed = (location.getSpeed() * 3.6f); 
      //float journeySpeed = (float) ((distance/10.0f) * 3.6f); 
      //journeySpeed *= 3.6f; 

      if (GPSTracker.inJourney) { 
       journey.setTotalDistanceCovered(distance); 
       journey.setJourneySpeedIntervals(journeySpeed); 
      } 
/* 
     TextView txt = (TextView) ((Activity) mContext).findViewById(R.id.textView38); 
     txt.setText(String.valueOf(journey.getTotalDistanceCovered())); 
*/ 
      //Log.e("Home GPS Update","Lat: " + latitude + " Long: " + longitude + " Speed: " + journeySpeed + " Distance: " + journey.getTotalDistanceCovered()); 
      Log.e("Home GPS Update", "Lat: " + latitude + " Long: " + longitude + " Speed: " + journeySpeed + " Distance: " + journey.getTotalDistanceCovered() + " Accuracy: " + location.getAccuracy() + " Seconds: " + secs); 
     } 
    }` 

Please help me out here as i am stuck on it since last 3 days... 

が設定されて私のコードですGoogle APIクライアント:

@Override 
public void onConnected(Bundle bundle) { 

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // public void onRequestPermissionsResult(int requestCode, String[] permissions, 
     //           int[] grantResults) 
     // to handle the case where the user grants the permission. See the documentation 
     // for ActivityCompat#requestPermissions for more details. 
     return; 
    } 

    mLocationRequest = LocationRequest.create(); 
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    mLocationRequest.setSmallestDisplacement(0); 
    mLocationRequest.setInterval(1 * 10000);  // 10 seconds, in milliseconds 
    mLocationRequest.setFastestInterval(1 * 10000); // 10 second, in milliseconds 

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

    if (location == null) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } else { 
     //If everything went fine lets get latitude and longitude 
     currentLatitude = location.getLatitude(); 
     currentLongitude = location.getLongitude(); 

     driverLoc.setDriverLocation(location); 

     driverLoc.setDriver_latitude(currentLatitude); 
     driverLoc.setDriver_longitude(currentLongitude); 

     //Toast.makeText(this, "Latitude: "+driverLoc.getDriver_latitude() + " Longitude: "+driverLoc.getDriver_longitude(), Toast.LENGTH_LONG).show(); 
    } 
} 


10-31 15:38:07.854 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.8 Seconds: 14 
10-31 15:38:07.855 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.8 Seconds: 14 
10-31 15:38:17.856 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.8 Seconds: 10 
10-31 15:38:17.858 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.8 Seconds: 10 
10-31 15:38:32.853 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.9 Seconds: 14 
10-31 15:38:32.854 21996-21996/amidtech.android.optimusdrive E/Home GPS Update: Lat: 24.833515 Long: 67.0378417 Speed: 0.0 Distance: 0.0 Accuracy: 4.9 Seconds: 14 
+0

これを2回呼び出すと、このコードの外側にコードが表示されることがあります。あなたが知っている、これを引き起こしている原因は –

+0

です。申し訳ありませんが、私はあなたを得ることができませんでした... 他の位置機能を指していますか? –

+0

呼び出されたコードを表示していますが、呼び出すコードの設定方法は表示されません。このコードは、それ自身を呼び出すものではありません。それはまったく問題には関係しません。 –

答えて

1

同じコールバックを2回登録したかどうかを確認してください。多分問題です。 あなたは二度同じリスナーを追加:

LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

    Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); 

    if (location == null) { 
     LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 
    } else { ... 

場所マネージャにはキャッシュされた値を持っていないし、null最初の時間を返します。この時点で、同じリスナーを再度登録します。最初のヌル値を無視する必要があります

+0

どうすれば確認できますか? –

+0

最初にリスナーを登録し、responseがnullの場合は2回目に登録します。 –

+0

それでは、どちらを削除すればよいですか? –

関連する問題