gpt4 book ai didi

android - 如何使用沿折线的实时位置更新标记?

转载 作者:行者123 更新时间:2023-12-02 16:59:17 25 4
gpt4 key购买 nike

我的问题标题似乎是现有的,但这是我的完整场景。

我有一个基于 map 的操作 Activity ,我正在沿着道路绘制折线,比方说两个位置之间的路线。基本上,该应用程序会跟踪用户的当前位置(驾车旅行)。所以直到部分一切正常,如正确显示路线,设备位置 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);
}

带有完整的源代码:

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)
);
}
}

你会得到类似的东西:

Marker snapped to path

但更好的方法是计算汽车与路径起点的距离,并通过 SphericalUtil.interpolate() 找到它在路径上的位置因为如果几个路径段彼此靠近(例如在同一条道路的不同车道上):

Wrong nearest segment

当前汽车位置可能是最近的“错误”路段。因此,计算汽车距离路线起点的距离并使用 SphericalUtil.interpolate()用于确定路径上的点。

关于android - 如何使用沿折线的实时位置更新标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54903593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com