判断经纬坐标是否在围栏内(地理围栏)

1 篇文章 0 订阅
1 篇文章 0 订阅

场景:

有时候我们会遇到根据用户不同的定位为用户推荐不同的信息内容

或者,根据用户位置不同 是否提供相应的服务

那么如何判断一个坐标是否在一组坐标所围成的地理围栏内呢?

以下代码作为参考

经纬坐标类:代码

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package util;

public class GeoPoint implements Cloneable {
    private double latitude;
    private double longitude;

    public GeoPoint(double latitude, double longitude) {
        this();
        this.setLatitude(latitude);
        this.setLongitude(longitude);
    }

    public GeoPoint() {
        this.latitude = 0.0D;
        this.longitude = 0.0D;
    }

    public void setLatitude(double lat) {
        this.latitude = lat;
    }
    public void setLongitude(double lon) {
        this.longitude = lon;
    }

    public double getLatitude() {
        return this.latitude;
    }

    public double getY() {
        return this.latitude;
    }

    public double getLongitude() {
        return this.longitude;
    }

    public double getX() {
        return this.longitude;
    }

    public boolean equals(Object other, double epsilon) {
        if (!(other instanceof GeoPoint)) {
            return false;
        } else {
            GeoPoint gp = (GeoPoint)other;
            double deltaLat = Math.abs(gp.getLatitude() - this.getLatitude());
            double deltaLon = Math.abs(gp.getLongitude() - this.getLongitude());
            return deltaLat < epsilon && deltaLon < epsilon;
        }
    }

    public boolean equals(Object other) {
        return this.equals(other, 1.0E-7D);
    }

    public int hashCode() {
        return super.hashCode();
    }

}

定位计算类:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package util;


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GeoPolygon implements Cloneable {
    private GeoPoint[] boundary;

    public GeoPolygon(List<GeoPoint> gpList) {
        this.boundary = gpList.toArray(getGeoPointArray(gpList.size()));
    }
    public static GeoPoint[] getGeoPointArray(int size) {
        return (GeoPoint[]) Array.newInstance(GeoPoint.class, size);
    }
    public static GeoPoint[] closePolygon(GeoPoint[] gp) {
        if (isEmpty(gp)) {
            return gp;
        } else if (gp.length < 3) {
            return gp;
        } else {
            GeoPoint gp0 = gp[0];
            GeoPoint gpN = gp[gp.length - 1];

            if(gp0.equals(gpN)){
                return gp;
            }else{
                ArrayList<GeoPoint> resultList = new ArrayList<>(Arrays.asList(gp)) ;
                resultList.add(gp0);
                return resultList.toArray(getGeoPointArray(resultList.size()));
            }
        }
    }
    public static <T> boolean isEmpty(T[] A) {
        return A == null || A.length == 0;
    }
    public GeoPoint[] getGeoPoints() {
        return this.boundary;
    }
    public boolean isPointInside(GeoPoint gp) {
        return isPointInside(gp, this.getGeoPoints());
    }
    public static boolean isPointInside(GeoPoint gp, GeoPoint... pp) {
        if (gp != null && pp != null) {
            pp = closePolygon(pp);
            int wn = 0;

            for(int i = 0; i < pp.length - 1; ++i) {
                if (pp[i].getY() <= gp.getY()) {
                    if (pp[i + 1].getY() > gp.getY() && _isLeft(pp[i], pp[i + 1], gp) > 0.0D) {
                        ++wn;
                    }
                } else if (pp[i + 1].getY() <= gp.getY() && _isLeft(pp[i], pp[i + 1], gp) < 0.0D) {
                    --wn;
                }
            }

            return wn != 0;
        } else {
            return false;
        }
    }

    private static double _isLeft(GeoPoint gp0, GeoPoint gp1, GeoPoint gpC) {
        double val = (gp1.getX() - gp0.getX()) * (gpC.getY() - gp0.getY()) - (gpC.getX() - gp0.getX()) * (gp1.getY() - gp0.getY());
        return val;
    }


}

测试

package util;

import java.util.ArrayList;
import java.util.List;

public class test {

    public static void main(String[] args) {
        GeoPoint testgp1 = new GeoPoint(Double.parseDouble("36.8"), Double.parseDouble("112.7"));// Point1 inside
        List<GeoPoint> geoPointList = new ArrayList<>();
        geoPointList.add(new GeoPoint(37.6, 111.3));
        geoPointList.add(new GeoPoint(37.5, 115.6));
        geoPointList.add(new GeoPoint(35.25, 113.2));

        GeoPolygon fence = new GeoPolygon(geoPointList);
        boolean inzone = fence.isPointInside(testgp1);
        System.out.println("testGp1是否在范围内:"+inzone);
    }
}

结果

以上就是全部代码了是不是非常简单呢

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值