【问题标题】:Android GoogleMap V2 : How can I draw a line with specific length?Android GoogleMap V2:如何画一条特定长度的线?
【发布时间】:2015-04-02 08:31:57
【问题描述】:

我的项目中有一个 GoogleMap。它设置为 18 级缩放。我想画一条 1 米长的线。我看到并使用了这样的代码:

googleMap.addCircle(new CircleOptions()
     .center(latLng1)
     .radius(5)
     .fillColor(Color.BLUE));     

我以米为单位给出了它的半径。我怎么能用一条线来做呢?(polyLine 没有这个选项)一条具有特定 LatLng 和特定方向(例如:从北向)和特定长度的线? 我可以通过 sin 和 cos 指定方向。但是我可以为线的长度做些什么?

【问题讨论】:

    标签: android google-maps google-maps-android-api-2 polyline


    【解决方案1】:

    对于给定的点,只有一个具有给定半径的圆。但是对于线条,情况有点不同。对于给定点,从该点和给定长度开始有无限数量的线。因此,你不能简单地画出这样的线。

    一种方法是在半径为 1 米的圆上选择一个点并将您的点居中。 Here 是一个很好的例子,如何计算给定半径的圆上的点。不仅仅是在两点之间画一条线。

    更新:

    这可能会帮助您如何找到圆圈上的 LatLng 点LatLng Points on circle on Google Map V2 in Android

    【讨论】:

    • 如果我指定方向......它只需要画一条线。一点,长度和方向(度)...我要试试你的建议页面
    • 问题是:如何指定1米?例如:LatLng2.longitude=r x cos(a);和 LatLng2.latitude=r x sin(a);但是我如何向 googlemap 解释,“r”是 1 米?
    • 非常感谢……我用过,但是有问题。我在这里解释了:stackoverflow.com/questions/28321738/…
    【解决方案2】:

    计算行尾我使用:

        SphericalUtil.computeOffset(LatLng,lenght,heading);
    

    要以米为单位计算宽度,我使用这个:

        public Double calcw(GoogleMap map,int ancho,LatLng p) {
        float tilt = map.getCameraPosition().tilt;
        CameraPosition old=map.getCameraPosition();
        if(tilt!=0){
            CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(old.target)      // Sets the center of the map to Mountain View
                    .zoom(old.zoom)                   // Sets the zoom
                    .bearing(old.bearing)                // Sets the orientation of the camera to east
                    .tilt(0)                   // Sets the tilt of the camera to 30 degrees
                    .build();
            map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
    
        }
        Point g1 =map.getProjection().toScreenLocation(p);
        Point g2=map.getProjection().toScreenLocation(SphericalUtil.computeOffset(p,ancho/10,0));
    
        Double result=(distance(g1,g2));
        //Log.e("PROJ1",Double.toString(distance(g1,g2)));
    
        map.moveCamera(CameraUpdateFactory.newCameraPosition(old));
        return result;
    
            }
        public double distance(Point a, Point b)
    {
        double dx = a.x - b.x;
        double dy = a.y - b.y;
        return Math.sqrt(dx * dx + dy * dy);
    }
    

    【讨论】:

      【解决方案3】:

      使用折线画线如下图,

      private ArrayList<LatLng> mLatlngs ;
      
      PolylineOptions mPolylineOptions = new PolylineOptions();
                          mPolylineOptions.color(Color.RED);
                          mPolylineOptions.width(3);
                          mPolylineOptions.addAll(mLatlngs);
                          mGoogleMap.addPolyline(mPolylineOptions);
      

      【讨论】:

      • 我不知道我下一个点的LatLng...我只是画一条特殊宽度的线。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 2011-04-01
      • 2017-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多