我已经实现了一个每秒跟踪用户位置的功能,并将跟踪的位置添加到折线,绘制用户的路径。麻烦的是我收集的数据,结果不精确。
下面是我在附近街道的驱动器上进行的测试运行图像的链接。黑线是收到的数据,注意线如何偶尔从一点跳到另一点。红线大致是折线应遵循的路径,因为它遵循我驾驶和跟踪Android手机中的位置数据时所采取的道路。
https://i.imgur.com/9nWEfna.png
以下是与我实施的路径追踪功能相关的代码。每次按下“跟踪”按钮时,都会创建一个新的折线,并且线程每秒开始执行以报告用户的位置。收到后,该位置的latlng将添加到折线,并重新绘制折线以反映新点的添加。
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
return;
}
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location == null) return;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title("Current Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f));
}
});
trackButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
final ArrayList currpath = new ArrayList<LatLng>();
paths.add(currpath);
PolylineOptions currpathlineoptions = new PolylineOptions();
final Polyline currpathline = mMap.addPolyline(currpathlineoptions);
tracking = true;
MapsActivity.this.runOnUiThread(new Runnable() {
@SuppressLint("MissingPermission")
public void run() {
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(MapsActivity.this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
// Got last known location. In some rare situations this can be null.
if (location == null) return;
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 15f));
currpath.add(latLng);
currpathline.setPoints(currpath);
}
});
if(tracking){
Handler h = new Handler();
h.postDelayed(this, 1000);
}
}
});
} else {
tracking = false;
}
}
});
}
}
在处理谷歌地图方面有经验的人能否告诉我为什么LatLngs报告从一点到另一点跳跃并且不准确地跟踪我开车的路线?