2013-05-20 45 views
0

我正在爲我的大學開發室內地圖應用程序。我在數據庫中存儲了地圖中每個位置的經度和緯度。下一步是從GPS獲取用戶的位置,並與數據庫進行比較,以便能夠向用戶提供其位置的名稱。我的問題是與比較步驟 - 我該怎麼做? GPS追蹤完美。GPS追蹤比較(Android)

的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(); 
} 
+0

你想比較用戶的位置與你的拼貼畫嗎?你想知道用戶來自你的拼貼畫還是你的拼貼畫?對 ?請簡要說明您想要的內容...... –

+0

我希望應用程序能夠在大學中找到用戶位置。我爲數據庫中的每個地方存儲了經度和緯度。現在,用戶將運行該應用程序,他站在大學的某個地方。我希望應用程序通過將他當前的long和lat與數據庫中的值進行比較來告訴他他在哪裏(顯示該地點的名稱)。希望現在很清楚。謝謝 – user2400799

+0

你的意思是你已經將每個地方的信息存儲在你的數據庫服務器中。像圖書館,部門,實驗室,教室等的座標。現在,例如,站在圖書館附近的用戶和他/她打開應用程序,他/她可以找到圖書館作爲他們最近的地方.....對嗎? –

回答

1

正如你所說,你已經制作了數據庫的集合,其中你所有的地方拼貼了經緯度。現在,任何靠近食堂的用戶現在都可以打開您的應用程序 應用程序應該找到最近的用戶站立的位置。 我假設您的拼貼的上述要求。

對於上述要求,最好的方法是Haversine公式。請查看這個wiki鏈接:Link

並製作一些R & D就可以了。現在通過使用這個公式,技術流程如下:

1:獲取用戶的緯度和長期使用GPS。

2:將此GSP座標發送到您已實施Haversin公式的php web服務。通過使用此公式,您可以從數據庫中找到離您最近的地方。

3:現在你離你現有的地方最近的地方了。

在PHP的Web服務,請參閱該鏈接使用Haversin公式:Link

希望你得到你想要做什麼。請做一些R & D,我相信你會解決它。

0

你比較兩個緯度經度座標,其中至少有一個是不完美的跟蹤代碼,因爲測量通過例如GPS,通過計算它們之間的以米爲單位的距離。
如果距離低於特定閾值,則可以將它們視爲匹配。

Android有一個用於計算距離的方法。