谷歌地图v2上的android不准确的gps

时间:2014-02-05 13:03:54

标签: android gps distance google-maps-api-2

1)我的Android应用程序需要在用户按下开始时计算累积距离。当我跑,它等待一段时间然后开始计算。我认为可能没问题。但是当我不动时,仪表也会自行累积。 当我在外面移动时进行测试时,蓝点不准确并且不停跳跃。这将使距离不准确。现在我重新测试我认为红色标记是我,因为它显示“你在这里”有一次我测试它。到处都是。我如何提高gps的准确性。 从看起来来看,仪表的不准确性是由于gps跳跃而不是计算。我想跟我说一个点,并没有很多标记出现在我曾经去过的地方..

2)另外,我尝试在模拟器上显示这个但是失败了(googles maps v2)。我确实安装了一些带有newely创建的模拟器的apk但也失败了。它是否合法,即使它有效?我还需要在与我的团队相同的模拟器上显示。作为最后的手段,我可​​以在手机上显示。

3)使谷歌地图v2在其他计算机上工作我需要做什么。我的研究表明我需要签署API密钥,但到目前为止,我发现谷歌地图v1版本。

我的java文件

public class MainActivity extends FragmentActivity implements LocationListener{

protected LocationManager locationManager;
private GoogleMap googleMap;
Button btnStartMove,btnPause,btnResume,btnStop;
static double n=0;
Long s1,r1;
double dis=0.0;
Thread t1;
EditText userNumberInput;
boolean bool=false;
int count=0;

    double lat1,lon1,lat2,lon2,lat3,lon3,lat4,lon4;
double dist = 0;
    TextView distanceText;
    float[] result;
    private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 0; // in Meters
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 0;
boolean startDistance = false;
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, this);
    if(isGooglePlay())
    {
        setUpMapIfNeeded();
    }
    distanceText=(TextView)findViewById(R.id.Distance);
    btnStartMove=(Button)findViewById(R.id.Start);//start moving
    btnStartMove.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) {
            startDistance = true;
        }
    });
    //prepare distance...........
    Log.d("GPS Enabled", "GPS Enabled");  
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, true);
    Location location=locationManager.getLastKnownLocation(provider);

    if(location!= null)
    {
        //Display current location in Toast
        String message = String.format(
                "Current Location \n Longitude: %1$s \n Latitude: %2$s",
                location.getLongitude(), location.getLatitude()
        );
        Toast.makeText(MainActivity.this, message,
                Toast.LENGTH_LONG).show();

        //Display current location in textview  
        //latitude.setText("Current Latitude: " + String.valueOf(location.getLatitude())); 
        //longitude.setText("Current Longitude: " + String.valueOf(location.getLongitude()));
        lat3 = location.getLatitude();
        lon3 = location.getLongitude();
    }
    else if(location == null)
    {
        Toast.makeText(MainActivity.this,
                "Location is null",    
                Toast.LENGTH_LONG).show();
    }


}

private void setUpMapIfNeeded() {

    if(googleMap == null)
    {
        Toast.makeText(MainActivity.this, "Getting map",
                Toast.LENGTH_LONG).show();
        googleMap =((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.displayMap)).getMap();

        if(googleMap != null)
        {
            setUpMap();
        }
    }

}

private void setUpMap() 
{
    //Enable MyLocation Layer of Google Map
    googleMap.setMyLocationEnabled(true);

    //Get locationManager object from System Service LOCATION_SERVICE
    //LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    //Create a criteria object to retrieve provider
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    //Get the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);
    if(provider == null)
    {
        onProviderDisabled(provider);
    }
    //set map type
    googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    //Get current location
    Location myLocation = locationManager.getLastKnownLocation(provider);
    if(myLocation != null)
    {
        onLocationChanged(myLocation);
    }       
    locationManager.requestLocationUpdates(provider, 0, 0, this);
}

private boolean isGooglePlay() 
{
    int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);

    if (status == ConnectionResult.SUCCESS)
    {

        Toast.makeText(MainActivity.this, "Google Play Services is available",
                Toast.LENGTH_LONG).show();
        return(true);
    }
    else
    {
            GooglePlayServicesUtil.getErrorDialog(status, this, 10).show();

    }
    return (false);

 }

@Override
public void onLocationChanged(Location myLocation) {
    //show location on map.................
    //Get latitude of the current location
    double latitude = myLocation.getLatitude();
    //Get longitude of the current location
    double longitude = myLocation.getLongitude();
    //Create a LatLng object for the current location
    LatLng latLng = new LatLng(latitude, longitude);
    //Show the current location in Google Map
    googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    //Zoom in the Google Map
    googleMap.animateCamera(CameraUpdateFactory.zoomTo(20));
    googleMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!"));

    //show distance............................
    if(startDistance == true)
    {
        Toast.makeText(MainActivity.this,
                  "Location has changed",    
                  Toast.LENGTH_LONG).show();
            if(myLocation != null)
            {
                //latitude.setText("Current Latitude: " + String.valueOf(loc2.getLatitude())); 
                //longitude.setText("Current Longitude: " + String.valueOf(loc2.getLongitude()));
                float[] results = new float[1]; 
                Location.distanceBetween(lat3, lon3, myLocation.getLatitude(), myLocation.getLongitude(), results);
                System.out.println("Distance is: " + results[0]);               

                dist += results[0];            
                DecimalFormat df = new DecimalFormat("#.##"); // adjust this as appropriate
            if(count==1)
            {
                distanceText.setText(df.format(dist) + "meters");
            }
                lat3=myLocation.getLatitude();
                lon3=myLocation.getLongitude();
                count=1;
          }
    }

}

@Override
public void onProviderDisabled(String provider) {
    Toast.makeText(MainActivity.this,
            "Provider disabled by the user. GPS turned off",
            Toast.LENGTH_LONG).show();
}

@Override
public void onProviderEnabled(String provider) {
    Toast.makeText(MainActivity.this,
            "Provider enabled by the user. GPS turned on",
            Toast.LENGTH_LONG).show();
}

@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    Toast.makeText(MainActivity.this, "Provider status changed",
            Toast.LENGTH_LONG).show();
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
protected void onResume() {
    super.onResume();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, this);
}}

第一个截图,一秒钟后秒,第二个1小时左右。

first shot

second shot

third shot

在第三次拍摄时,屏幕总是返回到有许多红色标记而不是蓝点的地方。吐司“位置已经改变”继续留在屏幕上

主要问题是通过确保准确的GPS来确保准确的距离。我已经困扰了很长一段时间,我甚至从我的应用程序中拿走了地图,因为我无法解决它,但现在我需要它。

1 个答案:

答案 0 :(得分:1)

要删除跳转位置,请忽略所有具有location.getSpeed()< speedThreshold。

根据您的应用需求选择速度阈值,但超过5公里/小时。

(如果您站立或缓慢移动,GPS会暂停)

其他信息:请参阅我的答案 - gps-location-reading-of-same-physical-location-on-windows-phone-geocoo