jjzjj

android - Google Maps API v2 在 MapFragment 上绘制部分圆圈

coder 2023-11-28 原文

我需要画这样的东西,它会被涂上颜色,透明度很低

它还需要可点击(onTouch 事件等)

我知道在 API v1 中您必须使用 Overlay 并使用 Canvas 和一些数学来扩展它。 在 Google Map API v2 中最简单的方法是什么?

PS:半径是可变的。

(进一步引用) 编辑 1:

我实现了 CanvasTileProvider 子类并覆盖了它的 onDraw() 方法:

@Override
void onDraw(Canvas canvas, TileProjection projection) {
    // TODO Auto-generated method stub

    LatLng tempLocation = moveByDistance(mSegmentLocation, mSegmentRadius, mSegmentAngle);

    DoublePoint segmentLocationPoint = new DoublePoint(0, 0);
    DoublePoint tempLocationPoint = new DoublePoint(0, 0);

    projection.latLngToPoint(mSegmentLocation, segmentLocationPoint);
    projection.latLngToPoint(tempLocationPoint, tempLocationPoint);

    float radiusInPoints = FloatMath.sqrt((float) (Math.pow(
            (segmentLocationPoint.x - tempLocationPoint.x), 2) + Math.pow(
            (segmentLocationPoint.y - tempLocationPoint.y), 2)));

    RectF segmentArea = new RectF();
    segmentArea.set((float)segmentLocationPoint.x - radiusInPoints, (float)segmentLocationPoint.y - radiusInPoints, 
            (float)segmentLocationPoint.x + radiusInPoints, (float)segmentLocationPoint.y + radiusInPoints);

    canvas.drawArc(segmentArea, getAdjustedAngle(mSegmentAngle), 
            getAdjustedAngle(mSegmentAngle + 60), true, getOuterCirclePaint());


}

另外,我从 MapActivity 添加了这个:

private void loadSegmentTiles() {

     TileProvider tileProvider; 
     TileOverlay tileOverlay = mMap.addTileOverlay(
         new TileOverlayOptions().tileProvider(new SegmentTileProvider(new LatLng(45.00000,15.000000), 250, 30)));

}

现在我想知道为什么我的弧线不在 map 上?

最佳答案

为了绘制圆段,如果圆段主要是静态的,我会注册一个 TileProvider。 (图 block 通常只加载一次,然后缓存。)为了检查点击事件,您可以注册一个 onMapClickListener 并遍历您的段以检查单击的 LatLng 是否在您的段之一内。 (有关更多详细信息,请参见下文。)

这是一个 TileProvider 示例,您可以将其子类化并仅实现 onDraw 方法。
一个重要提示:子类必须是线程安全的! onDraw 方法会被多个线程同时调用。因此,请避免在 onDraw 中更改任何全局变量!

/* imports should be obvious */ 
public abstract class CanvasTileProvider implements TileProvider {
private static int TILE_SIZE = 256;

private BitMapThreadLocal tlBitmap;

@SuppressWarnings("unused")
private static final String TAG = CanvasTileProvider.class.getSimpleName();

public CanvasTileProvider() {
    super();
    tlBitmap = new BitMapThreadLocal();
}

@Override
// Warning: Must be threadsafe. To still avoid creation of lot of bitmaps,
// I use a subclass of ThreadLocal !!!
public Tile getTile(int x, int y, int zoom) {
    TileProjection projection = new TileProjection(TILE_SIZE,
            x, y, zoom);

    byte[] data;
    Bitmap image = getNewBitmap();
    Canvas canvas = new Canvas(image);
    onDraw(canvas, projection);
    data = bitmapToByteArray(image);
    Tile tile = new Tile(TILE_SIZE, TILE_SIZE, data);
    return tile;
}

/** Must be implemented by a concrete TileProvider */
abstract void onDraw(Canvas canvas, TileProjection projection);

/**
 * Get an empty bitmap, which may however be reused from a previous call in
 * the same thread.
 * 
 * @return
 */
private Bitmap getNewBitmap() {
    Bitmap bitmap = tlBitmap.get();
    // Clear the previous bitmap
    bitmap.eraseColor(Color.TRANSPARENT);
    return bitmap;
}

private static byte[] bitmapToByteArray(Bitmap bm) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
    byte[] data = bos.toByteArray();
    return data;
}

class BitMapThreadLocal extends ThreadLocal<Bitmap> {
    @Override
    protected Bitmap initialValue() {
        Bitmap image = Bitmap.createBitmap(TILE_SIZE, TILE_SIZE,
                Config.ARGB_8888);
        return image;
    }
}
}

使用传递给 onDraw 方法的投影首先获取图 block 的边界。如果没有段在边界内,则返回。否则将您的 fragment 绘制到 Canvas 中。 projection.latLngToPoint 方法可帮助您将 LatLng 转换为 Canvas 的像素。

/** Converts between LatLng coordinates and the pixels inside a tile. */
public class TileProjection {

private int x;
private int y;
private int zoom;
private int TILE_SIZE;

private DoublePoint pixelOrigin_;
private double pixelsPerLonDegree_;
private double pixelsPerLonRadian_;

TileProjection(int tileSize, int x, int y, int zoom) {
    this.TILE_SIZE = tileSize;
    this.x = x;
    this.y = y;
    this.zoom = zoom;
    pixelOrigin_ = new DoublePoint(TILE_SIZE / 2, TILE_SIZE / 2);
    pixelsPerLonDegree_ = TILE_SIZE / 360d;
    pixelsPerLonRadian_ = TILE_SIZE / (2 * Math.PI);
}

/** Get the dimensions of the Tile in LatLng coordinates */
public LatLngBounds getTileBounds() {
    DoublePoint tileSW = new DoublePoint(x * TILE_SIZE, (y + 1) * TILE_SIZE);
    DoublePoint worldSW = pixelToWorldCoordinates(tileSW);
    LatLng SW = worldCoordToLatLng(worldSW);
    DoublePoint tileNE = new DoublePoint((x + 1) * TILE_SIZE, y * TILE_SIZE);
    DoublePoint worldNE = pixelToWorldCoordinates(tileNE);
    LatLng NE = worldCoordToLatLng(worldNE);
    return new LatLngBounds(SW, NE);
}

/**
 * Calculate the pixel coordinates inside a tile, relative to the left upper
 * corner (origin) of the tile.
 */
public void latLngToPoint(LatLng latLng, DoublePoint result) {
    latLngToWorldCoordinates(latLng, result);
    worldToPixelCoordinates(result, result);
    result.x -= x * TILE_SIZE;
    result.y -= y * TILE_SIZE;
}


private DoublePoint pixelToWorldCoordinates(DoublePoint pixelCoord) {
    int numTiles = 1 << zoom;
    DoublePoint worldCoordinate = new DoublePoint(pixelCoord.x / numTiles,
            pixelCoord.y / numTiles);
    return worldCoordinate;
}

/**
 * Transform the world coordinates into pixel-coordinates relative to the
 * whole tile-area. (i.e. the coordinate system that spans all tiles.)
 * 
 * 
 * Takes the resulting point as parameter, to avoid creation of new objects.
 */
private void worldToPixelCoordinates(DoublePoint worldCoord, DoublePoint result) {
    int numTiles = 1 << zoom;
    result.x = worldCoord.x * numTiles;
    result.y = worldCoord.y * numTiles;
}

private LatLng worldCoordToLatLng(DoublePoint worldCoordinate) {
    DoublePoint origin = pixelOrigin_;
    double lng = (worldCoordinate.x - origin.x) / pixelsPerLonDegree_;
    double latRadians = (worldCoordinate.y - origin.y)
            / -pixelsPerLonRadian_;
    double lat = Math.toDegrees(2 * Math.atan(Math.exp(latRadians))
            - Math.PI / 2);
    return new LatLng(lat, lng);
}

/**
 * Get the coordinates in a system describing the whole globe in a
 * coordinate range from 0 to TILE_SIZE (type double).
 * 
 * Takes the resulting point as parameter, to avoid creation of new objects.
 */
private void latLngToWorldCoordinates(LatLng latLng, DoublePoint result) {
    DoublePoint origin = pixelOrigin_;

    result.x = origin.x + latLng.longitude * pixelsPerLonDegree_;

    // Truncating to 0.9999 effectively limits latitude to 89.189. This is
    // about a third of a tile past the edge of the world tile.
    double siny = bound(Math.sin(Math.toRadians(latLng.latitude)), -0.9999,
            0.9999);
    result.y = origin.y + 0.5 * Math.log((1 + siny) / (1 - siny))
            * -pixelsPerLonRadian_;
};

/** Return value reduced to min and max if outside one of these bounds. */
private double bound(double value, double min, double max) {
    value = Math.max(value, min);
    value = Math.min(value, max);
    return value;
}

/** A Point in an x/y coordinate system with coordinates of type double */
public static class DoublePoint {
    double x;
    double y;

    public DoublePoint(double x, double y) {
        this.x = x;
        this.y = y;
    }
}

}

最后,您需要检查一下 LatLng 坐标上的点击是否在您的段内。 因此,我会通过 LatLng 坐标列表来近似该段,在您的情况下,一个简单的三角形可能就足够了。对于每个 LatLng 坐标列表,即对于每个段,您可以调用如下内容:

private static boolean isPointInsidePolygon(List<LatLng> vertices, LatLng point) {
    /**
     * Test is based on a horizontal ray, starting from point to the right.
     * If the ray is crossed by an even number of polygon-sides, the point
     * is inside the polygon, otherwise it is outside.
     */
    int i, j;
    boolean inside = false;
    int size = vertices.size();
    for (i = 0, j = size - 1; i < size; j = i++) {
        LatLng vi = vertices.get(i);
        LatLng vj = vertices.get(j);
        if ((vi.latitude > point.latitude) != (vj.latitude > point.latitude)) {
            /* The polygonside crosses the horizontal level of the ray. */
            if (point.longitude <= vi.longitude
                    && point.longitude <= vj.longitude) {
                /*
                 * Start and end of the side is right to the point. Side
                 * crosses the ray.
                 */
                inside = !inside;
            } else if (point.longitude >= vi.longitude
                    && point.longitude >= vj.longitude) {
                /*
                 * Start and end of the side is left of the point. No
                 * crossing of the ray.
                 */
            } else {
                double crossingLongitude = (vj.longitude - vi.longitude)
                        * (point.latitude - vi.latitude)
                        / (vj.latitude - vi.latitude) + vi.longitude;
                if (point.longitude < crossingLongitude) {
                    inside = !inside;
                }
            }
        }
    }
    return inside;
}

如您所见,我有一个非常相似的任务要解决 :-)

关于android - Google Maps API v2 在 MapFragment 上绘制部分圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20382823/

有关android - Google Maps API v2 在 MapFragment 上绘制部分圆圈的更多相关文章

  1. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  2. ruby - 使用 RMagick 从图像中切出圆圈 - 2

    我想使用rmagick从图像中剪出一个圆圈。这是我希望能够完成的示例:-->好像我想用http://studio.imagemagick.org/RMagick/doc/draw.html#circle切一个圆,然后clip_path来掩盖它,但文档不是很清楚。谁能给我指出正确的方向? 最佳答案 require'rmagick'im=Magick::Image.read('walter.jpg').firstcircle=Magick::Image.new200,200gc=Magick::Draw.newgc.fill'black

  3. ruby - 如何使用部分字符串搜索数组并返回索引? - 2

    我想使用部分字符串搜索数组,然后获取找到该字符串的索引。例如:a=["Thisisline1","Wehaveline2here","andfinallyline3","potato"]a.index("potato")#thisreturns3a.index("Wehave")#thisreturnsnil使用a.grep将返回完整的字符串,使用a.any?将返回正确的true/false语句,但都不会返回匹配的索引找到了,或者至少我不知道该怎么做。我正在编写一段代码,该代码读取文件、查找特定header,然后返回该header的索引,以便它可以将其用作future搜索的偏移量。如果

  4. ruby-on-rails - 如何将数据传递给部分? - 2

    K伙计们,所以我创建了这个赞成/反对的投票脚本(基本上就像stackoverflow上的那个),我试图向其中添加一些Ajax,这样页面就不会在您每次投票时都重新加载。我有两个Controller,一个叫grinder,一个叫votes。(磨床基本都是帖子)所以这是所有研磨机的索引(看起来像这样)这是该页面的代码。Listinggrinders"grinders/grinders")%>这就是我在views/grinders/_grinders.erb中的内容true)do|u|%>grinder.id%>"up"%>'create')%>true)do|d|%>grinder.id%>

  5. ruby-on-rails - 将 restclient 与多部分帖子一起使用 - 2

    我将restclient用于多部分表单,以将数据发送到restfulweb服务(它是Panda视频编码服务)。不过,诀窍在于我传递给restclient(Technoweenie分支)的文件来自用户提交的我自己的表单。那么,让我们来看看这个。用户将文件发布到我的Rails应用程序。在我的Controller中,它从params[:file]接收文件。然后我想使用RestClient将params[:file]传递给Panda。我在Panda服务器上遇到的错误如下。我注意到堆栈跟踪中的文件参数也在一个字符串中(我假设Panda将其转换为字符串以获得更好的堆栈跟踪)。~Startedreq

  6. ruby-on-rails - 独立测试 Rails 部分 View - 2

    我在标准rails2.1项目中使用Test/Unit。我希望能够独立于任何特定的Controller/操作来测试分部View。好像ZenTest'sTest::Rails::ViewTestCase会有所帮助,但我无法让它工作,与view_testhttp://www.continuousthinking.com/tags/view_test类似Google出现的大部分内容似乎都已经过时了,所以我猜它并不真正适用于Rails2.1非常感谢任何帮助。谢谢,罗兰 最佳答案 我们正在使用RSpec在我们的Rails2.1项目中,我们可以做

  7. ruby - 如何在 ruby​​ 中实现 curry(部分函数) - 2

    我需要一些在ruby​​(1.8.6或1.8.7而不是1.9)中实现curry函数的示例。 最佳答案 下面是如何用block而不是方法来柯里化(Currying):defcurry(&block)arity=(block.arity>=0)?block.arity:-(block.arity+1)#returnanimmediatevalueiftheblockhasonereturnblock[]ifarity==0#otherwise,curryitargumentbyargumentargs=[]innermost=lambd

  8. ruby-on-rails - Rails 4 通过渲染部分传递多个变量 - 2

    这个问题已经被问过很多次了,但我无法让它工作。我想像这样将多个变量传递给我的部分...这是部分material_fields.html.erb中的一行,我希望f.select预先填充Yes选项或“true”值。(有些情况下我希望它是假的)f可用并且有效,而feed不可用......我不知道为什么这不起作用。我在select语句之外尝试了,但它仍然不起作用。在这两种情况下,我都得到未定义的局部变量或方法“feed”。有人知道我的语法有什么问题吗? 最佳答案 我想通了是什么问题。我有后来我在同一个View中显然,当从一个文件渲染相同的部

  9. ruby-on-rails - 是否可以在部分中只放置一个 rails 表单元素? - 2

    我的应用有一个选择框供用户选择“地点”。如您所料,此选择框位于一个表单中。我还在页面上的某处执行了一个操作,该操作通过AJAX创建了一个新场所。创建新field后,我想更新field选择框以反射(reflect)这一点。我的解决方案是将选择框放在局部中,并从Controller中的创建操作中呈现局部。'venue/venue_select_box'%>局部看起来像这样:'Selectavenue'%>其中f是表单引用:问题是f在部分中未定义,所以我得到一个错误。一种解决方案是包括整个表格,但我觉得没有必要这样做,因为我没有更新整个表格。关于如何解决这个问题有什么想法吗?

  10. ruby-on-rails - 在这部分代码中设置 klass = self 的动机是什么 - 2

    我正在查看讨论中的一些代码并偶然发现了这个并且想知道为什么klass=self.据我所知,他们是比我更好的ruby​​开发人员,这一定是有充分理由的。他们为什么不调用self.remove_from_cache!(message["key"],false)?该block是否正在创建一个新范围,其中self引用MessageBus类?是否有其他示例说明您需要在Ruby中创建此类构造,或者这是主要示例?如果MessageBus.subscribe是MessageBus的一个实例(比如说m_bus.subscribe)会自己引用block中的m_bus吗?ensure_class_liste

随机推荐