【问题标题】:How to draw a grid over google maps in android?如何在android中的谷歌地图上绘制网格?
【发布时间】:2018-09-16 10:28:37
【问题描述】:

我想像 What3Words 应用一样创建一个网格。

一旦相机缩放超过某个级别,网格就会显示出来,并随着用户缩放而按比例缩放。

我试过TileOverlay 并成功创建了一个网格。问题是网格在每次缩放时都会重新绘制。我希望网格而不是重绘,以缩放级别进行缩放。

然后我转到GroundOverlay 并使用已经绘制的网格,我发现了两个问题:图像质量比原始图像质量差,并且与TileOverlay 一样,它不会随着缩放而缩放。

然后我尝试使用多边形来绘制正方形,并使用(Map.getProjection().getVisibleRegion())提供的经度和纬度,由于地球是一个球体,因此不同区域的网格大小不一致。

现在我正在使用画布手动绘制它。

你们中的任何人都知道如何实现我想要做的事情吗?

提前致谢。

【问题讨论】:

  • 当相机以相同的缩放级别左右上下移动时应该发生什么 - 网格“绑定”到表面点,因此它也移动或者它是“静止的”。如果是后者,那么我有一个使用视图的建议......
  • 谢谢@Andy。不,我希望多边形随相机移动。它会居中,但它会随着相机移动,并通过获取当前可见区域的纬度和经度点,一直固定在地图的中心(而不是布局)。多边形是一个正方形,所以当我们靠近赤道时,例如,它变成了一个矩形,因为地球是一个球体;移动到北极或南极时也会发生同样的情况。我想在整个地图上保持相同的比例。不过,你能告诉我你对固定点的建议吗?
  • 查看三词地址后,我意识到静态网格不是您想要的;三词地址网格(3m x 3m 单元格)与建模表面(地球)对齐,并受屏幕上显示的地图墨卡托投影的影响。这是一个非常有趣的问题;我假设显示的网格单元应始终显示正确对齐的 3x3(或多个取决于缩放)正方形。我将离开静态视图,但为了好玩,我会尝试为这个测量系统创建适当的网格视图并替换。
  • 我能够得到一个正方形,稍后可以将其更改为网格,在屏幕中间使用纬度和经度点并在角落缩放它们以获得正方形。但是,问题是,当我靠近赤道时,正方形会慢慢压扁成一个矩形,我离赤道越近,它的高度就会越小,直到它几乎变成一条线。我知道这是由于地球是球形的;这就是我来这里寻找答案的原因。感谢您抽出宝贵的时间。我期待您的建议。
  • 发布了一个移动网格解决方案,它使用画布翻译与 w3w 参考网格对齐。

标签: java android google-maps google-api


【解决方案1】:

好的,这个答案演示了绘制和移动网格的图形更新,以及使用 w3w API 对齐网格的尝试。

所以使用 w3w 的一个问题似乎是如何计算 网格单元。由于该算法显然是私有的,因此对于此实现 'grid' rest api 调用用于当前屏幕中心点(空闲时),json 响应解析为候选参考点。

在此示例中,始终为“参考”网格单元绘制一个多边形 从 w3w 网格调用中获得。

网格视图实现使用 canvas.translate 调用来正确对齐 并使用从参考点计算的偏移量绘制网格。

由于使用 SphericalUtil 将距离映射到屏幕像素,这在任何纬度都有效。

在底部录制(低质量)。

主要活动

这里的 w3w 网格休息调用是在相机空闲并放大到足够远(没有必要在近距离缩放中保持重新对齐)和结果(附近网格单元的角点用作 参考点)被馈送到网格视图。绘制一个填充的多边形来表示 参考网格单元格。

在相机移动时,使用相同的参考点,但使用当前屏幕位置来保持适当的偏移。

public void what3words() {

    // some initial default position
    LatLng pt = new LatLng(38.547279, -121.461019);

    mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);

    // move map to a coordinate
    CameraUpdate cu = CameraUpdateFactory.newLatLng(pt);
    mMap.moveCamera(cu);
    cu = CameraUpdateFactory.zoomTo(14);
    mMap.moveCamera(cu);

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
        @Override
        public void onMapClick(LatLng latLng) {
            mMap.addCircle(new CircleOptions().radius(4).strokeColor(Color.BLUE).center(latLng));
        }
    });

    // while the camera is moving just move the grid (realign on idle)
    mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
        @Override
        public void onCameraMove() {
            ((GridView) findViewById(R.id.grid_any)).setAlignment(
                    null, mMap.getProjection(), mMap.getProjection().getVisibleRegion());
        }
    });

    // on idle fetch the grid using the screen center point
    mMap.setOnCameraIdleListener(new GoogleMap.OnCameraIdleListener() {
        @Override
        public void onCameraIdle() {
            Log.d(TAG,"idle");


            final LatLng centerOfGridCell = mMap.getCameraPosition().target;

            if (!gridSet || mMap.getCameraPosition().zoom < 10) {
                getGrid(centerOfGridCell, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d(TAG, "reqpt: " + centerOfGridCell + " volley response: " + response);
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONArray jsonArray = jsonObject.getJSONArray("lines");
                            JSONObject firstList = jsonArray.getJSONObject(1);
                            JSONObject firstPt = firstList.getJSONObject("start");
                            String lat = firstPt.getString("lat");
                            String lng = firstPt.getString("lng");
                            Log.d(TAG, "lat: " + lat + " lng: " + lng);

                            LatLng alignmentPt = new LatLng(Double.parseDouble(lat), Double.parseDouble(lng));
                            Projection p = mMap.getProjection();
                            VisibleRegion vr = p.getVisibleRegion();


                            ((GridView) findViewById(R.id.grid_any)).setAlignment(alignmentPt, p, vr);

                            if (polygon != null) {
                                polygon.remove();
                            }

                            // take alignment point and draw 3 meter square polygon
                            LatLng pt1 = SphericalUtil.computeOffset(alignmentPt, 3, 90);
                            LatLng pt2 = SphericalUtil.computeOffset(pt1, 3, 180);
                            LatLng pt3 = SphericalUtil.computeOffset(pt2, 3, 270);

                            polygon = mMap.addPolygon(new PolygonOptions().add(alignmentPt,
                                    pt1, pt2, pt3, alignmentPt)
                                    .strokeColor(Color.BLUE).strokeWidth(4).fillColor(Color.BLUE));
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                });

                gridSet = true;
            }


        }
    });
}


// Issue request to w3w - REMEMBER TO REPLACE **YOURKEY** ...
private void getGrid(LatLng pt, Response.Listener<String> listener) {

    // something 9 meters to east
    LatLng otherPt = SphericalUtil.computeOffset(pt, 6.0, 225);
    String bboxStr = Double.toString(pt.latitude)+","+
            Double.toString(pt.longitude)+","+
            Double.toString(otherPt.latitude)+","+
            Double.toString(otherPt.longitude);
    RequestQueue rq = Volley.newRequestQueue(this);
    String url = "https://api.what3words.com/v2/grid?bbox="+bboxStr+"&format=json&key=YOURKEY";

    Log.d(TAG,"url="+url);
    StringRequest req = new StringRequest(Request.Method.GET, url, listener, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "volley error: "+error);
        }
    });

    rq.add(req);
}

网格视图

网格视图扩展了 View 并且在地图布局中作为地图片段的兄弟。

有趣的部分是:

setAlignment - 这里 3 米的屏幕像素范围是使用 SphericalUtil 类。此屏幕像素尺寸代表 3 米范围 (在提供的参考位置)然后用于通过计算 x/y 偏移(然后在 onDraw 中使用)来对齐网格。请注意,这会使用“SphericalUtil.computeOffset”实用程序自动缩放网格,以测量向东 3 米的点,从而计算出相当于 3 米的屏幕像素。

onDraw - 这里使用 canvas 的 translate 方法从计算的偏移量开始重复网格形状(在 setAlignment 中)。

public class GridView extends View {

    private static final String TAG = GridView.class.getSimpleName();

    BitmapDrawable bm;
    Bitmap bitmap;

    GradientDrawable gd;
    int offsetX = 0;
    int offsetY = 0;
    private int width = 16;


    public GridView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public void setWidth(int w) {
        width = w;
        render();
        invalidate();
    }


    private void render() {
        setShape();
        if (gd != null) {
            bitmap = drawableToBitmap(gd);
            bm = new BitmapDrawable(getResources(), bitmap);
            bm.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
            bm.setBounds(0, 0, width, width);
        }
    }

    Point startPt;
    LatLng savedAlignmentPt;


    public void setAlignment(LatLng alignmentPt, Projection p, VisibleRegion vr) {

        if (alignmentPt == null) {
            alignmentPt = savedAlignmentPt;
        }

        if (alignmentPt == null) {
            return;
        }
        // the alignment point is the a corner of a grid square "near" the center of the screen
        savedAlignmentPt = alignmentPt;

        // compute how many screen pixels are in 3 meters using alignment point
        startPt =  p.toScreenLocation(alignmentPt);
        LatLng upperRight = SphericalUtil.computeOffset(alignmentPt, 3, 90);
        Point upperRtPt = p.toScreenLocation(upperRight);

        int pixelsOf3meters = upperRtPt.x - startPt.x;

        // don't draw grid if too small
        if (pixelsOf3meters < 16) {
            return;
        }

        setWidth(pixelsOf3meters);

        // startPt is screen location of alignment point
        offsetX = (pixelsOf3meters - (startPt.x % pixelsOf3meters));
        offsetY = (pixelsOf3meters - (startPt.y % pixelsOf3meters));

        invalidate();

    }

    private void setShape() {
        int left, right, top, bottom;
        gd = new GradientDrawable();
        gd.setShape(GradientDrawable.RECTANGLE);
        gd.setSize(width, width);
        gd.setStroke(2, Color.argb(0xff, 0xcc, 0x22, 0x22));

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        Rect rect = canvas.getClipBounds();


        final int cWidth = canvas.getWidth();
        final int cHeight = canvas.getHeight();

        if (bm == null) {
            return;
        }

        final Rect bmRect = bm.getBounds();
        if (bmRect.width() <= 8 || bmRect.height() <= 8) {
            return;
        }


        final int iterX = iterations(cWidth, -offsetX, bmRect.width());
        final int iterY = iterations(cHeight, -offsetY, bmRect.height());

        canvas.translate(-offsetX, -offsetY);

        for (int x = 0; x < iterX; x++) {
            for (int y = 0; y < iterY; y++) {
                bm.draw(canvas);
                canvas.translate(.0F, bmRect.height());
            }
            canvas.translate(bmRect.width(), -bmRect.height() * iterY);
        }
    }

    private static int iterations(int total, int start, int side) {
        final int diff = total - start;
        final int base = diff / side;
        return base + (diff % side > 0 ? 1 : 0);
    }

    public static Bitmap drawableToBitmap (Drawable drawable) {
        Bitmap bitmap = null;

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }
}

注意事项:


我正在与 w3w 网格 API 进行自己的战斗。当我计算返回列表中每个点的起点/终点之间的距离时,我得到 4.24264 米,很明显我没有得到任何东西。这是显示结果和屏幕截图的简单方法(白色=请求中使用的当前中心,任何其他颜色=列表中点对的开始-结束;端点具有黑色轮廓)。这里也很清楚使用哪个点来对齐网格。

不过有趣的是,一条“线”的起点似乎距离下一条线的起点 3 米(比较红色起点和蓝色起点):

代码:

 // plot each point as a circle
 for (int i = 0; i < jsonArray.length(); i++) {
     JSONObject startPt = jsonArray.getJSONObject(i).getJSONObject("start");
     JSONObject endPt = jsonArray.getJSONObject(i).getJSONObject("end");
     LatLng start = new LatLng(Double.parseDouble(startPt.getString("lat")), Double.parseDouble(startPt.getString("lng")));
     LatLng end = new LatLng(Double.parseDouble(endPt.getString("lat")), Double.parseDouble(endPt.getString("lng")));
     int c = colors[(i % colors.length)];
     mMap.addCircle(new CircleOptions().center(start).strokeColor(c).fillColor(c).radius(1));
     mMap.addCircle(new CircleOptions().center(end).strokeColor(Color.BLACK).fillColor(c).radius(1).strokeWidth(2));

     Log.d(TAG, "d  = "+SphericalUtil.computeDistanceBetween(start,end));
 }
 mMap.addCircle(new CircleOptions().center(centerOfGridCell).strokeColor(Color.WHITE).radius(1).strokeWidth(4));

【讨论】:

  • 你的答案正是我想要的。谢谢@Andy。我正在努力让它发挥作用。我已经解决了大多数错误,除了 `getGrid(centerOfGridCell, new Response.Listener()` 它在getGrid 和响应中的侦听器中给了我一个错误。你为这两个使用了哪些库?再次感谢。
  • @techchief 我使用“getGrid”方法更新了主活动代码部分;它只是使用 Volley 向 w3w 站点发出请求。 Response.Listener 是 android 的 volley 的一部分:developer.android.com/training/volley
  • 如果您对网格 api 返回的内容有很好的理解,请发布 - 使用额外的代码绘制圆圈以帮助查看要点。
  • 我仍在阅读 w3w api,但坦率地说,我想构建自己的东西。感谢您的帮助,我对我将要对多边形做什么有了很好的了解,这就是我问这个问题的主要原因。您认为我可以使用网格类来创建网格而不使用 w3w api吗?
【解决方案2】:

这是您可能已经尝试过的固定方法。总之,为每个网格大小(在本例中为小、中、大)创建形状;在主布局中创建视图,每个网格大小一个(所有可见性“消失”或一个可见);创建一个类来扩展 View 以便可以平铺形状(我不知道如何在 xml 中设置平铺形状);并且在此测试中,缩放侦听器在缩放 11(大)。

先录音,后码:

代码

形状

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle" >
    <size android:width="8dp" android:height="8dp" />
    <stroke android:width="1px" android:color="@color/gridLine" />
</shape>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"  >
    <size android:width="12dp" android:height="12dp" />
    <stroke android:width="1px" android:color="@color/gridLine" />
</shape>

<shape xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle" >
    <size android:width="16dp" android:height="16dp" />
    <stroke android:width="1px" android:color="@color/gridLine" />
</shape>

主布局(网格视图(最多只有一个是可见的)是地图的对等体并且在顶部)

<RelativeLayout android:layout_height="match_parent" android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="stuff.MapsActivity" />

<stuff.GridView
    android:id="@+id/grid_small"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/grid_square_small"
    android:visibility="visible"/>

    <stuff.GridView
        android:id="@+id/grid_medium"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/grid_square_medium"
        android:visibility="gone"/>

    <stuff.GridView
        android:id="@+id/grid_large"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/grid_square_large"
        android:visibility="gone"/>

</RelativeLayout>

GridView(这里没什么令人兴奋的;只是用来将形状设置为 REPEAT,我不知道如何在 xml 中执行)。

package stuff;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class GridView extends View {
    BitmapDrawable bm;

    public GridView(Context context, AttributeSet attrs) {
        super(context, attrs);

        Drawable d= getBackground();
        if (d != null) {
            Bitmap b = drawableToBitmap(d);
            bm = new BitmapDrawable(getResources(), b);
            bm.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
        }

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        bm.setBounds(canvas.getClipBounds());
        bm.draw(canvas);
    }

    public static Bitmap drawableToBitmap (Drawable drawable) {
        Bitmap bitmap = null;

        if (drawable instanceof BitmapDrawable) {
            BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
            if(bitmapDrawable.getBitmap() != null) {
                return bitmapDrawable.getBitmap();
            }
        }

        if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
            bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
        } else {
            bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        }

        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
        drawable.draw(canvas);
        return bitmap;
    }
}

Main Activity(地图)(只是摄像头监听器)

int lastZoom = -1;
public void gridTest() {
    mMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
        @Override
        public void onCameraMove() {
            int zoomInt = (int) mMap.getCameraPosition().zoom;
            if (lastZoom == -1) {
                lastZoom = zoomInt;
                return;
            }
            Log.d(TAG, "zoom= "+zoomInt);
            if (zoomInt < 10) {
                findViewById(R.id.grid_small).setVisibility(View.VISIBLE);
                findViewById(R.id.grid_medium).setVisibility(View.GONE);
                findViewById(R.id.grid_large).setVisibility(View.GONE);
            } else if (zoomInt < 11) {
                findViewById(R.id.grid_small).setVisibility(View.GONE);
                findViewById(R.id.grid_medium).setVisibility(View.VISIBLE);
                findViewById(R.id.grid_large).setVisibility(View.GONE);
            } else {
                findViewById(R.id.grid_small).setVisibility(View.GONE);
                findViewById(R.id.grid_medium).setVisibility(View.GONE);
                findViewById(R.id.grid_large).setVisibility(View.VISIBLE);
            }
            lastZoom = zoomInt;
        }
    });
}

注意事项:

  • drawableToBitmap 来自这里:How to convert a Drawable to a Bitmap?
  • 请注意,缩放控件已被遮挡,因此不太好。
  • 也许可以修改它以支持平移 - 在视图的 onDraw 方法中以偏移 x/y 中的网格。
  • 理论上,您可以使用 GridView.onDraw 中的setBounds 来支持平移,方法是根据平移运动调整“左”、“上”。调整范围为 [0, gridwidth)(因为它是一个正方形并且重复)。

【讨论】:

  • 非常感谢@Andy 抽出宝贵时间深入撰写本文。我很感激。但就像你在上一条评论中所说的那样,这不是我要找的。我尝试了一些方法,发现我可以通过以像素为单位获取正方形点并使用getProjection().fromScreenLocation(Point) 将它们转换为 LatLng 来创建一个完美的正方形,但是当我得到 what3words 网格的样子时我仍然在绕圈子.我正在接近答案,但我担心它不会是一个可靠的答案。当然,如果我找到它,我会分享它。再次感谢您。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多