由网友(聽風憶雪)分享简介:我的应用程序记录上的一个按钮点击GPS定位;然而,当我这样做,说我大约三个街区远,是非常不准确的。你可以看看我的code,看我怎么能提高呢?谢谢@覆盖保护无效doInBackground(虚空......为arg0){共享preferences preFS = getShared preferences(设置,0);最...
我的应用程序记录上的一个按钮点击GPS定位;然而,当我这样做,说我大约三个街区远,是非常不准确的。你可以看看我的code,看我怎么能提高呢?谢谢
@覆盖
保护无效doInBackground(虚空......为arg0){
共享preferences preFS = getShared preferences(设置,0);
最后字符串ID = prefs.getString(ID,);
DefaultHttpClient的HttpClient =新DefaultHttpClient();
HttpPost httpost =新HttpPost(
http://iphone-radar.com/gps/gps_locations);
JSONObject的持有人=新的JSONObject();
尝试 {
holder.put(身份证,身份证);
LocationManager locationManager =(LocationManager)getSystemService(LOCATION_SERVICE);
标准标准=新标准();
字符串bestProvider = locationManager.getBestProvider(标准,
假);
LocationListener的loc_listener =新LocationListener的(){
@覆盖
公共无效onStatusChanged(字符串商,INT地位,捆绑演员){
// TODO自动生成方法存根
}
@覆盖
公共无效onProviderEnabled(字符串提供商){
// TODO自动生成方法存根
}
@覆盖
公共无效onProviderDisabled(字符串提供商){
// TODO自动生成方法存根
}
@覆盖
公共无效onLocationChanged(位置定位){
// TODO自动生成方法存根
}
};
尝试{
活套prepare()。
locationManager.requestLocationUpdates(bestProvider,0,0,loc_listener);
}赶上(例外五){
e.printStackTrace();
}
位置位置= locationManager。
getLastKnownLocation(bestProvider);
日历C = Calendar.getInstance();
SimpleDateFormat的SDF =新的SimpleDateFormat(HH:MMAA DD'/'MM'/'YYYY);
holder.put(时间,sdf.format(c.getTime()));
holder.put(time_since_epoch,System.currentTimeMillis的());
尝试 {
holder.put(纬度,location.getLatitude());
holder.put(LON,location.getLongitude());
}赶上(NullPointerException异常E){
尝试 {
holder.put(土地增值税,-1.0);
holder.put(LON,-1.0);
}赶上(JSONException E1){
// TODO自动生成的catch块
e1.printStackTrace();
}
}
StringEntity本身=新StringEntity(holder.toString());
httpost.setEntity(SE);
httpost.setHeader(接受,应用/ JSON);
httpost.setHeader(内容型,应用/ JSON);
ResponseHandler的ResponseHandler的=新BasicResponseHandler();
字符串响应= httpclient.execute(httpost,ResponseHandler所);
org.json.JSONObject OBJ;
OBJ =新org.json.JSONObject(响应);
}赶上(JSONException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(UnsupportedEncodingException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(ClientProtocolException E){
// TODO自动生成的catch块
e.printStackTrace();
}赶上(IOException异常E){
// TODO自动生成的catch块
e.printStackTrace();
}
解决方案
是的,有绝对的是,尝试加入一些精度到您的 标准
...
标准标准=新标准();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
默认构造函数的标准
国家(重点煤矿)
构造一个新的标准对象。新对象将有否对精度,电力,或响应时间要求;不需要高度,速度,或轴承;不至于让金钱成本。
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);
The default constructor for Criteria
states (emphasis mine)
Constructs a new Criteria object. The new object will have no requirements on accuracy, power, or response time; will not require altitude, speed, or bearing; and will not allow monetary cost.
相关推荐
最新文章