Java 在android中如何在谷歌地图上绘制网格?

Java 在android中如何在谷歌地图上绘制网格?,java,android,google-maps,google-api,Java,Android,Google Maps,Google Api,我想创建一个类似于3字应用程序的网格。 一旦相机缩放到某个级别,网格就会显示出来,并随着用户的缩放进行缩放 我尝试了TileOverlay,并成功创建了一个网格。问题是网格在每次缩放时都会重新绘制自身。我希望网格不是重画,而是按照缩放级别缩放 然后我转到GroundOverlay并使用已经绘制的网格,我发现了两个问题:图像质量比原始图像差,并且像TileOverlay一样,它不随缩放而缩放 然后我尝试使用多边形绘制正方形,并使用(Map.getProjection().getVisibleRe

我想创建一个类似于3字应用程序的网格。

一旦相机缩放到某个级别,网格就会显示出来,并随着用户的缩放进行缩放

我尝试了
TileOverlay
,并成功创建了一个网格。问题是网格在每次缩放时都会重新绘制自身。我希望网格不是重画,而是按照缩放级别缩放

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

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

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

你们有谁知道如何实现我的目标吗


提前感谢。

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

首先是录音,然后是代码:

代码

形状


主布局(栅格视图(最多只有一个是可见的)与地图对等,并且位于上方)


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

包装材料;
导入android.content.Context;
导入android.graphics.Bitmap;
导入android.graphics.Canvas;
导入android.graphics.Shader;
导入android.graphics.drawable.BitmapDrawable;
导入android.graphics.drawable.drawable;
导入android.util.AttributeSet;
导入android.view.view;
公共类GridView扩展了视图{
位图可绘制bm;
公共网格视图(上下文、属性集属性){
超级(上下文,attrs);
Drawable d=getBackground();
如果(d!=null){
位图b=drawableToBitmap(d);
bm=新的位图可绘制(getResources(),b);
bm.setTileModeXY(Shader.TileMode.REPEAT,Shader.TileMode.REPEAT);
}
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
bm.setBounds(canvas.getClipBounds());
绘画(画布);
}
公共静态位图drawableToBitmap(可绘制){
位图=空;
if(BitmapDrawable的可绘制实例){
BitmapDrawable BitmapDrawable=(BitmapDrawable)drawable;
if(bitmapDrawable.getBitmap()!=null){
返回bitmapDrawable.getBitmap();
}
}

如果(drawable.getIntrinsicWidth()确定,则此答案将演示绘制和移动网格的图形更新,以及使用w3w API对齐网格的尝试

因此,使用w3w的一个问题似乎是如何计算 由于算法显然是私有的,因此对于这个实现 “grid”restapi调用用于当前屏幕中心点(空闲时)和为候选参考点解析的json响应

在本例中,始终为“参考”网格单元绘制多边形 从w3w网格调用中获取

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

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

底部录音(低质量)

主要活动

在这里,w3w grid rest调用是在摄影机空闲时进行的,并将其放大到足够远的距离(无需在近距离缩放时保持重新对齐)和结果(附近网格单元的一个角点用作 参考点)输入栅格视图。绘制填充多边形以表示 参考网格单元

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

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);
}
public void what3words(){
//一些初始默认位置
LatLng pt=新LatLng(38.547279,-121.461019);
mMap.setMapType(GoogleMap.MAP_TYPE_卫星);
//将地图移动到坐标
CameraUpdate cu=CameraUpdateFactory.newLatLng(pt);
移动摄像机(cu);
cu=CameraUpdateFactory.zoomTo(14);
移动摄像机(cu);
mMap.setOnMapClickListener(新的GoogleMap.OnMapClickListener(){
@凌驾
公共无效的点击(LatLng LatLng){
mMap.addCircle(新的CircleOptions().radius(4)、strokeColor(Color.BLUE)、center(latLng));
}
});
//相机移动时,只需移动网格(空闲时重新对齐)
mMap.setOnCameraMoveListener(新的GoogleMap.OnCameraMoveListener(){
@凌驾
关于CameraMove()的公共无效{
((GridView)findViewById(R.id.grid_any)).setAlignment(
null,mMap.getProjection(),mMap.getProjection().getVisibleRegion();
}
});
//空闲时,使用屏幕中心点提取网格
mMap.setonCameraideListener(新的GoogleMap.onCameraideListener(){
@凌驾
公共无效OnCameradile(){
Log.d(标记为“空闲”);
最终LatLng centerOfGridCell=mMap.getCameraPosition().target;
如果(!gridSet | | mMap.getCameraPosition().zoom<10){
getGrid(centerOfGridCell,new Response.Listener()){
@凌驾
公众在
<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>
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;
    }
}
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;
        }
    });
}
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);
}
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;
    }
}
 // 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));