在Google Maps Android的两个纬度长点之间绘制一条曲线(贝塞尔曲线) [英] Draw a curve (bezier curve) between two lat long points on google maps android

查看:274
本文介绍了在Google Maps Android的两个纬度长点之间绘制一条曲线(贝塞尔曲线)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在两个经纬度点之间绘制一条曲线(Beizer曲线).目前,我指的是这篇文章(代码在javascript中).

I want to draw a curve (Beizer curve) between two lat-long points. Currently i am referring this post (code is in javascript).

使用三次方贝塞尔方程获得曲线点的代码

private void drawElementsOnMap(LatLng init, LatLng end) {

    mMap.addMarker(new MarkerOptions().position(init));
    mMap.addMarker(new MarkerOptions().position(end));

    LatLngBounds.Builder bc = new LatLngBounds.Builder();
    bc.include(init);
    bc.include(end);

    mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bc.build(), 100));

    PolylineOptions line = new PolylineOptions();
    line.add(init);
    line.add(end);
    mMap.addPolyline(line);

    double distanceBetween = SphericalUtil.computeDistanceBetween(init, end);
    double lineHeadingInit = SphericalUtil.computeHeading(init, end);  
    double lineHeadingEnd = bearing(end, init);*/

    double lineHeading1, lineHeading2;
    if (lineHeadingInit < 0) {
        lineHeading1 = lineHeadingInit + 45;
        lineHeading2 = lineHeadingInit + 135;
    } else {
        lineHeading1 = lineHeadingInit + -45;
        lineHeading2 = lineHeadingInit + -135;
    }

    LatLng pA = SphericalUtil.computeOffset(init, distanceBetween / 2.5, lineHeading1);
    LatLng pB = SphericalUtil.computeOffset(end, distanceBetween / 2.5, lineHeading2);

    // Beizer curves with 4 points
    gmapsCubicBezier(init, end, pA, pB);
}

private void gmapsCubicBezier(LatLng p1, LatLng p2, LatLng pA, LatLng pB) {

    //Polyline options
    PolylineOptions options = new PolylineOptions();

    LatLng curveLatLng = null;
    for (double t = 0.0; t < 1.01; t += 0.01) {
        // P = (1−t)3P1 + 3(1−t)2tP2 +3(1−t)t2P3 + t3P4; for 4 points
        double arcX = (1 - t) * (1 - t) * (1 - t) * p1.latitude
                + 3 * (1 - t) * (1 - t) * t * pA.latitude
                + 3 * (1 - t) * t * t * pB.latitude
                + t * t * t * p2.latitude;
        double arcY = (1 - t) * (1 - t) * (1 - t) * p1.longitude
                + 3 * (1 - t) * (1 - t) * t * pA.longitude
                + 3 * (1 - t) * t * t * pB.longitude
                + t * t * t * p2.longitude;

        curveLatLng = new LatLng(arcX, arcY);
        options.add(curveLatLng);
        //Draw polyline
        mMap.addPolyline(options.width(5).color(Color.DKGRAY).geodesic(false));
    }
    mMap.addMarker(new MarkerOptions().position(curveLatLng));
}

下面的图片显示了我尝试过的不同测试用例.

Following are the pictures show different test cases I tried.

情况1:P1-孟买,P2-钦奈

情况2:P1-法国,P2-新加坡

情况3:P1-美国,P2-班加罗尔

案例4:P1-美国,P2-新加坡

问题:

1)为什么仅在情况1下,算法才能绘制出相当不错的曲线?

1) Why is algorithm able to draw fairly decent curve only in case 1?

2)在第2种情况下,为什么贝塞尔曲线会在一侧弯曲?

2) Why bezier curve bents on one side in case 2?

3)为什么贝塞尔曲线在情况3和4(在P1(情况3)和P2(情况4)处出现奇怪的尖峰)看起来不同.反正有避免的机会吗?

3) Why bezier curve looks different in case 3 and 4 (strange cusp at P1 (case 3) and at P2 (case 4)). Is there anyway to avoid it?

4)为什么在情况4中折线的绘制方式不同(方向?)?

4) Why polyline is drawn differently (the direction?) in case 4?

我无法理解绘制曲线的方式.如果我做错了任何事情,请告诉我.

I am unable to understand the pattern in which curve is drawn. Please let me know if I am doing anything wrong.

推荐答案

这个答案有些特殊,但是我建议您所看到的实际上是错误的甚至是意外的.如您所知,地球是圆形的,但Google地图(和计算机屏幕)本质上是平坦的并且是二维的.我们开始创建地球的平面2D地图的方法是雕刻一个这样的地球:

This answer is somewhat specative, but I suggest that nothing you are seeing is actually wrong or even unexpected. As you know, the Earth is round, but Google Maps (and computer screens) are inherently flat and two dimensional. The way we begin creating a flat 2D map of the Earth is by carving up a globe like this:

在创建我们都习惯看到的世界的2D地图的过程中,事物会有些扭曲和歪斜.我没有数学来支持它,但是很明显,将全球大部分地区的两个城市连接起来的3D曲线在投影到2D地图上后也会发生扭曲和偏斜.而且我希望曲线越长,失真就会越明显.

Things get somewhat warped and skewed in the process of creating the 2D map of the world to which we are all accustomed to see. I don't have the maths to back it up, but it seems obvious that 3D curves connecting two cities across most of the globe would also get warped and skewed after projecting them onto a 2D map. And I would expect the distortions to get more pronounced the longer the curves are.

这也可以解释为什么从孟买到钦奈的曲线对您来说看起来很正常.它处于相对较短的距离内,由于地球的曲率造成的变形最小.

This also would explain why the curve from Mumbai to Chennai looks normal to you. It is over a relatively short distance, where distortions due to the curvature of the Earth are minimal.

这篇关于在Google Maps Android的两个纬度长点之间绘制一条曲线(贝塞尔曲线)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆