简体   繁体   English

Android GPS:连续测量位置之间的距离

[英]Android GPS: measured distance between locations continiously

I want to create an application of distance tracker in android. 我想在Android中创建距离跟踪器的应用程序。 I have a Spinner, button and a TextView . 我有一个Spinner, button和一个TextView
Initially text view will be 0.00km . 最初,文本视图将为0.00km
When I click the button (GPS tracking start) and start walking in the text view it will show the distance continuously. 当我单击按钮(GPS跟踪开始)并开始在文本视图中行走时,它将连续显示距离。 When I click the button again(GPS tracking terminate) and show the full distance between clicking button. 当我再次单击按钮(GPS跟踪终止)并显示单击按钮之间的完整距离时。

Here is the screenshot that the application will look like: 这是该应用程序的屏幕截图:

启动应用

单击开始按钮后

Here is My Code: 这是我的代码:

public class Gps extends Activity   {

 TextView display;


  double currentLon=0 ;
  double currentLat=0 ;
  double lastLon = 0;
  double lastLat = 0;
  double distance;




public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_test);

    display = (TextView) findViewById(R.id.textView1);


  LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE);
                lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);
                Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

                if(loc==null){
                    display.setText("No GPS location found");
                    }
                    else{
                        //set Current latitude and longitude
                        currentLon=loc.getLongitude();
                        currentLat=loc.getLatitude();

                        }
                //Set the last latitude and longitude
                lastLat=currentLat;
                lastLon=currentLon ;


}



 LocationListener Loclist = new LocationListener(){




@Override
public void onLocationChanged(Location location) {
    // TODO Auto-generated method stub

     //start location manager
     LocationManager lm =(LocationManager) getSystemService(LOCATION_SERVICE);

      //Get last location
     Location loc = lm.getLastKnownLocation(lm.GPS_PROVIDER);

    //Request new location
      lm.requestLocationUpdates(lm.GPS_PROVIDER, 0,0, Loclist);

      //Get new location
      Location loc2 = lm.getLastKnownLocation(lm.GPS_PROVIDER);

      //get the current lat and long
     currentLat = loc.getLatitude();
     currentLon = loc.getLongitude();


    Location locationA = new Location("point A");
        locationA.setLatitude(lastLat);
        locationA.setLongitude(lastLon);

    Location locationB = new Location("point B");
        locationB.setLatitude(currentLat);
        locationB.setLongitude(currentLon);

        double distanceMeters = locationA.distanceTo(locationB);

        double distanceKm = distanceMeters / 1000f;

        display.setText(String.format("%.2f Km",distanceKm ));

        }



@Override
public void onProviderDisabled(String provider) {
    // TODO Auto-generated method stub

}



@Override
public void onProviderEnabled(String provider) {
    // TODO Auto-generated method stub

}



@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
    // TODO Auto-generated method stub



}

 };

}

Please help me. 请帮我。 Thanks. 谢谢。

You should register a listener on the GPS when the location changed. 位置更改后,您应该在GPS上注册一个侦听器。 You basically have to store the previous known location and compare it with the new one. 您基本上必须存储先前的已知位置,并将其与新位置进行比较。

The simplest way to get the distance between two points would be to use: 获取两点之间距离的最简单方法是使用:

sqrt( (x2 - x1)^2 + (y2 - y1)^2 + (z2 - z1)^2 )
  • X may be the latitude X可能是纬度
  • Y may be the longitude Y可能是经度
  • Z may be the altitude Z可能是海拔

Here is a pseudo code sample to help you 这是一个伪代码示例可以帮助您

public class GpsCalculator
{

    private LocationManager locationManager = null;
    private Location previousLocation = null;
    private double totalDistance = 0D;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 100; // 100 meters
    private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minutes

    public void run(Context context)
    {
        // Get the location manager
        locationManager = (LocationManager) context.getSystemService(Service.LOCATION_SERVICE);

        // Add new listeners with the given params
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener); // Network location
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, locationListener); // Gps location
    }

    public void stop()
    {
        locationManager.removeUpdates(locationListener);
    }

    private LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location newLocation)
        {
            if (previousLocation != null)
            {
                double latitude = newLocation.getLatitude() + previousLocation.getLatitude();
                latitude *= latitude;
                double longitude = newLocation.getLongitude() + previousLocation.getLongitude();
                longitude *= longitude;
                double altitude = newLocation.getAltitude() + previousLocation.getAltitude();
                altitude *= altitude;
                GpsCalculator.this.totalDistance += Math.sqrt(latitude + longitude + altitude);
            }

            // Update stored location
            GpsCalculator.this.previousLocation = newLocation;
        }

        @Override
        public void onProviderDisabled(String provider) {}

        @Override
        public void onProviderEnabled(String provider) {}

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };
}

And the Activty should look like that: 活动应如下所示:

public class MainActivity extends Activity
{
    private Button mainButton = null;
    private boolean isButtonPressed = false;

    private GpsCalculator gpsCalculator = null;

    public void onCreate(Bundle savedInstance)
    {
        super.onCreate(savedInstance);

        // Create a new GpsCalculator instance
        this.gpsCalculator =  new GpsCalculator();

        // Get your layout + buttons

        this.mainButton = (Button) findViewById(R.id.main_button);

        this.mainButton.addOnClickListener(new OnClickListener() {
            @Override
            public void onClick()
            {
                // Enable or diable gps
                if (MainActivity.this.isButtonPressed) gpsCalculator.run(this);
                else gpsCalculator.stop();

                // Change button state
                MainActivity.this.isButtonPressed = !MainActivity.this.isButtonPressed;
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM