【问题标题】:How can I draw route on own path?如何在自己的路径上绘制路线?
【发布时间】:2017-08-04 17:15:10
【问题描述】:
{
"type":"FeatureCollection",
"generator":"JOSM",
"features":[
    {
        "type":"Feature",
        "properties":{
        },
        "geometry":{
            "type":"LineString",
            "coordinates":[
                [
                    121.54821846780,
                    24.98741107673
                ],
                [
                    121.54812039953,
                    24.98739360280
                ],
                [
                    121.54812750162,
                    24.98736155308
                ],
                [
                    121.54813477853,
                    24.98732871440
                ],
                [
                    121.54814403650,
                    24.98728693577
                ]
            ]
        }
    },
    {
        "type":"Feature",
        "properties":{
        },
        "geometry":{
            "type":"LineString",
            "coordinates":[
                [
                    121.54813477853,
                    24.98732871440
                ],
                [
                    121.54819734540,
                    24.98733966151
                ],
                [
                    121.54819365737,
                    24.98735561598
                ]
            ]
        }
    },
    {
        "type":"Feature",
        "properties":{
        },
        "geometry":{
            "type":"LineString",
            "coordinates":[
                [
                    121.54812750162,
                    24.98736155308
                ],
                [
                    121.54780189872,
                    24.98730374867
                ],
                [
                    121.54776282754,
                    24.98729681235
                ]
            ]
        }
    }
]

这是Geojson路径,我怎么画两点,路径会沿着这条路径走? 我用 JOSM 画画。

 if (route == true) {
                        start = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(point.getLatitude(), point.getLongitude())).title("start").icon(icon));
                        route = false;
                    } else {
                        if(destination!=null){
                            mapboxMap.removeMarker(destination);
                        }
                        destination = mapboxMap.addMarker(new MarkerOptions().position(new LatLng(point.getLatitude(), point.getLongitude())).title("finish"));

                        LatLng[] points = new LatLng[2];
                        LatLng loc = new LatLng(start.getPosition());
                        LatLng dest = new LatLng(destination.getPosition());
                        points[0] = loc;
                        points[1] = dest;
                        if (poly != null) {
                            mapboxMap.removePolyline(poly);
                        }
                        poly = mapboxMap.addPolyline(new PolylineOptions()
                                .add(points)
                                .color(Color.parseColor("#3887be"))
                                .width(5));

                    }

路线是直线,我想在我的 Geojson 路径上绘制路线,我该怎么做? 我希望两个标记跟随我的路径来规划最佳路径,我可以在 Android 上加载 Geojson 路径并在地图上绘制路径,但是我如何让两点跟随我的路径来创建路线?谢谢!

【问题讨论】:

    标签: android routes mapbox


    【解决方案1】:

    尝试使用此代码沿您的路径制作路线:

        mMap = googleMap;
        mMap.addPolyline(new PolylineOptions().add(
        new LatLng(Longitude,Latitude),
        new LatLng(Longitude,Latitude), // add many as you want
        )
             .width(10)
             .color(Color.Red)
        );
    

    对于地图上的两个随机点,试试这里给出的答案
    Get location of clicked point
    这是单个标记(源)的示例,但您可以将这些值存储在临时变量中,并可以获得另一个标记(目标。)。
    获取两个点后,您可以使用折线在它们之间创建一条路径.

    【讨论】:

    • 谢谢!但是当我手动点击随机的起点和终点时,如何使路径绘制在我的路径上?
    • 正在编辑我的答案...已编辑
    【解决方案2】:

    首先检查你有正确的 GeoJson。然后为绘制方向使用下面的方法。 如果您发现合适,则使用以下代码从响应中获取航点数组:

    List<DirectionsRoute> route = response.body().getRoutes().get(0).getGeometry().getWaypoints();
    

    将此航点数组传递给下面的方法。它将使用 AsyncTask 绘制一条折线。

    public void drawDirection(List<DirectionsRoute> route) {
            map.clear();
            map.addMarker(new MarkerOptions()
                    .title("Destination")
                    .snippet("Distance: "+String.valueOf(route.get(0).getDistance()/1000))
                    .position(destLatlng));
            new DrawGeoJson().execute(route);
        }
    

    下面是 AsyncTask 用于绘制具有多条路线(如果可能)和多种颜色的折线。

    private class DrawGeoJson extends AsyncTask<List<DirectionsRoute>,String,List<List<LatLng>>>
        {
            @Override
            protected void onPreExecute() {
    
            }
    
            @Override
            protected List<List<LatLng>> doInBackground(List<DirectionsRoute>... params) {
    
                List<List<LatLng>> pointsArray =new ArrayList<>();
                List<LatLng> points = new ArrayList<>();
                String jsonResponse= getString(R.string.jsonResponse);
                String ch=jsonResponse.substring(95,110);
                Log.d("Char",String.valueOf(ch));
    
                try {
                    List<DirectionsRoute> routeList=params[0];
                  //If more than one route available then this loop will iterate for every route.
                    for(int i=0;i<routeList.size();i++) {
                        points=new ArrayList<>();
                        DirectionsRoute route = params[0].get(i);
                        for (List<Double> doubleList : route.getGeometry().getCoordinates()) {
                            LatLng latLng = new LatLng(doubleList.get(1), doubleList.get(0));
                            points.add(latLng);
                        }
                        pointsArray.add(points);
                    }
                } catch (Exception exception) {
                    Log.e("LOG", "Exception Loading GeoJSON: " + exception.toString());
                    exception.printStackTrace();
                }
                return pointsArray;
            }
    
            @Override
            protected void onPostExecute(List<List<LatLng>> pointsArray) {
                super.onPostExecute(pointsArray);
                if(pointsArray.size()>0) {
                    int[] rainbow=MainActivity.this.getResources().getIntArray(R.array.rainbow);
                    for(int i=0;i<pointsArray.size();i++) {
                        int colorForPolyline;
                        if(i>rainbow.length){
                            colorForPolyline=i%7;
                        }else{
                            colorForPolyline=i;
                        }
                        if (pointsArray.get(i).size() > 0) {
                            map.addPolyline(new PolylineOptions()
                                    .addAll(pointsArray.get(i))
                                    .color(rainbow[colorForPolyline])
                                    .width(4));
                        }
                    }
                }
    
                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(sourceLatlng) // Sets the center of the map to Maracanã
                        .bearing(0) // Sets the orientation of the camera to look west
                        .tilt(20) // Sets the tilt of the camera to 30 degrees
                        .build(); // Creates a CameraPosition from the builder
                map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition), 3000, null);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      相关资源
      最近更新 更多