如何在android中获得与Location侦听器的准确距离

时间:2015-12-18 06:19:32

标签: android

我是新的Android开发我正在尝试建立一个基于GPS的跟踪系统,我们的客户将根据KMS计算支付给我的客户,但我不会得到准确的距离我尝试使用distanceTo方法和它们之间的距离没有给出准确结果有人可以帮我这个

    06:57:49 D:\jenkins\workspace\AnxNT.Archetype\Web>npm install 
06:58:05 npm WARN install Couldn't install optional dependency: Unsupported
07:00:31 npm ERR! fetch failed https://github.com/rase-/node-XMLHttpRequest/archive/a6b6f2.tar.gz
07:00:31 npm WARN retry will retry, error on last attempt: Error: connect ETIMEDOUT 192.30.252.131:443
07:02:55 npm ERR! fetch failed https://github.com/rase-/node-XMLHttpRequest/archive/a6b6f2.tar.gz
07:02:55 npm WARN retry will retry, error on last attempt: Error: connect ETIMEDOUT 192.30.252.131:443
07:06:08 npm ERR! fetch failed https://github.com/rase-/node-XMLHttpRequest/archive/a6b6f2.tar.gz
07:06:08 npm ERR! Windows_NT 6.3.9600

从这里我得到LatLng并使用onLocationChanged方法我正在使用新的位置并使用距离来计算距离To你能建议我们最好的方法,即使我尝试了

if (isNetworkEnabled) {

                /*if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    public void requestPermissions(@NonNull String[] permissions, int requestCode)
                    // 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 Activity#requestPermissions for more details.
                    return null;
                }*/
                locationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                Criteria criteria = new Criteria();
                String provider = locationManager.getBestProvider(criteria, true);
                criteria.setAccuracy(Criteria.ACCURACY_FINE);
                if (locationManager != null) {
                    location = locationManager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        latitude = location.getLatitude();
                        longitude = location.getLongitude();
                    }
                }
            } 

2 个答案:

答案 0 :(得分:0)

试试这个:

Location  locationA = new Location("point A");
    locationA.setLatitude(currentLati);
    locationA.setLongitude(currentLongi);

Location locationB = new Location("point B");
                    locationB.setLatitude(Double.valueOf(destinationLatitude));
                    locationB.setLongitude(Double.valueOf(destinationLongitude));
                    //float distance = locationA.distanceTo(locationB); //in metres
                    float distance = locationA.distanceTo(locationB)/1000; //in KM

答案 1 :(得分:0)

要获得最佳位置,请使用此功能:

public static LocationManager mLocationManager;

 private Location getLastKnownLocation() {
        mLocationManager = (LocationManager)getActivity().getApplicationContext().getSystemService(getActivity().LOCATION_SERVICE);
        List<String> providers = mLocationManager.getProviders(true);
        Location bestLocation = null;
        try {
            for (String provider : providers) {
                Location l = mLocationManager.getLastKnownLocation(provider);
                if (l == null) {
                    continue;
                }
                if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
                    // Found best last known location: %s", l);
                    bestLocation = l;
                }
            }
        }catch (Exception e){

        }
        return bestLocation;
    }