2017-08-04 11 views
0

私は私のロケーションマネージャーの精度に問題があります。私は高い正確な精度を得るために助けが必要です..時には正しい座標を与えているが時には間違っている...私が最初に試したときに、それは正しい座標を与えた後、再びそれを再試行した、それは間違った座標を与えた..それは他の都市から与えられた座標のように...これを修正する方法はありますか?ここで私の位置マネージャーの正確さに問題があります

は私のコード..です

package com.example.christian.mylocation; 

import android.Manifest; 
import android.content.Context; 
import android.content.pm.PackageManager; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.support.v4.app.ActivityCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends AppCompatActivity { 

    EditText etLatitude, etLongitude; 
    Button btnGetloc; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     etLatitude = (EditText) findViewById(R.id.etLatitude); 
     etLongitude = (EditText) findViewById(R.id.etLongitude); 
     btnGetloc = (Button) findViewById(R.id.btnGetloc); 

     btnGetloc.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       LocationManager locationManager = (LocationManager) MainActivity.this.getSystemService(Context.LOCATION_SERVICE); 
       LocationListener locationListener = new LocationListener() { 
        @Override 
        public void onLocationChanged(Location location) { 
         String lat = Double.toString(location.getLatitude()); 
         String lon = Double.toString(location.getLongitude()); 

         etLatitude.setText(lat); 
         etLongitude.setText(lon); 
        } 

        @Override 
        public void onStatusChanged(String provider, int status, Bundle extras) { 

        } 

        @Override 
        public void onProviderEnabled(String provider) { 

        } 

        @Override 
        public void onProviderDisabled(String provider) { 

        } 
       }; 
       if ((ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)) 
        if ((ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_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; 
        } 
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener); 
      } 
     }); 

    } 
} 

答えて

2

あなたは、最新の位置の修正が最も正確であることを期待するかもしれません。ただし、ロケーションフィックスの精度が異なるため、最新のフィックスが必ずしも最良ではありません。あなたは、あなたが検索された場所は、以前の推定値より大幅に新しい場合、これらの手順は、より高い精度のために

  • チェックを実行する必要があり、いくつかの基準に基づいて

    を位置修正を選択するためのロジックを含める必要があります。

  • 場所が主張している正確さが、前回見積もりの​​ より良いか悪いかを確認してください。
  • 新しいロケーションの提供元を確認して、 がそれ以上信頼できるかどうかを判断します。

例はこちらです。

private static final int TWO_MINUTES = 1000 * 60 * 2; 

/** Determines whether one Location reading is better than the current Location fix 
    * @param location The new Location that you want to evaluate 
    * @param currentBestLocation The current Location fix, to which you want to compare the new one 
    */ 
protected boolean isBetterLocation(Location location, Location currentBestLocation) { 
    if (currentBestLocation == null) { 
     // A new location is always better than no location 
     return true; 
    } 

    // Check whether the new location fix is newer or older 
    long timeDelta = location.getTime() - currentBestLocation.getTime(); 
    boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; 
    boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; 
    boolean isNewer = timeDelta > 0; 

    // If it's been more than two minutes since the current location, use the new location 
    // because the user has likely moved 
    if (isSignificantlyNewer) { 
     return true; 
    // If the new location is more than two minutes older, it must be worse 
    } else if (isSignificantlyOlder) { 
     return false; 
    } 

    // Check whether the new location fix is more or less accurate 
    int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); 
    boolean isLessAccurate = accuracyDelta > 0; 
    boolean isMoreAccurate = accuracyDelta < 0; 
    boolean isSignificantlyLessAccurate = accuracyDelta > 200; 

    // Check if the old and new location are from the same provider 
    boolean isFromSameProvider = isSameProvider(location.getProvider(), 
      currentBestLocation.getProvider()); 

    // Determine location quality using a combination of timeliness and accuracy 
    if (isMoreAccurate) { 
     return true; 
    } else if (isNewer && !isLessAccurate) { 
     return true; 
    } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { 
     return true; 
    } 
    return false; 
} 

/** Checks whether two providers are the same */ 
private boolean isSameProvider(String provider1, String provider2) { 
    if (provider1 == null) { 
     return provider2 == null; 
    } 
    return provider1.equals(provider2); 
} 

EDIT:あなたがどのステップを行う前に、マニフェスト

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.google.android.gms.location.sample.basiclocationsample" > 

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 
</manifest> 

にACCESS_FINE_LOCATIONは、都市レベルに制限されてACCESS_COURSE_LOCATIONよりも正確である権限を持っている必要があります。

+0

私は率直にあなたのコードのいくつかを取得していない...しかし、私はそれらを学ぶでしょう!私はこれが私の以前のものよりも優れていると感じることができます...あなたの努力のために大変ありがとう!私は本当にそれを感謝します。 -Student –

+0

ところで私はちょうど私のコードの下にこれを加えるか、これは新しいですか? –

+0

はい..私は既に私の許可の下にこれを追加しました。 –

関連する問題