San*_*oop 2 gps android location google-maps google-roads-api
我的问题标题似乎是现有的,但这是我的完整场景。
我有一个基于地图的操作活动,其中沿着道路绘制折线,比方说两个位置之间的路线。基本上,该应用程序跟踪用户当前位置(开车旅行)。因此,直到一切正常,如正确显示路线,设备位置 API 正在提供位置更新(有点精确),而且我也能够顺利更改位置更新,
所以问题是,位置更新有时是锯齿形的,有时可能不会接触道路,位置更新会到处都是。
我也研究过 ROAD api,但即使从之前提出的一些问题中也没有得到正确的帮助。
是否可以使标记仅沿着道路移动?
任何形式的帮助将不胜感激。
您可以通过将标记投影到最近的路径段上来将标记捕捉到路径。您可以通过以下方式找到最近的路段PolyUtil.isLocationOnPath():
PolyUtil.isLocationOnPath(carPos, segment, true, 30)
您可以通过将测地球面坐标转换为正交屏幕坐标、计算投影正交坐标并将其转换回球面坐标来找到标记到该段的投影 ( WGS84 LatLng -> Screen x,y -> WGS84 LatLng):
Point carPosOnScreen = projection.toScreenLocation(carPos);
Point p1 = projection.toScreenLocation(segment.get(0));
Point p2 = projection.toScreenLocation(segment.get(1));
Point carPosOnSegment = new Point();
float denominator = (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y);
// p1 and p2 are the same
if (Math.abs(denominator) <= 1E-10) {
markerProjection = segment.get(0);
} else {
float t = (carPosOnScreen.x * (p2.x - p1.x) - (p2.x - p1.x) * p1.x
+ carPosOnScreen.y * (p2.y - p1.y) - (p2.y - p1.y) * p1.y) / denominator;
carPosOnSegment.x = (int) (p1.x + (p2.x - p1.x) * t);
carPosOnSegment.y = (int) (p1.y + (p2.y - p1.y) * t);
markerProjection = projection.fromScreenLocation(carPosOnSegment);
}
Run Code Online (Sandbox Code Playgroud)
带有完整的源代码:
public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private GoogleMap mGoogleMap;
private MapFragment mapFragment;
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(this);
mButton = (Button) findViewById(R.id.button);
mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
@Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
mGoogleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
@Override
public void onMapLoaded() {
List<LatLng> sourcePoints = new ArrayList<>();
PolylineOptions polyLineOptions;
LatLng carPos;
sourcePoints.add(new LatLng(-35.27801,149.12958));
sourcePoints.add(new LatLng(-35.28032,149.12907));
sourcePoints.add(new LatLng(-35.28099,149.12929));
sourcePoints.add(new LatLng(-35.28144,149.12984));
sourcePoints.add(new LatLng(-35.28194,149.13003));
sourcePoints.add(new LatLng(-35.28282,149.12956));
sourcePoints.add(new LatLng(-35.28302,149.12881));
sourcePoints.add(new LatLng(-35.28473,149.12836));
polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(sourcePoints);
polyLineOptions.width(10);
polyLineOptions.color(Color.BLUE);
mGoogleMap.addPolyline(polyLineOptions);
carPos = new LatLng(-35.281120, 149.129721);
addMarker(carPos);
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sourcePoints.get(0), 15));
for (int i = 0; i < sourcePoints.size() - 1; i++) {
LatLng segmentP1 = sourcePoints.get(i);
LatLng segmentP2 = sourcePoints.get(i+1);
List<LatLng> segment = new ArrayList<>(2);
segment.add(segmentP1);
segment.add(segmentP2);
if (PolyUtil.isLocationOnPath(carPos, segment, true, 30)) {
polyLineOptions = new PolylineOptions();
polyLineOptions.addAll(segment);
polyLineOptions.width(10);
polyLineOptions.color(Color.RED);
mGoogleMap.addPolyline(polyLineOptions);
LatLng snappedToSegment = getMarkerProjectionOnSegment(carPos, segment, mGoogleMap.getProjection());
addMarker(snappedToSegment);
break;
}
}
}
});
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(sourcePoints.get(0), 15));
}
private LatLng getMarkerProjectionOnSegment(LatLng carPos, List<LatLng> segment, Projection projection) {
LatLng markerProjection = null;
Point carPosOnScreen = projection.toScreenLocation(carPos);
Point p1 = projection.toScreenLocation(segment.get(0));
Point p2 = projection.toScreenLocation(segment.get(1));
Point carPosOnSegment = new Point();
float denominator = (p2.x - p1.x) * (p2.x - p1.x) + (p2.y - p1.y) * (p2.y - p1.y);
// p1 and p2 are the same
if (Math.abs(denominator) <= 1E-10) {
markerProjection = segment.get(0);
} else {
float t = (carPosOnScreen.x * (p2.x - p1.x) - (p2.x - p1.x) * p1.x
+ carPosOnScreen.y * (p2.y - p1.y) - (p2.y - p1.y) * p1.y) / denominator;
carPosOnSegment.x = (int) (p1.x + (p2.x - p1.x) * t);
carPosOnSegment.y = (int) (p1.y + (p2.y - p1.y) * t);
markerProjection = projection.fromScreenLocation(carPosOnSegment);
}
return markerProjection;
}
public void addMarker(LatLng latLng) {
mGoogleMap.addMarker(new MarkerOptions()
.position(latLng)
);
}
}
Run Code Online (Sandbox Code Playgroud)
你会得到类似的东西:
但更好的方法是计算汽车距路径起点的距离,并通过路径找到它在路径上的位置SphericalUtil.interpolate(),因为如果多个路径段彼此接近(例如在同一道路的不同车道上),如下所示:
到当前汽车位置可能是最近的“错误”路段。因此,计算汽车距路线起点的距离,并用于SphericalUtil.interpolate()准确确定路径上的点。
| 归档时间: |
|
| 查看次数: |
3565 次 |
| 最近记录: |