如何在两个标记之间绘制路径?

时间:2015-03-17 07:07:06

标签: android openstreetmap osmdroid

我在我的android应用程序中使用osm。而且我必须在两个标记之间显示一条路径。我使用了路径叠加但是没有画线。

任何人都知道如何在android osm map中的两个标记之间画线?

3 个答案:

答案 0 :(得分:3)

我得到了解决方案 - 这是答案:

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

我正在使用两个JAR文件:slf4j-android-1.5.8.jarosmdroid-android-4.2.jar以及osmbonuspack库。

答案 1 :(得分:1)

您可以使用在Android和iOS上脱机工作的开源路由规划器GraphHopper(我是其作者)。请参阅使用mapsforge的the Android documentation,代码为here

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

或者您也可以通过在线连接使用它,这里有JavaScriptJava个客户。

答案 2 :(得分: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;
    }