java判斷經緯度是否在地圖圓內

方法一 geodesy

        <dependency>
              <groupId>org.gavaghan</groupId>
              <artifactId>geodesy</artifactId>
              <version>1.1.3</version>
        </dependency>
import org.gavaghan.geodesy.Ellipsoid;
import org.gavaghan.geodesy.GeodeticCalculator;
import org.gavaghan.geodesy.GeodeticCurve;
import org.gavaghan.geodesy.GlobalCoordinates;

public static double getDistanceMeter(GlobalCoordinates gpsFrom, GlobalCoordinates gpsTo, Ellipsoid ellipsoid){

		//創建GeodeticCalculator,調用計算方法,傳入座標系、經緯度用於計算距離
		GeodeticCurve geoCurve = new GeodeticCalculator().calculateGeodeticCurve(ellipsoid, gpsFrom, gpsTo);

		return geoCurve.getEllipsoidalDistance();
	}

	public static void main(String[] args) throws IOException {
		GlobalCoordinates source = new GlobalCoordinates(30f, 110f);
		GlobalCoordinates target = new GlobalCoordinates(31f, 111f);

		double meter1 = getDistanceMeter(source, target, Ellipsoid.Sphere);
		double meter2 = getDistanceMeter(source, target, Ellipsoid.WGS84);

		System.out.println("Sphere座標系計算結果:"+meter1 + "米");
		System.out.println("WGS84座標系計算結果:"+meter2 + "米");
	}

方法二 GeoTools

<dependency>
            <groupId>org.locationtech</groupId>
            <artifactId>jts</artifactId>
            <version>1.13</version>
        </dependency>
import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.operation.buffer.BufferOp;
import com.vividsolutions.jts.algorithm.PointLocator;

/**
	 * 判斷經緯度是否在圓內
	 * @param lon 目標經度
	 * @param lat 目標緯度
	 * @param dis 距離,米
	 * @param centerLon 圓心經度
	 * @param centerLat 圓心緯度
	 * @return
	 */
	public static boolean inCircle(float lon, float lat,long dis,float centerLon,float centerLat){
		//創建一條直線
		Coordinate[] coordinates4 = new Coordinate[] {
				new Coordinate(centerLon,centerLat),
				new Coordinate(centerLon,centerLon+0.01f*dis/1000),
		};

		GeometryFactory gf=new GeometryFactory();
		Geometry gfLineString = gf.createLineString(coordinates4);
		double degree = dis / (2*Math.PI*6371004)*360; //將度換算成米,公式爲:degree = meter / (2 * Math.PI * 6371004) * 360
		//緩衝區建立
		BufferOp bufOp = new BufferOp(gfLineString);
		bufOp.setEndCapStyle(BufferOp.CAP_BUTT);
		Geometry bg = bufOp.getResultGeometry(degree);
		//點是否在多邊形內判斷
		Coordinate point = new Coordinate(lon,lat);
		PointLocator a=new PointLocator();
		boolean p1=a.intersects(point, bg);
		if(p1) {
			System.out.println("point1:" + "該點在多邊形內" + p1);
		}else {
			System.out.println("point1:" + "該點不在多邊形內" + p1);
		}
		return p1;
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章