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有一个用于计算距离的方法。