本文介绍了我一直在 Android 中获取不准确的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的应用程序在单击按钮时记录 gps 位置;但是,当我这样做时,它说我在三个街区之外并且非常不准确.你能看看我的代码,看看我可以如何改进它吗?谢谢
my app records the gps location on a button click; however when I do it says I'm about three blocks away and is extremely inaccurate. Can you take a look at my code to see how I can improve it? Thanks
@Override
protected Void doInBackground(Void... arg0) {
SharedPreferences prefs = getSharedPreferences("Settings", 0);
final String id = prefs.getString("ID", "");
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(
"http://iphone-radar.com/gps/gps_locations");
JSONObject holder = new JSONObject();
try {
holder.put("id", id);
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria,
false);
LocationListener loc_listener=new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
};
try{
Looper.prepare();
locationManager.requestLocationUpdates(bestProvider, 0, 0, loc_listener);
}catch(Exception e){
e.printStackTrace();
}
Location location=locationManager.
getLastKnownLocation(bestProvider);
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("hh:mmaa dd'/'MM'/'yyyy");
holder.put("time", sdf.format(c.getTime()));
holder.put("time_since_epoch", System.currentTimeMillis());
try {
holder.put("lat", location.getLatitude());
holder.put("lon", location.getLongitude());
} catch (NullPointerException e) {
try {
holder.put("lat", -1.0);
holder.put("lon", -1.0);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
StringEntity se = new StringEntity(holder.toString());
httpost.setEntity(se);
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler responseHandler = new BasicResponseHandler();
String response = httpclient.execute(httpost, responseHandler);
org.json.JSONObject obj;
obj = new org.json.JSONObject(response);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
推荐答案
是的,绝对有,尝试为您的 标准
...
Yes there absolutely is, try adding some accuracy to your Criteria
...
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
Criteria
状态的默认构造函数(强调我的)
The default constructor for Criteria
states (emphasis mine)
构造一个新的 Criteria 对象.新对象对准确性没有要求、功率或响应时间;不需要高度、速度或方位;并且不允许货币成本.
这篇关于我一直在 Android 中获取不准确的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!