在 Google Maps v2 上将地图标记移动一定数量的像素

Oce*_*ife 2 android canvas google-maps-api-2

我已将可绘制形状的自定义地图标记添加到我的 Google 地图。这些标记是各种颜色的点。当用户点击其中一个标记时,会从源位置到所选标记绘制一条折线;参见图1 ;

]

图 1:我当前的地图

该线直接绘制到由彩色圆点标记的坐标。然而,正如紫色点清楚地表明,标记绘制在顶部 - 我宁愿折线与圆的中心相交,这样从每个角度看的线看起来更像这样;

/] http://milspace.viecorefsd.com/~nlehman/example.png

图 2:所需的折线与圆的交点

为了实现这一点,我试图通过点半径平移支持可绘制的画布。这是我的尝试;

    // Circular marker.
    final int px = getResources().getDimensionPixelSize(dimen.map_dot_marker_size);
    final Bitmap mDotMarkerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(mDotMarkerBitmap);
    final Drawable shape = getResources().getDrawable(drawable.purple_map_dot);
    shape.setBounds(0, 0, px, px);
    canvas.translate(0, px/2);
    shape.draw(canvas);
    final MarkerOptions options = new MarkerOptions();
    options.position(spot.getNearestCityLatLng());
    options.icon(BitmapDescriptorFactory.fromBitmap(mDotMarkerBitmap));
    options.title(spot.getNearestCity());
    lastLocationSelectionMarker.addMarker(options);
Run Code Online (Sandbox Code Playgroud)

这段代码确实移动了可绘制对象,但支持画布的大小保持不变,这意味着圆圈被切成两半,另一半不可见。

社区能否建议如何最好地实现我在图 2 中所追求的效果,并将标记中心直接放在它所标记的坐标上?

Pav*_*dka 5

你必须使用anchor当您创建属性Marker。默认情况下,anchor 设置为 0.5f,1f,它指向标记的中心水平和底部垂直部分。对于您的标记类型,我假设您需要使用[0.5f,0.5f]锚点(请参阅文档

所以你的代码看起来像:

// Circular marker.
final MarkerOptions options = new MarkerOptions();
options.position(spot.getNearestCityLatLng());
options.icon(BitmapDescriptorFactory.fromResource(R.drawable.purple_map_dot));
options.title(spot.getNearestCity());
options.anchor(0.5f, 0.5f);
lastLocationSelectionMarker.addMarker(options);
Run Code Online (Sandbox Code Playgroud)