经纬度6位小数格式,java代码 计算点到线的距离
时间: 2023-11-04 14:04:17 浏览: 88
以下是Java代码示例,使用了JTS Topology Suite库来进行计算:
```java
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
import com.vividsolutions.jts.operation.distance.DistanceOp;
public class PointLineDistance {
public static void main(String[] args) {
// 创建点和线段对象
Coordinate pointCoord = new Coordinate(105.123456, 30.654321);
Point point = new GeometryFactory().createPoint(pointCoord);
Coordinate[] lineCoords = new Coordinate[]{new Coordinate(105.123, 30.654),
new Coordinate(105.124, 30.655),
new Coordinate(105.125, 30.654)};
LineString line = new GeometryFactory().createLineString(lineCoords);
// 计算点到线的距离
DistanceOp distOp = new DistanceOp(point, line);
double distance = distOp.distance();
System.out.println("Distance from point to line: " + distance);
}
}
```
上述代码中,首先创建了一个点对象和一个线段对象,然后使用`DistanceOp`对象计算了点到线的距离。最后输出了距离的值。需要注意的是,这里的经纬度坐标需要先转换为平面直角坐标系中的坐标才能进行计算。
阅读全文