0
{
"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
                ]
            ]
        }
    }
]

this is Geojson path,how could I draw two point,and the route will follow this path? I used JOSM to draw.

 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));

                    }

the route is straight line,I want route draw on my Geojson path,how can i do? I want the two marker follow my path to plan the best path,I can load the Geojson path on Android and draw the path on map,but how do i let the two point follow my path to creat the route? thanks!

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
LEI N
  • 1
  • 1

2 Answers2

0

Try this code for making a route along your path:

    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)
    );

For two random points on map, try the answer given here
Get location of clicked point
This is an example of single marker(Source) but you can store these values in temp variables and can get another marker(Dest.).
After getting both the points, you can create a path between them using Polyline.

Lalit Fauzdar
  • 5,953
  • 2
  • 26
  • 50
0

First check you got proper GeoJson. Then for draw direction use below method. If you found proper then get waypoint array from response by using this code:

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

Pass this waypoint array to below method. It will use AsyncTask for draw a polyline.

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);
    }

Below is AsyncTask for draw polyline with multiple route (if possible) with multiple color.

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);
        }
    }
Siddhpura Amit
  • 67
  • 2
  • 10