我怎样才能在自己的路上画路线?

时间:2017-08-01 07:50:50

标签: android routes mapbox

{
"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路径并在地图上绘制路径,但是我如何让这两个点跟随我的路径创建路径?谢谢!

2 个答案:

答案 0 :(得分:0)

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

    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  
这是单个标记(Source)的示例,但您可以将这些值存储在临时变量中,并可以获取另一个标记(Dest。)。在获取这两个点之后,您可以在它们之间创建路径使用Polyline。

答案 1 :(得分:0)

首先检查你是否得到了合适的GeoJson。然后使用以下方法绘制方向。 如果您找到了正确的,那么使用此代码从响应中获取waypoint数组:

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);
        }
    }
相关问题