2013-05-20 4 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

당신은 당신이 databse 서버에있는 모든 장소 정보를 저장했다는 것을 의미합니다. 도서관,학과, 실험실, 교실 등과 같은 좌표. 그리고 이제 예를 들어 도서관 근처에 서서 앱을 열고 가장 가까운 장소로 도서관을 찾을 수있는 사용자 ..... 예? –

답변

1

당신이 말했듯이 당신은 이미 모든 장소가 위도와 경도로 콜라주 된 데이터베이스의 수집을 만들었습니다. 이제 콜라주 매점 근처에있는 사용자가 앱을 엽니 다. 앱은 사용자가 현재 서있는 가장 가까운 장소를 찾아야합니다. 위의 콜라주 요구 사항을 전제로합니다.

위의 요구 사항에 가장 적합한 방법은 Haversine 공식입니다. 이 위키 링크를 확인하십시오. Link

그리고 일부는 R & D가됩니다. 이제이 공식을 사용하여 기술 흐름은 다음과 같습니다.

1 : GPS를 사용하여 사용자의 위도와 경도를 구합니다.

2 :이 GSP 조정자를 Haversin 수식을 구현 한 PHP 웹 서비스로 보내주십시오. 이 수식을 사용하면 데이터베이스에서 현재 장소를 가장 가까운 곳에서 찾을 수 있습니다.

3 : 이제 기존 장소에서 가장 가까운 장소에 있습니다.

Link 당신은 당신이 원하는 것을 얻을 희망 : PHP는 웹 서비스이 링크를 참조하시기 바랍니다에 사용 Haversin 수식에 대한

. 제발 그것에 약간의 R & D를해라. 그러면 나는 당신이 그것을 해결할 것이라고 확신한다.

0

, 적어도 하나는 완벽하지 않습니다에 추적을 표시하는 코드를 측정하기 때문에 예를 들어 GPS를 이용하여 거리를 미터로 계산하여
거리가 특정 임계 값보다 낮은 경우 일치하는 것으로 간주 할 수 있습니다.

Android에는 거리 계산 방법이 있습니다.