如何在OSM(开放地图街道)中的Android Studio中绘制一条线(折线)?

时间:2018-06-27 16:59:04

标签: android openstreetmap osmdroid

我想在OSM中在2点之间画一条线,但是我找不到任何对我有帮助的东西。类似于googlemap中的折线。

public class MainActivity extends Activity  {
    private MapView         mMapView;
    private MapController   mMapController;
    public TextView textView;
    public String longitude;
    public String latitude;
    public Drawable marker;
    private ItemizedIconOverlay<OverlayItem> anotherItemizedIconOverlay;
    ArrayList<OverlayItem> overlayItemArray;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.osm_main);
        mMapView = (MapView) findViewById(mapview);
        textView = (TextView) findViewById(R.id.textView);

        mMapView.setTileSource(TileSourceFactory.DEFAULT_TILE_SOURCE);
        mMapView.setBuiltInZoomControls(true);
        mMapController = (MapController) mMapView.getController();
        mMapController.setZoom(16);
        Double latE6 = (52.507621 )* 1E6;
        Double lngE6 = (13.407334 )* 1E6;
        GeoPoint gPt = new GeoPoint(latE6.intValue(), lngE6.intValue());
        mMapController.setCenter(gPt);
}
}

2 个答案:

答案 0 :(得分:3)

好吧...只需使用osmdroid折线即可。

    GeoPoint gPt0 = new GeoPoint(52.507621d, 13.407334d);
    GeoPoint gPt1 = new GeoPoint(52.527621d, 13.427334d);
    Polyline line = new Polyline(this);
    line .addPoint(gPt0);
    line .addPoint(gPt1);
    mMapView.getOverlays().add(line);

答案 1 :(得分:1)

我将此代码放在此行后的 onCreate 中:

  

mMapController.setCenter(gPt);

    GeoPoint gPt0 = new GeoPoint(52.507621d, 13.407334d);
    GeoPoint gPt1 = new GeoPoint(52.527621d, 13.427334d);
    PathOverlay myPath = new PathOverlay(Color.RED, this);
    myPath.addPoint(gPt0);
    myPath.addPoint(gPt1);
    mMapView.getOverlays().add(myPath);

注意:点之后请使用d。