gpt4 book ai didi

android - 在谷歌地图中绘制 ARC 折线

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:07:57 27 4
gpt4 key购买 nike

如何在 Google map 中绘制圆弧折线?

我已经用过this创建曲线折线的代码。

下面是绘制曲线折线的方法:

private void showCurvedPolyline (LatLng p1, LatLng p2, double k) {
//Calculate distance and heading between two points
double d = SphericalUtil.computeDistanceBetween(p1,p2);
double h = SphericalUtil.computeHeading(p1, p2);

//Midpoint position
LatLng p = SphericalUtil.computeOffset(p1, d*0.5, h);

//Apply some mathematics to calculate position of the circle center
double x = (1-k*k)*d*0.5/(2*k);
double r = (1+k*k)*d*0.5/(2*k);

LatLng c = SphericalUtil.computeOffset(p, x, h + 90.0);

//Polyline options
PolylineOptions options = new PolylineOptions();
List<PatternItem> pattern = Arrays.<PatternItem>asList(new Dash(30), new Gap(20));

//Calculate heading between circle center and two points
double h1 = SphericalUtil.computeHeading(c, p1);
double h2 = SphericalUtil.computeHeading(c, p2);

//Calculate positions of points on circle border and add them to polyline options
int numpoints = 100;
double step = (h2 -h1) / numpoints;

for (int i=0; i < numpoints; i++) {
LatLng pi = SphericalUtil.computeOffset(c, r, h1 + i * step);
options.add(pi);
}

//Draw polyline
mMap.addPolyline(options.width(10).color(Color.MAGENTA).geodesic(false).pattern(pattern));
}

输出

1。如果我使用 this.showCurvedPolyline(latLng1, latLng2, 0.1);然后得到:

enter image description here

如您在上图中所见,我们非常接近我们的目标,但不知道为什么它没有连接到另一个端点

2。如果我使用 this.showCurvedPolyline(latLng1, latLng2, 1);然后得到:

enter image description here

3。如果我使用 LatLng c = SphericalUtil.computeOffset(p, x, h - 90.0);然后得到:

enter image description here

注意:我不想要这么大的大圆圈,真的我不想要那么高度.

这是我想要的弧形,如下图所示

enter image description here

这是我用来在两个 geo-locatios 之间添加曲线折线代码:

private void addCurvedPolyLine() {

LatLng latLng1 = new LatLng(40.7128, 74.0059); // New York
LatLng latLng2 = new LatLng(51.5074, 0.1278); // London

Marker marker1 = mMap.addMarker(new MarkerOptions().position(latLng1).title("Start"));
Marker marker2 = mMap.addMarker(new MarkerOptions().position(latLng2).title("End"));

LatLngBounds.Builder builder = new LatLngBounds.Builder();

builder.include(marker1.getPosition());
builder.include(marker2.getPosition());

LatLngBounds bounds = builder.build();
int padding = 0; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.moveCamera(cu);
mMap.animateCamera(cu);

this.showCurvedPolyline(latLng1, latLng2, 0.1);

}

最佳答案

我在another question中提出的解决方案专注于非常小距离的弯曲折线。例如,当您有一条 Directions API 路线,其中起点和终点与道路对齐,但原始请求中的真正起点和终点是建筑物的位置,因此您可以使用这条弯曲的虚折线连接道路和建筑物.

如果像您的示例中的距离很远,我相信您可以只使用 API 中提供的测地线折线。您可以将折线选项的测地线属性设置为 true。

public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

mMap.getUiSettings().setZoomControlsEnabled(true);

LatLng latLng1 = new LatLng(40.7128, 74.0059); // New York
LatLng latLng2 = new LatLng(51.5074, 0.1278); // London

Marker marker1 = mMap.addMarker(new MarkerOptions().position(latLng1).title("Start"));
Marker marker2 = mMap.addMarker(new MarkerOptions().position(latLng2).title("End"));

List<PatternItem> pattern = Arrays.<PatternItem>asList(new Dash(30), new Gap(20));
PolylineOptions popt = new PolylineOptions().add(latLng1).add(latLng2)
.width(10).color(Color.MAGENTA).pattern(pattern)
.geodesic(true);
mMap.addPolyline(popt);

LatLngBounds.Builder builder = new LatLngBounds.Builder();

builder.include(marker1.getPosition());
builder.include(marker2.getPosition());

LatLngBounds bounds = builder.build();
int padding = 150; // offset from edges of the map in pixels
CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
mMap.moveCamera(cu);
mMap.animateCamera(cu);
}

生成的折线显示在我的屏幕截图中

enter image description here

希望对您有所帮助!

关于android - 在谷歌地图中绘制 ARC 折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46411266/

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