简体   繁体   English

如何使用Mapsforge在运行时中正确添加可绘制对象

[英]How to add drawable in runtime properly using Mapsforge

I could get Mapsforge lib 0.30 working on Android and I'm really satisfied with it. 我可以在Android上使用Mapsforge lib 0.30,并且对此感到非常满意。 Problem I have is I can't add a custom drawable in a proper way. 我的问题是我无法以适当的方式添加自定义绘图。 When I add a drawable to be drawn, the drawable doesn't show up in the right position, but it shows in the left top corner of the screen. 当我添加要绘制的可绘制对象时,该可绘制对象不会显示在正确的位置,但是会显示在屏幕的左上角。 This is an example of my test code: 这是我的测试代码的示例:

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);

So can anyone please help me to do it the right way? 所以有人可以帮助我以正确的方式做吗? In short, I want to create a drawable in runtime and put it on the right position. 简而言之,我想在运行时中创建一个drawable并将其放置在正确的位置。 Just adding a drawable from resource is not a problem, that is working good, but from runtime, the drawable is not on the right position. 仅从资源添加可绘制对象不是问题,这很好,但是从运行时开始,可绘制对象位置不正确。 Thank you. 谢谢。

You need to call Marker.boundCenter(...) on it. 您需要在其上调用Marker.boundCenter(...)。

I use a line like: 我用这样的一行:

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

I'm working from Trunk (looks like 0.3.1-snapshot to Maven). 我正在Trunk(看起来像0.3.1-snapshot到Maven)。

If that method is missing from your version of Mapsforge then the full method is: 如果您的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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM