【问题标题】:How to draw path between two marker?如何在两个标记之间绘制路径?
【发布时间】:2015-03-17 07:07:06
【问题描述】:

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

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

【问题讨论】:

标签: android openstreetmap osmdroid


【解决方案1】:

我找到了解决方案——这就是答案:

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 库。

【讨论】:

  • 请使用您问题上的编辑链接添加更多信息。 Post Answer 按钮应仅用于问题的完整答案。
  • 我得到了解决方案,这就是我回答的原因
【解决方案2】:

您可以使用开源路线规划器GraphHopper(我是其中的作者),它可以在 Android 和 iOS 上离线工作。见the Android documentation,使用mapsforge,代码为here

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

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

【讨论】:

    【解决方案3】:
     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;
        }
    

    【讨论】:

    • 我认为这是谷歌地图而不是开放街道地图
    猜你喜欢
    • 2019-02-01
    • 2015-09-19
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多