Java GPS跟踪比较(Android)
我正在为我的大学开发一个室内地图应用程序。我已将地图中每个地方的经度和纬度存储在数据库中。下一步是从GPS获取用户的位置,并与数据库进行比较,以便能够为用户提供其位置的名称。我的问题是比较步骤——我怎么做?GPS跟踪工作得很好 GPS跟踪的类别:Java GPS跟踪比较(Android),java,android,gps,Java,Android,Gps,我正在为我的大学开发一个室内地图应用程序。我已将地图中每个地方的经度和纬度存储在数据库中。下一步是从GPS获取用户的位置,并与数据库进行比较,以便能够为用户提供其位置的名称。我的问题是比较步骤——我怎么做?GPS跟踪工作得很好 GPS跟踪的类别: public class GPSTracker { public GPSTracker(Context context) { this.mContext = context; getLocation(); } public
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();
}
您可以比较两个横向坐标,其中至少有一个不完美,因为通过计算它们之间的距离(以米为单位),例如GPS测量。
如果距离低于特定的阈值,可以将它们视为匹配。
Android有一种计算距离的方法。正如你所说,你已经收集了一个数据库,你所有的位置都拼贴了Lat和Long。现在,任何靠近拼贴食堂的用户现在都可以打开你的应用程序 应用程序应该找到用户现在站立的最近位置。 我假设你们学院有上述要求 对于上述要求,最好的方法是哈弗森公式。检查此wiki链接: 并对其进行一些研发。现在使用这个公式,技术流程如下: 1:使用GPS获取用户的lat和long 2:将此GSP坐标发送到您的php web服务,您在那里实现了Haversin公式。通过使用此公式,您将从数据库中找到离您当前位置最近的位置 3:现在您有了离您现有位置最近的位置 有关在php web服务中使用Haversin公式的信息,请参考以下链接:
希望你得到你想做的。请对它进行一些研究和开发,我相信你会解决它。你想比较用户的位置和你的拼贴吗?你想知道用户来自你的拼贴还是在你的拼贴之外?正确的?请简单解释一下你到底想要什么……我希望应用程序能找到大学里的用户位置。我在数据库中存储了学院每个地方的经度和纬度。现在,用户将运行该应用程序,他站在学院的某个地方。我希望应用程序通过比较他当前的long和lat与数据库中的值来告诉他在哪里(显示地点的名称)。希望现在一切都清楚了。谢谢你的意思是你已经在数据库服务器中存储了每个位置的信息。比如图书馆、部门、实验室、教室等的坐标。例如,现在一个用户站在图书馆附近,打开应用程序,他/她可以找到图书馆作为最近的地方。。。。。对吧?对。正是我需要的!!!GPS在室内不起作用,只在靠近窗户的地方,然后被扭曲了30-50米甚至更多。你试过了吗?