Nehalkumar
Nehalkumar

Reputation: 219

How to draw path between two marker?

I am using osm in my android application. And i have to show a path between two marker.I have used path overlay but line not draw.

Anyone knows how to draw line between two marker in android osm map ?

Upvotes: 1

Views: 2494

Answers (3)

Nehalkumar
Nehalkumar

Reputation: 219

I got the solution — here is the answer:

new Thread(new Runnable()
    {
        public void run() 
        {
            RoadManager roadManager = new OSRMRoadManager();
            ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>();
            GeoPoint startPoint = new GeoPoint(source_lat, source_longi);               
            waypoints.add(startPoint);
            GeoPoint endPoint = new GeoPoint(desti_lat,desti_longi);
            waypoints.add(endPoint);                    
            try 
            {
                road = roadManager.getRoad(waypoints);
            } 
            catch (Exception e)
            {
                e.printStackTrace();
            }

            runOnUiThread(new Runnable() 
            {
                public void run() 
                {
                    if (road.mStatus != Road.STATUS_OK)
                    {
                          //handle error... warn the user, etc. 
                    }

                    Polyline roadOverlay = RoadManager.buildRoadOverlay(road, Color.RED, 8, context);
                    map.getOverlays().add(roadOverlay);                 
                }
            });
        }
    }).start(); 

And I am using two JAR files: slf4j-android-1.5.8.jar and osmdroid-android-4.2.jar, and osmbonuspack library.

Upvotes: 4

Karussell
Karussell

Reputation: 17375

You can use the open source route planner GraphHopper (where I'm the author of) which works offline on Android and iOS. See the Android documentation, which uses mapsforge and the code is here:

mapView = new MapView(this);
...
mapView.getLayerManager().getLayers().add(createPolyline(resp));

Or you can use it via online connection too, here are JavaScript and Java clients.

Upvotes: 1

Karthika PB
Karthika PB

Reputation: 1373

 float[] arr=new float[5];

            android.location.Location.distanceBetween(latitude,longitude,(Double.parseDouble(bn.getString("lat"))),(Double.parseDouble(bn.getString("lon"))),arr);

            System.out.println("distance"+arr[0]);
            if(arr[0]>5){

            if(isNetworkAvailable()){

                if(latitude!=0){

                 findDirections( latitude, longitude,Double.parseDouble(bn.getString("lat")),Double.parseDouble( bn.getString("lon")), GMapV2Direction.MODE_DRIVING );

                }else
                    Toast.makeText(getApplicationContext(), "sorry can't access your current location",1500).show();

            }

public void findDirections(double fromPositionDoubleLat, double fromPositionDoubleLong, double toPositionDoubleLat, double toPositionDoubleLong, String mode)
    {
        Map<String, String> map = new HashMap<String, String>();
        map.put(GetDirectionsAsyncTask.USER_CURRENT_LAT, String.valueOf(fromPositionDoubleLat));
        map.put(GetDirectionsAsyncTask.USER_CURRENT_LONG, String.valueOf(fromPositionDoubleLong));
        map.put(GetDirectionsAsyncTask.DESTINATION_LAT, String.valueOf(toPositionDoubleLat));
        map.put(GetDirectionsAsyncTask.DESTINATION_LONG, String.valueOf(toPositionDoubleLong));
        map.put(GetDirectionsAsyncTask.DIRECTIONS_MODE, mode);

        GetDirectionsAsyncTask asyncTask = new GetDirectionsAsyncTask(this);
        asyncTask.execute(map); 
    }
    public void handleGetDirectionsResult(ArrayList<LatLng> directionPoints) {
        PolylineOptions rectLine = new PolylineOptions().width(5).color(Color.RED);

        for(int i = 0 ; i < directionPoints.size() ; i++) 
        {          
            rectLine.add(directionPoints.get(i));
        }
        if (newPolyline != null)
        {
            newPolyline.remove();
        }
        newPolyline = map.addPolyline(rectLine);
        Display display = getWindowManager().getDefaultDisplay();


            latlngBounds = createLatLngBoundsObject(new LatLng(latitude, longitude),new LatLng(Double.parseDouble(bn.getString("lat")),Double.parseDouble( bn.getString("lon"))));
            map.animateCamera(CameraUpdateFactory.newLatLngBounds(latlngBounds, display.getWidth(), display.getHeight(), 150));



    }
    private LatLngBounds createLatLngBoundsObject(LatLng firstLocation, LatLng secondLocation)
    {
        if (firstLocation != null && secondLocation != null)
        {
            LatLngBounds.Builder builder = new LatLngBounds.Builder();    
            builder.include(firstLocation).include(secondLocation);

            return builder.build();
        }
        return null;
    }

Upvotes: 0

Related Questions