1

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 ?

Nehalkumar
  • 227
  • 2
  • 15
  • may be http://stackoverflow.com/questions/21874351/how-to-display-route-on-my-osm-application or http://stackoverflow.com/questions/10104581/osmdroid-pathoverlay/10109812#10109812 will be useful to you – Hardy Mar 17 '15 at 07:11
  • thnxs for reply but this link not work for me i have tried both – Nehalkumar Mar 17 '15 at 07:21

3 Answers3

4

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.

cmbuckley
  • 40,217
  • 9
  • 77
  • 91
Nehalkumar
  • 227
  • 2
  • 15
1

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.

Karussell
  • 17,085
  • 16
  • 97
  • 197
0
 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;
    }
Karthika PB
  • 1,373
  • 9
  • 16