如何在osmbonuspack中为多边形添加标题

时间:2015-07-14 08:42:49

标签: openstreetmap osmdroid

如何在osmbonuspack中的Polygon上添加Marker.title(不在InfoWindow中)等标题?

private void showGeoJsonObjects(String str){
    KmlDocument kmlDocument = new KmlDocument();
    kmlDocument.parseGeoJSON(str);
    KmlFeature.Styler styler = new MyKmlStyler();
    FolderOverlay kmlOverlay = (FolderOverlay) kmlDocument.mKmlRoot.buildOverlay(mMapView, null, styler, kmlDocument);

    mMapView.getOverlays().add(kmlOverlay);
    mMapView.invalidate();
}


class MyKmlStyler implements KmlFeature.Styler {
    @Override
    public void onFeature(Overlay overlay, KmlFeature kmlFeature) {}

    @Override
    public void onPoint(Marker marker, KmlPlacemark kmlPlacemark, KmlPoint kmlPoint) {}

    @Override
    public void onLineString(Polyline polyline, KmlPlacemark kmlPlacemark, KmlLineString kmlLineString) {}

    @Override
    public void onPolygon(Polygon polygon, KmlPlacemark kmlPlacemark, KmlPolygon kmlPolygon) {
        try {
            String styleString = kmlPlacemark.getExtendedData("style");
            JSONObject o = new JSONObject(styleString);
            if(o.getBoolean("stroke")) {
                String colorHash = "#"+Integer.toHexString((int)(o.getDouble("opacity")*255))+o.getString("color").replace("#","");
                polygon.setStrokeColor(Color.parseColor(colorHash));
                polygon.setStrokeWidth((float) o.getDouble("weight"));
            }
            if(o.getBoolean("fill")){
                String colorHash = "#"+Integer.toHexString((int)(o.getDouble("fillOpacity")*255))+o.getString("color").replace("#","");
                polygon.setFillColor(Color.parseColor(colorHash));
            }
            //Would be great if this method helped me add title
            polygon.setTitle("Tadadada!!!");
        }catch (Exception e){}

    }
}

//Probably I should override Polygon, something like this 
class TitledPolygon extends Polygon {
    public TitledPolygon(Context ctx) {
        super(ctx);
    }

    public TitledPolygon(ResourceProxy resourceProxy) {
        super(resourceProxy);
    }

    @Override
    protected void draw(Canvas canvas, MapView mapView, boolean shadow) {
        super.draw(canvas, mapView, shadow);
        if(mTitle!=null && mTitle.length()>0){
            Paint textPaint = new Paint();
            textPaint.setColor( Color.RED);
            textPaint.setTextSize(40f);
            textPaint.setAntiAlias(true);
            textPaint.setTextAlign(Paint.Align.LEFT);
            Point p = new Point(100,100);
            canvas.drawText(mTitle, p.x, p.y + 20, textPaint);
        }
    }
}

我需要做很多额外的工作。覆盖另一个使用Polygon的类,以调用TitledPolygon。 可能找到多边形的最大高度,以在多边形上方添加标题。 也许它更容易解决?

1 个答案:

答案 0 :(得分:0)

我创造了更简单的方法。我添加了额外的Overlay,并在onPolygon中将OverlayItems添加到Overlay。