1.在高德地图上绘制的多边形;经纬度逗号分隔格式;上面是用来方便存坐标的对象;下面是方法测试;直接复制代码即可运行

public class Point {

       private Double x;
private Double y;
public Point (Double x , Double y) {
this.x = x;
this.y = y;
}
public Double getX() {
return x;
}
public void setX(Double x) {
this.x = x;
}
public Double getY() {
return y;
}
public void setY(Double y) {
this.y = y;
}
}
public class Test01 {

     public static void main(String[] args) {
//114.331951,30.64091#114.341049,30.610185#114.331436,30.588058#114.312038,30.56393#114.293498,30.558609#114.267922,30.563784#114.231185,30.57945#114.212303,30.601616#114.235649,30.626878#114.280624,30.646818#
Map [] map=new Map[]{};
Point[] ps = new Point[] { new Point(114.309914,30.599556),//114.309914,30.599556
new Point(114.295688,30.592879),//114.295688,30.592879
new Point(114.292812,30.587726), //114.292812,30.587726
new Point(114.292812,30.587726), //114.292812,30.587726
new Point(114.30058,30.580318),//114.30058,30.580318
new Point(114.303606,30.586959),//114.303606,30.586959
new Point(114.304534,30.594751),//114.304534,30.594751
new Point(114.30838,30.590131),//114.30838,30.590131
new Point(114.308651,30.584182),//114.308651,30.584182
new Point(114.304495,30.584015),//114.304495,30.584015
new Point(114.301301,30.578759),//114.301301,30.578759
new Point(114.309437,30.578528),//114.309437,30.578528
new Point(114.323282,30.592786)};//114.323282,30.592786
Point n1 = new Point(114.303217,30.583553);
Point n2 = new Point(114.307336,30.597592);
Point n3 = new Point(114.286565,30.590056);
Point y1 = new Point(114.227342,30.587987);
Point y2 = new Point(120.1866 , 30.2672);
Point y4 = new Point(120.1869 , 30.2718);
System.out.println( "n1:" + isPtInPoly(n1.getX() , n1.getY() , ps));
System.out.println( "n2:" + isPtInPoly(n2.getX() , n2.getY() , ps));
System.out.println( "n3:" + isPtInPoly(n3.getX() , n3.getY() , ps));
System.out.println( "y1:" + isPtInPoly(y1.getX() , y1.getY() , ps));
System.out.println( "y2:" + isPtInPoly(y2.getX() , y2.getY() , ps));
System.out.println( "y4:" + isPtInPoly(y4.getX() , y4.getY() , ps));
}
public static boolean isPtInPoly (double ALon , double ALat , Point[] ps) {
int iSum, iCount, iIndex;
double dLon1 = 0, dLon2 = 0, dLat1 = 0, dLat2 = 0, dLon;
if (ps.length < 3) {
return false;
}
iSum = 0;
iCount = ps.length;
for (iIndex = 0; iIndex<iCount;iIndex++) {
if (iIndex == iCount - 1) {
dLon1 = ps[iIndex].getX();
dLat1 = ps[iIndex].getY();
dLon2 = ps[0].getX();
dLat2 = ps[0].getY();
} else {
dLon1 = ps[iIndex].getX();
dLat1 = ps[iIndex].getY();
dLon2 = ps[iIndex + 1].getX();
dLat2 = ps[iIndex + 1].getY();
}
// 以下语句判断A点是否在边的两端点的水平平行线之间,在则可能有交点,开始判断交点是否在左射线上
if (((ALat >= dLat1) && (ALat < dLat2)) || ((ALat >= dLat2) && (ALat < dLat1))) {
if (Math.abs(dLat1 - dLat2) > 0) {
//得到 A点向左射线与边的交点的x坐标:
dLon = dLon1 - ((dLon1 - dLon2) * (dLat1 - ALat) ) / (dLat1 - dLat2);
// 如果交点在A点左侧(说明是做射线与 边的交点),则射线与边的全部交点数加一:
if (dLon < ALon) {
iSum++;
}
}
}
}
if ((iSum % 2) != 0) {
return true;
}
return false;
}
}

java实现判断一个经纬度坐标是否在一个多边形内(经自己亲测)的更多相关文章

  1. Mysql中判断一个点是否落在多边形内

    关于地理空间数据,经常需要处理两个空间数据的关联关系.有很多种方法可以处理,通过编写程序算法,或者是调用数据库中对应的function.在mysql数据库中,https://dev.mysql.com ...

  2. Python 射线法判断一个点坐标是否在一个坐标区域内

    class Point: lng = '' lat = '' def __init__(self, lng, lat): self.lng = lng self.lat = lat # 求外包矩形 d ...

  3. 根据一个经纬度坐标获取周边最近经纬。Java实现

    1.需求:指定一个经纬度坐标,如:(31.2121751783,121.4411213954).周围有一堆经纬度坐标,找出与它最近的那个. 2.实现思路:将给出经纬度看成原点(0,0).周围经纬度定位 ...

  4. JAVA - 如何判断一个类是无用的类

    JAVA - 如何判断一个类是无用的类 方法区主要回收的是无用的类,那么如何判断一个类是无用的类的呢? 判定一个常量是否是"废弃常量"比较简单,而要判定一个类是否是"无用 ...

  5. Java中传入一个时间范围,取出该时间范围内所有日期的集合

    直接上代码: import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; impor ...

  6. 查看某一个点是否在某个多边形内 使用ST_Contains函数

    查看某一个点是否在某个多边形内  使用ST_Contains函数 --LINESTRING ( 121.312350 30.971457 , 121.156783 31.092221 , 121.35 ...

  7. java中判断一个字符串是否“都为数字”和“是否包含数字”和“截取数字”

    在javascript中有一个方法isDigit()使用来判断一个字符串是否都是数字,在java的字符串处理方法中没有这样的方法,觉得常常需要用到,于是上网搜了一下,整理出了两个用正则表达式匹配的判断 ...

  8. Java 如何判断一个字符是否是数字或字母

    在C++中, 可以用isdigit()判断一个字符是否是数字,可以用isalpha()判断一个字符是否是字母,还有很多,都在<cctype>头文件中 而类似的方法在JAVA中,则主要是Ch ...

  9. Java 正则判断一个字符串中是否包含中文

    使用正则判断一个字符串中是否包含中文或者中文字符 代码实现如下: import java.util.regex.Matcher; import java.util.regex.Pattern; /** ...

随机推荐

  1. golang string int int64转换

    #string到int int,err:=strconv.Atoi(string) #string到int64 int64, err := strconv.ParseInt(string, 10, 6 ...

  2. charCodeAt() 和charAt()

    charAt() 方法可返回指定位置的字符. charCodeAt() 方法可返回指定位置的字符的 Unicode 编码.这个返回值是 0 - 65535 之间的整数. 方法 charCodeAt() ...

  3. python - 自定制property/property的延时计算

    自定制prooerty: #模拟@property 实现将类的函数属性变成类属性: #定义描述符 class msf(): def __init__(self,obj): self.obj = obj ...

  4. css scrollbar样式设置

    参考链接:https://segmentfault.com/a/1190000012800450

  5. group by与avg(),max(),min(),sum()函数的关系

    数据库表: create table pay_report(     rdate varchar(8),     --日期     region_id varchar(4),    --地市      ...

  6. mysql 索引无法使用问题

    今天碰到一个问题,表中有一个索引不使用,怎么强制也没用 ,force index都没用, 后来才发现是类型不对, 比如索引字段是int,如果参数使用varchar,那么是无法使用索引的,参数类型最好统 ...

  7. 在SecureCRT中做make menuconfig乱码

      不能在SecureCRT中做(显示为乱码),从高手那里学来一招,解决了这个问题: options--terminal--emulation-- xterm  ansi color1.先设置终端为x ...

  8. lxde 的安装和卸载以及注意事项,lubuntu

    安装: $ sudo apt install lxde $ sudo apt install lxde-common 安装完毕后,可能没法关机及logout,可以使用如下安装: $ sudo apt ...

  9. SharePoint 2010 使用Install-SPSolution部署wsp包状态一直是”正在部署”

    1.服务器场信息如下: 2.使用下面命令部署,状态一直是"正在部署" Install-SPSolution –Identity xxxx.wsp –WebApplication h ...

  10. iptables实现网络防火墙及地址转换

    iptables主机防火墙功能及常用命令 FSM:Finite State Machine 有限状态机 客户端:closed -->syn_sent -->established --&g ...