2017-08-04 47 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); 
} 

编辑:你做的任何步骤之前,您必须有权在清单

<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

是的..我已经添加了我的许可.. –

相关问题