将折线与openstreetmap中的标记一起拖动?

时间:2018-05-21 05:47:13

标签: android google-maps google-maps-api-3 openstreetmap osmdroid

我目前正在开发一个Android应用程序,允许用户使用Polylines绘制Markers或在用户长按点时在折线上指向如何使用点拖动线也会线在地图上移动。我怎么做到这一点我拖动标记但不能移动标记

public class MainMapActivity extends AppCompatActivity {

GeoPoint startPoint;
MapView map;
Road road;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_map);
    map = (MapView) findViewById(R.id.map);
    map.setTileSource(TileSourceFactory.MAPNIK);
    map.setBuiltInZoomControls(true);
    map.setMultiTouchControls(true);
    GpsTracking gps=new GpsTracking(MainMapActivity.this);
    if (gps.canGetLocation()) {
        double latitude = gps.getLatitude();
        double longitude = gps.getLongitude();
        startPoint = new GeoPoint(latitude, longitude);
        Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
    } else {
        gps.showSettingsAlert();
    }

  //  GeoPoint startPoint = new GeoPoint(48.13, -1.63);
    IMapController mapController = map.getController();
    mapController.setZoom(17);
    mapController.setCenter(startPoint);

    Marker startMarker = new Marker(map);
    startMarker.setPosition(startPoint);
    startMarker.setDraggable(true);
    startMarker.setOnMarkerDragListener(new OnMarkerDragListenerDrawer());
    startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
    map.getOverlays().add(startMarker);


    //Set-up your start and end points:
    RoadManager roadManager = new OSRMRoadManager(this);
    ArrayList<GeoPoint> waypoints = new ArrayList<GeoPoint>();
    waypoints.add(startPoint);
    GeoPoint endPoint = new GeoPoint(31.382108, 74.260107);
    waypoints.add(endPoint);

    // retreive the road between those points:
    Road road = roadManager.getRoad(waypoints);

    // build a Polyline with the route shape:
    Polyline polyline=new Polyline();
    polyline.setOnClickListener(new Polyline.OnClickListener() {
        @Override
        public boolean onClick(Polyline polyline, MapView mapView, GeoPoint eventPos) {
            return false;
        }
    });
    Polyline roadOverlay = RoadManager.buildRoadOverlay(road);

    //Polyline to the overlays of your map:
    map.getOverlays().add(roadOverlay);

    //Refresh the map!
    map.invalidate();
   //3. Showing the Route steps on the map
    FolderOverlay roadMarkers = new FolderOverlay();
    map.getOverlays().add(roadMarkers);
    Drawable nodeIcon = ResourcesCompat.getDrawable(getResources(), R.drawable.marker_node, null);
    for (int i = 0; i < road.mNodes.size(); i++) {
        RoadNode node = road.mNodes.get(i);
        Marker nodeMarker = new Marker(map);
        nodeMarker.setDraggable(true);
        nodeMarker.setOnMarkerDragListener(new OnMarkerDragListenerDrawer());
        nodeMarker.setPosition(node.mLocation);
        nodeMarker.setIcon(nodeIcon);

        //4. Filling the bubbles
        nodeMarker.setTitle("Step " + i);
        nodeMarker.setSnippet(node.mInstructions);
        nodeMarker.setSubDescription(Road.getLengthDurationText(this, node.mLength, node.mDuration));
        Drawable iconContinue = ResourcesCompat.getDrawable(getResources(), R.drawable.ic_continue, null);
        nodeMarker.setImage(iconContinue);
        //4. end

        roadMarkers.add(nodeMarker);
    }
}


class OnMarkerDragListenerDrawer implements Marker.OnMarkerDragListener {
    ArrayList<GeoPoint> mTrace;
    Polyline mPolyline;

    OnMarkerDragListenerDrawer() {
        mTrace = new ArrayList<GeoPoint>(100);
        mPolyline = new Polyline();
        mPolyline.setColor(0xAA0000FF);
        mPolyline.setWidth(2.0f);
        mPolyline.setGeodesic(true);
        map.getOverlays().add(mPolyline);
    }

    @Override public void onMarkerDrag(Marker marker) {
        //mTrace.add(marker.getPosition());
    }

    @Override public void onMarkerDragEnd(Marker marker) {
        mTrace.add(marker.getPosition());
        mPolyline.setPoints(mTrace);
        map.invalidate();
    }

    @Override public void onMarkerDragStart(Marker marker) {
        //mTrace.add(marker.getPosition());
    }
}

}

有谁知道我怎么能做到这一点?上面是我的代码片段。谢谢!

0 个答案:

没有答案