View on GitHub

Find the closest vertex and coordinate of a geometry to a point.

Image of find nearest vertex

Use case

Determine the shortest distance between a location and the boundary of an area. For example, developers can snap imprecise user taps to a geometry if the tap is within a certain distance of the geometry.

How to use the sample

Tap anywhere on the map. An orange cross will show at that location. A blue circle will show the polygon’s nearest vertex to the point that was tapped. A red diamond will appear at the coordinate on the geometry that is nearest to the point that was tapped. If tapped inside the geometry, the red and orange markers will overlap. The information callout showing distance between the tapped point and the nearest vertex/coordinate will be updated with every new location tapped.

How it works

  1. Get a Geometry and a Point to check the nearest vertex against.
  2. Call class GeometryEngine.nearestVertex(in:to:).
  3. Use the returned ProximityResult to get the Point representing the polygon vertex, and to determine the distance between that vertex and the tapped point.
  4. Call class GeometryEngine.nearestCoordinate(in:to:).
  5. Use the returned ProximityResult to get the Point representing the coordinate on the polygon, and to determine the distance between that coordinate and the tapped point.

Relevant API

  • class GeometryEngine.nearestCoordinate(in:to:)
  • class GeometryEngine.nearestVertex(in:to:)
  • class GeometryEngine.normalizeCentralMeridian(of:)
  • Geometry
  • ProximityResult

Additional information

The value of ProximityResult.distance is planar (Euclidean) distance. Planar distances are only accurate for geometries that have a defined projected coordinate system, which maintain the desired level of accuracy. The example polygon in this sample is defined in California State Plane Coordinate System - Zone 5 (WKID 2229), which maintains accuracy near Southern California. Accuracy declines outside the state plane zone.

Tags

analysis, coordinate, geometry, nearest, proximity, vertex