我想开发一个小型应用程序来计算旅行时的距离。 我已经尝试过基于Android的GPS的解决方案,但结果是错误的。
请帮我解决这个问题
我的代码:
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 10, this);
public void onLocationChanged(Location location) {
if ((location != null)) {
if (location.getAccuracy()<50 && location.getSpeed()>0) {
onlocationChangeMethod(location);
lastlatitude = location.getLatitude();
lastlongitude = location.getLongitude();
}
}
}
public void onlocationChangeMethod(Location location){
if (lastlatitude > 0 ) {
float[] results = new float[5];
location.distanceBetween(location.getLatitude(), location.getLongitude(),
lastlatitude, lastlongitude, results);
}
}
答案 0 :(得分:2)
距离才能准确。因此,一种方法是根据gps坐标移位计算距离。这不能100%准确
还听说Ola / Uber汽车仪表板内部装有内部跟踪装置,可以准确跟踪车队并与他们的服务器通信(跟踪设备附带SIM卡,以便他们可以与拥有网关的服务器通信安装)。这意味着,如果Ola和Uber的距离非常准确,他们可能会依赖安装在汽车仪表板内的跟踪设备,并通过服务器获取信息。
答案 1 :(得分:0)
<强> GPS 强>: 将A视为起点。
每隔X秒(比如10秒)继续在2点(A和当前位置)之间添加GPS距离。检查Android位置。distanceTo
或distanceBetween
。 Check My Tracks app,
它是开源的。 GPS在室内不可用,如果用户频繁改变方向(每1-2秒读取一次),则会出现错误
要获得准确距离,请使用您的位置纬度和经度调用Google WebService,它将返回距离精确的距离
private String getDistanceOnRoad(double latitude, double longitude,
double prelatitute, double prelongitude) {
String result_in_kms = "";
String url = "http://maps.google.com/maps/api/directions/xml?origin="
+ latitude + "," + longitude + "&destination=" + prelatitute
+ "," + prelongitude + "&sensor=false&units=metric";
String tag[] = { "text" };
HttpResponse response = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
response = httpClient.execute(httpPost, localContext);
InputStream is = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(is);
if (doc != null) {
NodeList nl;
ArrayList args = new ArrayList();
for (String s : tag) {
nl = doc.getElementsByTagName(s);
if (nl.getLength() > 0) {
Node node = nl.item(nl.getLength() - 1);
args.add(node.getTextContent());
} else {
args.add(" - ");
}
}
result_in_kms = String.format("%s", args.get(0));
}
} catch (Exception e) {
e.printStackTrace();
}
return result_in_kms;
}