谷歌地图Android版的多边形点 [英] Google Maps Android point in polygon

查看:165
本文介绍了谷歌地图Android版的多边形点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个使用谷歌地图API的android一个Android应用程序。结果
这个应用程序需要有一个对点击多边形的功能,但因为它不适用于谷歌地图Android的API V2。我做了一些研究,发现一个变通此功能。结果
我结束了同是检查点是否在多边形的方法。结果
我使用这个。而且它的工作pretty好,如果你手工创建的界限。结果
如果可以创建能够解决我的问题一个循环的界限。结果
我从数据库中获取的多边形点。

I creating a android app that uses google maps android api.
This app need to to have a on click polygon function, but since it not available on google maps android api v2. I did some research and found a work around for this function.
What i ended up with is to check if a point is in a polygon method.
I'm using this library. And it work pretty well if you create the bounds manually.
If can create the bounds in a loop that would solve my problem.
I get the polygon points from my database.

下面是我的MapFragment code:

Here is my MapFragment code:

public class MapBlocksMapView extends Fragment {

    protected GoogleMap googleMap;
    protected LatLng latLng;
    protected Intent intent;
    protected String color, crops, block_code, tmp_user;

    public MapBlocksMapView() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        ActionBar actionBar = getActivity().getActionBar();
        actionBar.setTitle("Block View");
        View rootView = inflater.inflate(R.layout.fragment_blocksmapview, container, false);

        if (googleMap== null) {
            googleMap= ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
        }

        Bundle bundle = getArguments();
        if (bundle != null) {
            block_code = bundle.getString("block_code");
            tmp_user = bundle.getString("user");
        }

        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                checkpoint(latLng.latitude, latLng.longitude);
            }
        });
        return rootView;

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState){
        getdata(getActivity(), block_code);
        super.onActivityCreated(savedInstanceState);
    }

    private void checkpoint(double latitude, double longitude) {

        System.out.println(latitude +"," +longitude);
    }

    public void getdata(Context ctx, String block_code) {

        SQLHelper dbhelper = new SQLHelper(ctx);
        dbhelper.getReadableDatabase();

        JSONArray jsonArray = dbhelper.getSingleBlocks(block_code);

        try {
            for (int a = 0; a < jsonArray.length(); a++) {

                JSONObject jsonObject = new JSONObject(String.valueOf(jsonArray.getJSONObject(a)));
                JSONArray jsonArray1 = jsonObject.getJSONArray("b_points");
                if (jsonObject.getJSONArray("b_points").length() > 0) {
                    color = jsonObject.getString("b_color");
                    crops = jsonObject.getString("b_crop");
                    PolygonOptions rectOptions = new PolygonOptions();

                    for (int b = 0; b < jsonArray1.length(); b++) {
                        JSONArray jsonArray2 = jsonArray1.getJSONArray(b);

                        rectOptions.add(new LatLng(jsonArray2.getDouble(0), jsonArray2.getDouble(1)));


                        System.out.println(jsonArray2.get(0) + " / " + jsonArray2.get(1));

                    }
                    latLng = new LatLng(jsonArray1.getJSONArray(0).getDouble(0), jsonArray1.getJSONArray(0).getDouble(1));
                    rectOptions.strokeWidth(1).strokeColor(Color.parseColor(color)).fillColor(Color.parseColor(color));
                    googleMap.addPolygon(rectOptions);
                    CameraUpdate cameraPosition = CameraUpdateFactory.newLatLngZoom(latLng, 17);
                    googleMap.animateCamera(cameraPosition);

                } else {

                    Toast.makeText(getActivity(), "Error with the selected block", Toast.LENGTH_LONG).show();
                    closeFragment();
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        SupportMapFragment f = (SupportMapFragment) getFragmentManager()
                .findFragmentById(R.id.map);
        if (f != null)
            getFragmentManager().beginTransaction().remove(f).commit();
    }

    private void closeFragment() {
        getActivity().getFragmentManager().popBackStack();
    }

}

下面是从图书馆的例子code:

Here is the example code from the library:

Polygon polygon = Polygon.Builder()
    .addVertex(new Point(1, 3))
    .addVertex(new Point(2, 8))
    .addVertex(new Point(5, 4))
    .addVertex(new Point(5, 9))
    .addVertex(new Point(7, 5))
    .addVertex(new Point(6, 1))
    .addVertex(new Point(3, 1))
    .build();

Point point = new Point(4.5f, 7);
boolean contains = polygon.contains(point);

如果一些有其他的解决方案或建议将是有益的。结果
谢谢

If some have a other solution or suggestions would be helpful.
Thanks

推荐答案

我管理的一些周围挖后解决了这个发现在GitHub上这个code。结果
我使用这个现在,它就像一个魅力。

I manage to solve this after some digging around and found this code on github.
I'm using this library now and it works like a charm.

我的多边形点存储像这样在我的数据库字符串。

My polygon points are stored like this in my database as string.

[[3.65E-4,-1.1E-5],[-5.0E-6,3.54E-4],[-3.0E-6,-1.1E-4]]

下面是一个例子code

Here is a example code

protected double[] points;

public void getdata(){

    SQLHelper dbhelper = new SQLHelper(ctx);
    dbhelper.getReadableDatabase();

    String[] ls =dbhelper.getSingleBlocksPointsArray(block_code)
                .replaceAll("\\[", "")
                .replaceAll("\\]","")
                .split(",");
    points = new double[ls.length];
    for(int i=0;i<ls.length;i++)
    {

        points[i] = Double.parseDouble(ls[i]);
    }
}

在onCreateView我用这个:

in onCreateView i use this:

googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
    @Override
    public void onMapClick(LatLng latLng) {
        if(contains(new Coordinate(latLng.latitude,latLng.longitude), points)) {
            Toast.makeText(getActivity(),"True",Toast.LENGTH_SHORT).show();
        }else {
            Toast.makeText(getActivity(),"False",Toast.LENGTH_SHORT).show();
        }
    }
});

这篇关于谷歌地图Android版的多边形点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆