pom文件引入依赖jar包
<dependency>
<groupId>com.mapbox.mapboxsdk</groupId>
<artifactId>mapbox-sdk-turf</artifactId>
<version>5.8.0</version>
</dependency>
定义dto
import lombok.Data;
import java.io.Serializable;
import java.util.List;
@Data
public class CoordinatesDTO implements Serializable {
private static final long serialVersionUID = 1L;
private List<List<List<Double>>> rings;
private SpatialReferenceDTO spatialReference;
}
import lombok.Data;
import java.io.Serializable;
@Data
public class SpatialReferenceDTO implements Serializable {
private static final long serialVersionUID = 1L;
private Integer wkid;
}
封装的工具类方法
import cn.hutool.core.collection.CollectionUtil;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;
import com.mapbox.turf.TurfTransformation;
import lombok.extern.slf4j.Slf4j;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @Author: 夏威夷8080
* @Date: 2021/7/9 13:24
*/
@Slf4j
public class GeometryUtil {
public static CoordinatesDTO buildCoordinates(BigDecimal lng, BigDecimal lat, double radius) {
Point TOWER_BRIDGE = Point.fromLngLat(lng.doubleValue(), lat.doubleValue());
Polygon metres = TurfTransformation.circle(TOWER_BRIDGE, radius, "metres");
List<List<Point>> coordinates = metres.coordinates();
CoordinatesDTO coordinatesDTO = new CoordinatesDTO();
if (CollectionUtil.isNotEmpty(coordinates)){
List<Point> points = coordinates.get(0);
List<List<Double>> collect = points.stream().map(point -> point.coordinates()).collect(Collectors.toList());
List<List<List<Double>>> rings = new ArrayList<>();
rings.add(collect);
coordinatesDTO.setRings(rings);
SpatialReferenceDTO spatialReference = new SpatialReferenceDTO();
spatialReference.setWkid(4326);
coordinatesDTO.setSpatialReference(spatialReference);
}
return coordinatesDTO;
}
}
测试
public static void main(String[] args) {
CoordinatesDTO coordinatesDTO = buildCoordinates(new BigDecimal(116.440488), new BigDecimal(39.947025), 12.05);
System.out.println(JSONObject.toJSONString(coordinatesDTO));
}
返回的数据
{
"rings": [
[
[
116.440488,
39.94713333424496
],
[
116.44050185089663,
39.94713281258514
],
[
116.44051556840091,
39.947131252629549
],
[
116.44052902040518,
39.94712866940151
],
[
116.44054207735873,
39.94712508777902
],
[
116.44055461351548,
39.94712054225522
],
[
116.440566508145,
39.947115076606177
],
[
116.44057764669518,
39.947108743469289
],
[
116.44058792189549,
39.94710160383635
],
[
116.44059723479013,
39.94709372646619
],
[
116.44060549569086,
39.94708518722241
],
[
116.4406126250409,
39.94707606834284
],
[
116.44061855418096,
39.94706645764749
],
[
116.44062322601063,
39.94705644769277
],
[
116.4406265955381,
39.94704613488022
],
[
116.44062863031354,
39.947035618527937
],
[
116.44062931074156,
39.94702499991421
],
[
116.44062863026989,
39.947014381302157
],
[
116.44062659545249,
39.94700386495475
],
[
116.44062322588631,
39.94699355215012
],
[
116.44061855402274,
39.94698354220607
],
[
116.44061262485482,
39.946973931523718
],
[
116.44060549548412,
39.946964812658979
],
[
116.44059723457065,
39.94695627343129
],
[
116.44058792167172,
39.946948396077868
],
[
116.44057764647568,
39.94694125646166
],
[
116.44056650793826,
39.94693492334087
],
[
116.44055461332943,
39.946929457706648
],
[
116.4405420772005,
39.94692491219584
],
[
116.44052902028084,
39.946921330584029
],
[
116.44051556831526,
39.94691874736391
],
[
116.44050185085297,
39.94691718741321
],
[
116.440488,
39.94691666575503
],
[
116.44047414914705,
39.94691718741321
],
[
116.44046043168474,
39.94691874736391
],
[
116.44044697971917,
39.946921330584029
],
[
116.44043392279952,
39.94692491219584
],
[
116.4404213866706,
39.946929457706648
],
[
116.44040949206176,
39.94693492334087
],
[
116.44039835352435,
39.94694125646166
],
[
116.4403880783283,
39.946948396077868
],
[
116.44037876542937,
39.94695627343129
],
[
116.4403705045159,
39.946964812658979
],
[
116.4403633751452,
39.946973931523718
],
[
116.44035744597727,
39.94698354220607
],
[
116.4403527741137,
39.94699355215012
],
[
116.44034940454753,
39.94700386495475
],
[
116.44034736973014,
39.947014381302157
],
[
116.44034668925846,
39.94702499991421
],
[
116.44034736968648,
39.947035618527937
],
[
116.44034940446191,
39.94704613488022
],
[
116.44035277398938,
39.94705644769277
],
[
116.44035744581906,
39.94706645764749
],
[
116.44036337495912,
39.94707606834284
],
[
116.44037050430916,
39.94708518722241
],
[
116.44037876520989,
39.94709372646619
],
[
116.4403880781045,
39.94710160383635
],
[
116.44039835330485,
39.947108743469289
],
[
116.44040949185502,
39.947115076606177
],
[
116.44042138648453,
39.94712054225522
],
[
116.44043392264126,
39.94712508777902
],
[
116.44044697959484,
39.94712866940151
],
[
116.4404604315991,
39.947131252629549
],
[
116.4404741491034,
39.94713281258514
],
{
"$ref": "$.rings[0][0]"
}
]
],
"spatialReference": {
"wkid": 4326
}
}