
如何在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));}OUTPUT
1.如果我使用this.showCurvedpolyline(latLng1,latLng2,0.1);然后得到:
正如您在上面的图像中看到的那样,我们非常接近我们的目标,但不知道为什么它没有与另一个终点连接
2.如果我使用this.showCurvedpolyline(latLng1,latLng2,1);然后得到:
3.如果我使用LatLng c = SphericalUtil.computeOffset(p,x,h – 90.0);然后得到:
注意:我不想要这么大的圆形,真的我不想要那么高的高度.
这就是我想要的ARC Shape,如下图所示
这是我用来在两个地理位置之间添加弯曲折线的CODE:
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);} 生成的折线显示在我的屏幕截图中
我希望这有帮助!
总结以上是内存溢出为你收集整理的android – 在Google Map中绘制ARC Polyline全部内容,希望文章能够帮你解决android – 在Google Map中绘制ARC Polyline所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)