0

私はこれが以前にたくさん尋ねられたことを知っていますが、何が正しいか、何がそうでないかについて、私はさまざまな意見を得ています.

開始と停止の 2 つのボタンを使用して、アプリ内の 2 点間の距離を計算しようとしています。

これは簡単だと思いました。開始が押されたときに位置の緯度と経度を取得し、停止が押されたときにそれらを再度取得し、2 つを計算して出来上がりです。しかし、それは計画するつもりはありません。

フィードバックのためにクリックされたときに停止ボタンにダイアログを設定しましたが、現在、(おそらく) 単一の緯度または経度のみを返しています。

以下の私のコード::

 public class MapRun extends FragmentActivity implements LocationListener,
            LocationSource {
        private OnLocationChangedListener mListener;
        private LocationManager locationManager;

        private GoogleMap mMap;
        double lat, lng;
        static double startLat;
        double startLong;
        double stopLat;
        double stopLong;
        public static Location l;

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

            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
            // Creating a criteria object to retrieve provider
            Criteria criteria = new Criteria();

            // Getting the name of the best provider
            String provider = locationManager.getBestProvider(criteria, true);

            // Getting Current Location
            Location location = locationManager.getLastKnownLocation(provider);

            l = location;
            lat = l.getLatitude();
            lng = l.getLongitude();

            onLocationChanged(location);
            locationManager.requestLocationUpdates(provider, 20000, 0, this);

            setUpMapIfNeeded();
        }

        @Override
        public void onPause() {
            if (locationManager != null) {
                locationManager.removeUpdates(this);
            }

            super.onPause();
        }

        @Override
        public void onResume() {
            super.onResume();

            setUpMapIfNeeded();

            if (locationManager != null) {
                mMap.setMyLocationEnabled(true);
            }
        }

        /**
         * Sets up the map if it is possible to do so (i.e., the Google Play
         * services APK is correctly installed) and the map has not already been
         * instantiated.. This will ensure that we only ever call
         * {@link #setUpMap()} once when {@link #mMap} is not null.
         * <p>
         * If it isn't installed {@link SupportMapFragment} (and
         * {@link com.google.android.gms.maps.MapView MapView}) will show a prompt
         * for the user to install/update the Google Play services APK on their
         * device.
         * <p>
         * A user can return to this Activity after following the prompt and
         * correctly installing/updating/enabling the Google Play services. Since
         * the Activity may not have been completely destroyed during this process
         * (it is likely that it would only be stopped or paused),
         * {@link #onCreate(Bundle)} may not be called again so we should call this
         * method in {@link #onResume()} to guarantee that it will be called.
         */
        private void setUpMapIfNeeded() {
            // Do a null check to confirm that we have not already instantiated the
            // map.
            if (mMap == null) {
                // Try to obtain the map from the SupportMapFragment.
                mMap = ((SupportMapFragment) getSupportFragmentManager()
                        .findFragmentById(R.id.map)).getMap();
                // Check if we were successful in obtaining the map.

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

                // This is how you register the LocationSource
                mMap.setLocationSource(this);
            }
        }

        /**
         * This is where we can add markers or lines, add listeners or move the
         * camera. In this case, we just add a marker near Africa.
         * <p>
         * This should only be called once and when we are sure that {@link #mMap}
         * is not null.
         */
        private void setUpMap() {
            mMap.setMyLocationEnabled(true);
        }

        @Override
        public void activate(OnLocationChangedListener listener) {
            mListener = listener;
        }

        @Override
        public void deactivate() {
            mListener = null;
        }

        @Override
        public void onLocationChanged(Location location) {
            if (mListener != null) {
                mListener.onLocationChanged(location);

                // Move the camera to the user's location once it's available!

                mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                        location.getLatitude(), location.getLongitude()), 16.0f));
            }
        }

        @Override
        public void onProviderDisabled(String provider) {
            // TODO Auto-generated method stub
            Toast.makeText(this, "provider disabled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderEnabled(String provider) {
            // TODO Auto-generated method stub
            Toast.makeText(this, "provider enabled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            // TODO Auto-generated method stub
            Toast.makeText(this, "status changed", Toast.LENGTH_SHORT).show();
        }




        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            super.onCreateOptionsMenu(menu);
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.map_menu, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case R.id.startRun:

                startRun();
            case R.id.stopRun:

                stopRun();

                return true;

            default:
                return super.onOptionsItemSelected(item);
            }

        }

        public void startRun() {
            ;



        }

        public void stopRun() {
            Location startPoint = new Location("Ran From");

            startPoint.setLatitude(lat);
            startPoint.setLongitude(lng);

            Location stopPoint = new Location("Ran To");
            stopPoint.setLatitude(stopLat);
            stopPoint.setLongitude(stopLong);
            float distance = startPoint.distanceTo(stopPoint);
            String distStr = String.valueOf(distance);
            Dialog d = new Dialog(this);
            d.setTitle("distance");
            TextView tv = new TextView(this);
            tv.setText(distStr);
            d.setContentView(tv);
            d.show();

        }

これまでに更新されたボタンは次のとおりです。変数を保存して渡す正しい方法に苦労していると思う原則があります。

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.startRun:
            currentLocation = lat + lng;
            startLocation = currentLocation;

        case R.id.stopRun:
            currentLocation = lat + lng;
            stopLocatio

n = 現在の場所;

        float distance = startLocation.distanceTo(stopLocation);
        String distStr = String.valueOf(distance);
        Dialog d = new Dialog(this);
        d.setTitle("distance");
        TextView tv = new TextView(this);
        tv.setText(distStr);
        d.setContentView(tv);
        d.show();

        return true;

    default:
        return super.onOptionsItemSelected(item);
    }

そして、私の OnLocationchanged:

@Override
    public void onLocationChanged(Location location) {
        l = location;
        lat = l.getLatitude();
        lng = l.getLongitude();
        if (mListener != null) {
            mListener.onLocationChanged(location);

            // Move the camera to the user's location once it's available!

            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                    location.getLatitude(), location.getLongitude()), 16.0f));
        }
    }
4

3 に答える 3

3

Location持っていdistanceToます。ユーザーが開始を押したときに、Gps から取得した場所を登録します。ユーザーが停止を押したら、現在の場所を取得して次を実行します。

currentLocation.distanceTo(firstLocation) ;

distanceToこの場所と指定された場所の間のおおよその距離をメートル単位で表す float を返します

public class MapRun extends FragmentActivity implements LocationListener,
        LocationSource {

   Location startLocation;
   Location endLocation;

   Location currentLocation;


    @Override
    public void onLocationChanged(Location location) {
        if (mListener != null) {
            mListener.onLocationChanged(location);

            // Move the camera to the user's location once it's available!

            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(
                    location.getLatitude(), location.getLongitude()), 16.0f));
        }
       currentLocation = location;
    }

 public void startRun() {
        startLocation = currentLocation;
    }

    public void stopRun() {
       stopLocation = currentLocation;
    }

 public float getDistance() {
   float distance = 0f;
   if (stopLocation != null && startLocation != null)
       distance = stopLocation.distanceTo(startLocation);
   return distance;
 }

}
于 2013-05-10T12:28:30.333 に答える
0

startRun() で何もしません。何が起こると思いますか?

LatLng/ LocationcurrentLocation を最新の状態に保つことをお勧めしますonLocationChanged()startRun()あなたの中で

startLocation = currentLocation

stopRun で行う

stopLocation = currentLocation

本当にこれだけです。現在、緯度と経度を最後に確認された場所に設定していますが、これはあなたが何をしたいか疑わしいものであり、stopLat と stopLon afai が確認できる設定を決して設定していません。

于 2013-05-10T12:30:57.407 に答える
0

これを試して:

private double distance(double lat1, double lon1, double lat2, double lon2) {
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2))
            + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2))
            * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    return (dist);
}

private double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}

private double rad2deg(double rad) {
    return (rad * 180.0 / Math.PI);
}

distance()メソッドで Lat long を渡すだけです。

于 2013-05-10T13:37:02.800 に答える