Android:位置服务每次都会返回不同的数字 - 为什么?

时间:2018-02-10 09:35:56

标签: android android-location android-gps

您好我正在开发应用程序,我需要获取精确(准确,程度)的经度和纬度。我使用的方法是LocationListener,但问题是,每次我得到相同位置的不同数字 - 比如几乎是800米的差异。有没有办法我可以每次获得确切的数字?下面是代码。请帮帮我

public Location getlocation(){

    if(ContextCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED){
        Toast.makeText(context,"Permission Not Granted",Toast.LENGTH_LONG).show();
        return null;
    }

    LocationManager lm = (LocationManager) context.getSystemService(context.LOCATION_SERVICE);
    boolean isGPSenabled=lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
    if (isGPSenabled){
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,1,this);
        Location l = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        return l;
    }
    else {
        Toast.makeText(context,"Please Enable GPS",Toast.LENGTH_LONG).show();
    }
    return null;

}

1 个答案:

答案 0 :(得分:0)

使用融合API

googleApiClient = new GoogleApiClient.Builder(FusedLocation1.this)
                        .addApi(LocationServices.API)
                        .addConnectionCallbacks(FusedLocation1.this)
                        .addOnConnectionFailedListener(FusedLocation1.this)
                        .build();

                locationRequest = LocationRequest.create();
                locationRequest.setInterval(2000);

                locationRequest.setFastestInterval(2000);
                locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


                googleApiClient.connect();