0

私は大学の屋内地図アプリを開発しています。地図の各場所の経度と緯度をデータベースに保存しました。次のステップは、GPS からユーザーの位置を取得し、データベースと比較して、ユーザーにその位置の名前を与えることができるようにすることです。私の問題は比較ステップにあります - どうすればできますか? GPS追跡は完全に機能します。

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;
  }

}

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();
}
4

2 に答える 2

1

おっしゃる通り、あなたのすべての場所が緯度と経度をコラージュしたデータベースのコレクションがすでに作成されています。コラージュの食堂の近くにいるユーザーは、今すぐアプリを開くと、ユーザーが現在立っている最も近い場所を見つける必要があります。私はあなたのコラージュの上記の要件を想定しています。

上記の要件の場合、最善の方法は Haversine 式です。このwikiリンクをチェックしてください:リンク

その上でいくつかの研究開発を行います。この式を使用すると、テクニカル フローは次のようになります。

1 : GPS を使用してユーザーの緯度と経度を取得します。

2 : この GSP 座標を、Haversin 式を実装した php Web サービスに送信します。この式を使用すると、データベースから現在の場所から最も近い場所を見つけることができます。

3 : これで、現在の場所から最も近い場所ができました。

PHP Web サービスで Haversin Formula を使用するには、次のリンクを参照してください:リンク

あなたがやりたいことを手に入れてください。それについていくつかの研究開発を行ってください。あなたがそれを解決すると確信しています。

于 2013-05-20T11:43:25.443 に答える
0

GPS などで測定されたために少なくとも 1 つが完全ではない 2 つの Lat Lon 座標を比較し、それらの間の距離をメートル単位で計算します。
距離が特定のしきい値よりも低い場合、一致していると見なすことができます。

Android には、その距離を計算するメソッドがあります。

于 2013-05-20T11:14:49.753 に答える