简体   繁体   English

GPS追踪比较(Android)

[英]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. 下一步是从GPS获取用户的位置,并与数据库进行比较,以便为用户提供其位置的名称。 My problem is with the comparing step - how could I do it? 我的问题是比较步骤-我该怎么做? The GPS tracking works perfectly. GPS追踪效果完美。

The class for the GPS tracking: GPS追踪的类别:

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:: 在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. 如您所知,您已经收集了数据库,其中所有位置的位置都是Lat和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. 对于上述要求,最好的方法是Haversine公式。 check this wiki link : Link 检查此Wiki链接: 链接

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. 1:使用GPS获取用户的经纬度。

2 : Send this GSP Co-ordinates to your php web service where you have implemented Haversin formula. 2:将此GSP坐标发送到已实现Haversin公式的php Web服务。 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. 3:现在您距现有地点最近。

For use Haversin Formula in php web service please refer this links : Link 要在php Web服务中使用Haversin公式,请参考以下链接: 链接

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. 您可以通过计算两个纬度之间的距离(以米为单位)来比较两个纬度坐标,其中至少一个不是完美的,因为例如通过GPS测量。
If the distance is lower than a specific threshold, you can consider them as matching. 如果距离小于特定阈值,则可以将它们视为匹配。

Android has a method for that distance calculation. Android有一种用于距离计算的方法。

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

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