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

3

我想在OSM上两点之间画一条线,但是我找不到任何有用的帮助。类似于Google地图中的Polyline。

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个回答

6

好的...只需使用osmdroid Polyline。

    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
我将这段代码放在`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。

如何删除折线上的最后一个点?在Polyline类中,我们有addpoint方法,但在osmdroid Polyline类中没有remove point方法。 - jesu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接