Android的地理围栏(多边形) [英] Android Geofencing (Polygon)

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

问题描述

如何从多个地理位置(长,纬度值)创建多边形地理栅栏。还有如何跟踪用户进入到这个区域范围设定的区域或该区域在Android上退出。

How to create Polygon Geofence from multiple geo locations(long,lat values) . Also how to track user is entering into this geofence region or exiting from this region on android.

推荐答案

一个地理围栏仅仅是形成多边形纬度/长点的数组。一旦你有纬度/长点的列表,你可以用一个点内的多边形检查,看是否有位置是多边形内。

A geofence is simply an array of lat/long points that form a polygon. Once you have a list of lat/long points, you can use a point-inside-polygon check to see if a location is within the polygon.

这是code我在自己的项目来执行点的多边形检查非常大的凹多边形(20K +顶点):

This is code I have used in my own projects to perform point-in-polygon checks for very large concave polygons (20K+ vertices):

public class PolygonTest
{
    class LatLng
    {
        double Latitude;
        double Longitude;

        LatLng(double lat, double lon)
        {
            Latitude = lat;
            Longitude = lon;
        }
    }

    bool PointIsInRegion(double x, double y, LatLng[] thePath)
    {
        int crossings = 0;

        LatLng point = new LatLng (x, y);
        int count = thePath.length;
        // for each edge
        for (var i=0; i < count; i++) 
        {
            var a = thePath [i];
            var j = i + 1;
            if (j >= count) 
            {
                j = 0;
            }
            var b = thePath [j];
            if (RayCrossesSegment(point, a, b)) 
            {
                crossings++;
            }
        }
        // odd number of crossings?
        return (crossings % 2 == 1);
    }

    bool RayCrossesSegment(LatLng point, LatLng a, LatLng b)
    {
        var px = point.Longitude;
        var py = point.Latitude;
        var ax = a.Longitude;
        var ay = a.Latitude;
        var bx = b.Longitude;
        var by = b.Latitude;
        if (ay > by)
        {
            ax = b.Longitude;
            ay = b.Latitude;
            bx = a.Longitude;
            by = a.Latitude;
        }
            // alter longitude to cater for 180 degree crossings
        if (px < 0) { px += 360; };
        if (ax < 0) { ax += 360; };
        if (bx < 0) { bx += 360; };

        if (py == ay || py == by) py += 0.00000001;
        if ((py > by || py < ay) || (px > Math.max(ax, bx))) return false;
        if (px < Math.min(ax, bx)) return true;

        var red = (ax != bx) ? ((by - ay) / (bx - ax)) : float.MAX_VALUE;
        var blue = (ax != px) ? ((py - ay) / (px - ax)) : float.MAX_VALUE;
        return (blue >= red);
    }
}

我会尽力追捕链接到原来的javascript code,我移植这个距离。

I'll try to hunt down the link to the original javascript code that I ported this from.

在程序流程方面,你会希望有一个后台服务做位置更新,然后执行对你的纬度/经度的多边形数据此检查,看是否该位置是内在的。

In terms of program flow, you will want a background service to do location updates and then perform this check against your lat/long polygon data to see if the location is inside.

祝你好运!

编辑: 这里是链接到原始出处code,该算法来自: 的http://www.counsellingbyabhi.com/2013/01/google-map-check-whether-point-latlong.html

Here is the link to the original source code that this algorithm came from: http://www.counsellingbyabhi.com/2013/01/google-map-check-whether-point-latlong.html

这篇关于Android的地理围栏(多边形)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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