在ArcGIS中,可以使用SpatialReference、Graphic、geometryEngine和query相关类来查找距离给定点最近的点。
以下是一个示例代码,其中包含一个示例geoJSON多点要素数组和一个用于查找最近点的函数findClosestPoint:
require([
"esri/geometry/Point",
"esri/tasks/GeometryService",
"esri/tasks/support/ProjectParameters",
"esri/geometry/geometryEngine",
"esri/Graphic",
"dojo/domReady!"
], function(Point, GeometryService, ProjectParameters, geometryEngine, Graphic) {
// GeoJSON multi-point feature example
var multiPointFeature = {
"type": "Feature",
"geometry": {
"type": "MultiPoint",
"coordinates": [
[0, 0],
[2, 2],
[4, 4],
[6, 6]
]
}
};
// Function to find closest point to given point in multi-point feature
function findClosestPoint(point, multiPointFeature) {
// Project point to spatial reference of multi-point feature
var geometryService = new GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
var projectParameters = new ProjectParameters({
geometries: [point],
outSR: multiPointFeature.geometry.spatialReference
});
geometryService.project(projectParameters).then(function(result) {
// Find closest point using geometryEngine.nearestCoordinate
var closestPoint = geometryEngine.nearestCoordinate(multiPointFeature.geometry, result[0]);
// Create a graphic for the closest point and add it to the map
var graphic = new Graphic({
geometry: closestPoint.coordinate,
symbol: {
type: "simple-marker",
style: "square",
color: "blue",
size: "12px",
outline: {
color: [255, 255, 0],
width: 3
}
}
});