如何使用此代码获取Android中的GPS距离?

时间:2017-07-26 07:43:40

标签: android gps location distance

我是Android新手,我遇到了问题。

基本上我想要的是当我移动时,我的应用程序获得GPS信息并显示在我的手机上,包括我以米为单位的距离。但我不知道如何在Android中这样做。

这是我的代码。

public class MainActivity extends AppCompatActivity {

    TextView tv;
    TextView tv2;
    ToggleButton tb;
    Button currentGPS;



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

        Location location11 = null;
        double Longitude1 = 0;//location11.getLongitude();
        double Latitude1 = 0;//location11.getLatitude();

        tv = (TextView) findViewById(R.id.textView2);
        tv.setText("Ready");

        tb = (ToggleButton)findViewById(R.id.toggle1);




        final LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        tb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try{
                    if(tb.isChecked()){
                        tv.setText("connecting..");

                        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 
                                100, 
                                1, 
                                mLocationListener);
                        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 
                                100, 
                                1, 
                                mLocationListener);

                    }else{
                        tv.setText("Information not connecting");
                        lm.removeUpdates(mLocationListener);  
                    }
                }catch(SecurityException ex){
                }
            }
        });
    } 


private final LocationListener mLocationListener = new LocationListener() {


        public void onLocationChanged(Location location1) {

            Log.d("test", "onLocationChanged, location:" + location1);
            double longitude = location1.getLongitude(); 
            double latitude = location1.getLatitude();   
            double altitude = location1.getAltitude();   
            float accuracy = location1.getAccuracy();    
            String provider = location1.getProvider();   


            final double latitudeA = location1.getLatitude();
            final double longitudeA = location1.getLongitude();

            double distance;


            Location locationA = new Location("point A");
            locationA.setLatitude(latitudeA);
            locationA.setLongitude(longitudeA);

            Location locationB = new Location("point B");
            locationB.setLatitude(latitude);
            locationB.setLongitude(longitude);


            distance = locationA.distanceTo(locationB);



            float currentSpeed = (location1.getSpeed()*3600/1000);
            String convertedSpeed = String.format("%.2f",currentSpeed);




            tv.setText("Provider : " + provider + "\n\nlatitude : " + longitude + "\n\nlangitude : " + latitude
                    + "\n\naltitude : " + altitude + "\n\naccuracy : "  + accuracy +"/\n\nMoving distance :  " + distance + "/m"
                    + "\n\nCurrent speed : " + convertedSpeed + "Km/h");
        }
        public void onProviderDisabled(String provider) {

            Log.d("test", "onProviderDisabled, provider:" + provider);
        }

        public void onProviderEnabled(String provider) {

            Log.d("test", "onProviderEnabled, provider:" + provider);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {

            Log.d("test", "onStatusChanged, provider:" + provider + ", status:" + status + " ,Bundle:" + extras);
        }
    };
}

0 个答案:

没有答案