【问题标题】:How to add drawable in runtime properly using Mapsforge如何使用 Mapsforge 在运行时正确添加可绘制对象
【发布时间】:2012-08-30 07:42:04
【问题描述】:

我可以让 Mapsforge lib 0.30 在 Android 上运行,我对此非常满意。 我遇到的问题是我无法以正确的方式添加自定义可绘制对象。 当我添加要绘制的drawable时,drawable不会显示在正确的位置,而是显示在屏幕的左上角。 这是我的测试代码示例:

MapView mapView = new MapView(this);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setEnabled(true);
mapView.setMapFile(new File("/sdcard/remob/netherlands.map"));

setContentView(mapView);

// Creating my own custom drawable
Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(60, 60, conf);
Drawable drawable = new BitmapDrawable(getResources(), bmp) {
@Override
public void draw(Canvas canvas) {
    super.draw(canvas);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.BLUE);
    paint.setStyle(Style.FILL);

    canvas.drawCircle(30, 30, 15, paint);
}
};

// create an ItemizedOverlay with the default marker
ArrayItemizedOverlay itemizedOverlay = new ArrayItemizedOverlay(drawable);

// create a GeoPoint with the latitude and longitude coordinates
GeoPoint geoPoint = new GeoPoint(51.92434, 4.47769);

// create an OverlayItem with title and description
OverlayItem item = new OverlayItem(geoPoint, "MyPoint", "This is my own point.");
item.setMarker(ItemizedOverlay.boundCenter(drawable));

// add the OverlayItem to the ArrayItemizedOverlay
itemizedOverlay.addItem(item);

// add the ArrayItemizedOverlay to the MapView
mapView.getOverlays().add(itemizedOverlay);

那么任何人都可以帮助我以正确的方式做到这一点吗? 简而言之,我想在运行时创建一个可绘制对象并将其放在正确的位置。 只是从资源中添加一个可绘制对象不是问题,这很好,但是从运行时开始,可绘制对象不在正确的位置。 谢谢。

【问题讨论】:

    标签: android bitmap maps drawable


    【解决方案1】:

    你需要调用 Marker.boundCenter(...) 。

    我使用这样的一行:

    ef_icon = Marker.boundCenter(getResources().getDrawable(R.drawable.ef_icon));
    

    我正在使用 Trunk(看起来像 0.3.1-snapshot 到 Maven)。

    如果您的 Mapsforge 版本中缺少该方法,那么完整的方法是:

    public static Drawable boundCenter(Drawable drawable) {
        int intrinsicWidth = drawable.getIntrinsicWidth();
        int intrinsicHeight = drawable.getIntrinsicHeight();
        drawable.setBounds(intrinsicWidth / -2, intrinsicHeight / -2, intrinsicWidth / 2, intrinsicHeight / 2);
        return drawable;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-16
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 1970-01-01
      相关资源
      最近更新 更多