[英]finding current location in android through GPS
在更改其非常短的距离(如3-4英尺)的位置后,是否可以通过“ GPS”获得用户的准确纬度和经度。
实际上,我已经在android中制作了一个应用程序,该应用程序可以给我用户的经纬度,但是它会在用户的同一点或同一位置更改其值。
请帮我解决这个问题。 提前致谢。
您应该在活动中实施locationlistener
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters
// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute
boolean isGPSEnabled = false;
LocationManager locationManager = (LocationManager) yourapplicationcontext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
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();
}
}
}
}
//Add this permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
我的经验说,这取决于您使用的手机/设备,如果您的设备支持AGPS,则获取GPS坐标将变得更加轻松快捷(与您将要使用android SDK的编码技能无关)。 普通的GPS也会返回准确的数据,请尝试从Google Play下载Savaari应用程序,它的工作原理就像魅力一样,您的第二个问题是可以在特定时间或距离间隔后获取坐标,无论何时注册列表服务器,您都必须提供时间和要接收GPS值的距离(以米为单位)。
查看此代码段:-
public void startReadingGPS(int readAfterMinimumTime, int readAfterMinimumDistance ){
locationManager = (LocationManager)mContext.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, readAfterMinimumTime, readAfterMinimumDistance, this);
}
公共无效requestLocationUpdates(字符串提供程序,长minTime,浮动minDistance,LocationListener侦听器)已在API级别1中添加。使用命名的提供程序和未决的意图注册位置更新。
有关如何使用此方法的更多详细信息,请参见requestLocationUpdates(long,float,Criteria,PendingIntent)。
参数provider提供方名称,用于注册的minTime位置更新之间的最小时间间隔(以毫秒为单位)minDistance位置更新之间的最小距离,以米为单位。listener一个LocationListener,其每次位置更新将调用其onLocationChanged(Location)方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.