简体   繁体   中英

GPS Tracking Comparison (Android)

I am developing an indoor map app for my college. I have stored the longitude and latitude for each place in the map in the database. The next step is getting the user's location from the GPS and comparing with the database to be able to give the user the name of their location. My problem is with the comparing step - how could I do it? The GPS tracking works perfectly.

The class for the GPS tracking:

public class GPSTracker {
  public GPSTracker(Context context) {
    this.mContext = context;
    getLocation();
  }

  public Location getLocation() {
    try {
      locationManager = (LocationManager) mContext
        .getSystemService(LOCATION_SERVICE);

      // getting GPS status
      isGPSEnabled = locationManager
        .isProviderEnabled(LocationManager.GPS_PROVIDER);

      // getting network status
      isNetworkEnabled = locationManager
        .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

      if (!isGPSEnabled && !isNetworkEnabled) {
        // no network provider is enabled
      } else {
        this.canGetLocation = true;
        if (isNetworkEnabled) {
          locationManager.requestLocationUpdates(
                                                 LocationManager.NETWORK_PROVIDER,
                                                 MIN_TIME_BW_UPDATES,
                                                 MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
          Log.d("Network", "Network");
          if (locationManager != null) {
            location = locationManager
              .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            if (location != null) {
              latitude = location.getLatitude();
              longitude = location.getLongitude();
            }
          }
        }
        // if GPS Enabled get lat/long using GPS Services
        if (isGPSEnabled) {
          if (location == null) {
            locationManager.requestLocationUpdates(
                                                   LocationManager.GPS_PROVIDER,
                                                   MIN_TIME_BW_UPDATES,
                                                   MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
            Log.d("GPS Enabled", "GPS Enabled");
            if (locationManager != null) {
              location = locationManager
                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
              if (location != null) {
                latitude = location.getLatitude();
                longitude = location.getLongitude();
              }
            }
          }
        }
      }

    } catch (Exception e) {
      e.printStackTrace();
    }

    return location;
  }

  /**
   * Stop using GPS listener
   * Calling this function will stop using GPS in your app
   * */
  public void stopUsingGPS(){
    if(locationManager != null){
      locationManager.removeUpdates(GPSTracker.this);
    }       
  }

  /**
   * Function to get latitude
   * */
  public double getLatitude(){
    if(location != null){
      latitude = location.getLatitude();
    }

    // return latitude
    return latitude;
  }

  /**
   * Function to get longitude
   * */
  public double getLongitude(){
    if(location != null){
      longitude = location.getLongitude();
    }

    // return longitude
    return longitude;
  }

  /**
   * Function to check GPS/wifi enabled
   * @return boolean
   * */
  public boolean canGetLocation() {
    return this.canGetLocation;
  }

  /**
   * Function to show settings alert dialog
   * On pressing Settings button will lauch Settings Options
   * */
  public void showSettingsAlert(){
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

    // Setting Dialog Title
    alertDialog.setTitle("GPS is settings");

    // Setting Dialog Message
    alertDialog.setMessage("GPS is not enabled. Do you want to go to settings menu?");

    // On pressing Settings button
    alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int which) {
          Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
          mContext.startActivity(intent);
        }
      });

    // on pressing cancel button
    alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
          dialog.cancel();
        }
      });

    // Showing Alert Message
    alertDialog.show();
  }

  @Override
  public void onLocationChanged(Location location) {
  }

  @Override
  public void onProviderDisabled(String provider) {
  }

  @Override
  public void onProviderEnabled(String provider) {
  }

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

  @Override
  public IBinder onBind(Intent arg0) {
    return null;
  }

}

The code to display tracking in MainActivity::

// check if GPS enabled
if(gps.canGetLocation()){
  double latitude = gps.getLatitude();
  double longitude = gps.getLongitude();
  // \n is for new line
  Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
} else {
  // can't get location
  // GPS or Network is not enabled
  // Ask user to enable GPS/network in settings
  gps.showSettingsAlert();
}

As you told you have already made collection of database in which all the places of you collage Lat and Long. Now any user who is near to canteen of your collage open your app now app should find the nearest place where user standing right now. I assuming above requirement of your collage.

For the above requirement the best way is Haversine formula. check this wiki link : Link

and make some R&D on it. Now by using this formula technical flow is like below :

1 : Get user's lat and long using GPS.

2 : Send this GSP Co-ordinates to your php web service where you have implemented Haversin formula. By using this formula you will find the nearest place from you current place from your database.

3 : Now you have nearest place from your existing place.

For use Haversin Formula in php web service please refer this links : Link

Hope you get what you want to do. Please do some R&D on it and I am sure that you will solve it.

You compare two Lat Lon coordinates, where at least one is not perfect because measured by eg GPS, by calculating the distance in meters inbetween them.
If the distance is lower than a specific threshold, you can consider them as matching.

Android has a method for that distance calculation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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