【发布时间】:2018-03-06 18:44:24
【问题描述】:
如何在谷歌地图中绘制弧折线?
我已经使用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);然后得到:
如上图所示,我们非常接近目标,但不知道为什么它没有连接到另一个端点
2。如果我使用 this.showCurvedPolyline(latLng1, latLng2, 1);然后得到:
3。如果我使用 LatLng c = SphericalUtil.computeOffset(p, x, h - 90.0);然后得到:
注意:我不想要这么大的圆形,真的我不想要那么高度。
这是我想要一个弧形,如下图所示
这是我用来在两个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);
}
【问题讨论】:
-
他提供了一个解决方案,使用 k=1 而不是 k==1,请参阅他的第二个问题快速修复here。
-
@LalitSinghFauzdar 我在上述方法的任何地方都没有找到 k==1,你能告诉我我需要在哪里进行更改吗?
-
看到这个函数调用 this.showCurvedPolyline(sydney1,sydney2, 0.5);这里 0.5 是 k 的值,这里尝试传递 1。
-
对不起,我忘了告诉你我已经尝试过 1.0 和 1 但同样的问题:this.showCurvedPolyline(latLng2, latLng1, 1);
-
正如他们所提到的“最后一个参数 k 定义了折线的曲率,它可以是 >0 和
标签: android google-maps google-maps-android-api-2 android-custom-view polyline