Skip to content
View on GitHub

Format coordinates in a variety of common notations.

Images of show coordinates in multiple formats

Use case

The coordinate formatter can format a map location in WGS84 in a number of common coordinate notations. Parsing one of these formats to a location is also supported. Formats include decimal degrees; degrees, minutes, seconds; Universal Transverse Mercator (UTM), and United States National Grid (USNG).

How to use the sample

Tap on the map to see the text fields update with the tapped location's coordinates formatted in 4 different ways. You can also put a coordinate string in the corresponding text field. Tap return and the coordinate string will be parsed to a map location which will move the map point and update the other text fields' coordinate strings.

How it works

  1. Get or create a map Point with a spatial reference.
  2. To get the formatted string, use one of the static methods below.
    • class CoordinateFormatter.latitudeLongitudeString(from:format:decimalPlaces:)
    • class CoordinateFormatter.utmString(from:conversionMode:addSpaces:)
    • class CoordinateFormatter.usngString(from:precision:addSpaces:)
  3. To get an Point from a formatted string, use one of the static methods below.
    • class CoordinateFormatter.point(fromLatitudeLongitudeString:spatialReference:)
    • class CoordinateFormatter.point(fromUTMString:spatialReference:conversionMode:)
    • class CoordinateFormatter.point(fromUSNGString:spatialReference:)

Relevant API

  • CoordinateFormatter
  • CoordinateFormatter.LatitudeLongitudeFormat
  • CoordinateFormatter.UTMConversionMode

Tags

convert, coordinate, decimal degrees, degree minutes seconds, format, latitude, longitude, USNG, UTM

Sample Code

ShowCoordinatesInMultipleFormatsView.swift
Use dark colors for code blocksCopy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 // Copyright 2023 Esri // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License.  import ArcGIS import SwiftUI  struct ShowCoordinatesInMultipleFormatsView: View {  /// The view model for the sample.  @StateObject private var model = Model()   /// The tapped point on the map.  @State private var tappedPoint = Point(latitude: 0, longitude: 0)   var body: some View {  // Input text fields.  VStack {  CoordinateTextField(  title: "Decimal Degrees",  text: $model.latLongDDString  )  .onSubmit {  if let point = CoordinateFormatter.point(  fromLatitudeLongitudeString: model.latLongDDString,  spatialReference: tappedPoint.spatialReference  ) {  model.updateCoordinates(point: point)  } else {  model.updateCoordinates(point: tappedPoint)  }  }  CoordinateTextField(  title: "Degrees, Minutes, Seconds",  text: $model.latLongDMSString  )  .onSubmit {  if let point = CoordinateFormatter.point(  fromLatitudeLongitudeString: model.latLongDMSString,  spatialReference: tappedPoint.spatialReference  ) {  model.updateCoordinates(point: point)  } else {  model.updateCoordinates(point: tappedPoint)  }  }  CoordinateTextField(  title: "UTM",  text: $model.utmString  )  .onSubmit {  if let point = CoordinateFormatter.point(  fromUTMString: model.utmString,  spatialReference: tappedPoint.spatialReference,  conversionMode: .latitudeBandIndicators  ) {  model.updateCoordinates(point: point)  } else {  model.updateCoordinates(point: tappedPoint)  }  }  CoordinateTextField(  title: "USNG",  text: $model.usngString  )  .onSubmit {  if let point = CoordinateFormatter.point(  fromUSNGString: model.usngString,  spatialReference: tappedPoint.spatialReference  ) {  model.updateCoordinates(point: point)  } else {  model.updateCoordinates(point: tappedPoint)  }  }   MapView(map: model.map, graphicsOverlays: [model.graphicsOverlay])  .onSingleTapGesture { _, mapPoint in  // Updates the `mapPoint`, its graphic, and the corresponding coordinates.  self.tappedPoint = mapPoint  model.updateCoordinates(point: mapPoint)  }  }  } }  private struct CoordinateTextField: View {  /// The title of the text field.  var title: String   /// The text in the text field.  @Binding var text: String   var body: some View {  VStack(alignment: .leading) {  Text(title)  .padding([.leading, .top], 8)  .padding(.bottom, -5)  TextField("", text: $text)  .textInputAutocapitalization(.never)  .autocorrectionDisabled()  .textFieldStyle(.roundedBorder)  .padding([.leading, .trailing, .bottom], 8)  }  } }  private extension ShowCoordinatesInMultipleFormatsView {  // The view model for the sample.  class Model: ObservableObject {  /// A map with an imagery basemap.  let map = Map(basemapStyle: .arcGISImageryStandard)   /// The graphics overlay for the point graphic.  let graphicsOverlay: GraphicsOverlay   /// A yellow cross graphic for the map point.  private let pointGraphic = Graphic(symbol: SimpleMarkerSymbol(style: .cross, color: .yellow, size: 20))   /// The decimal degrees text.  @Published var latLongDDString = ""   /// The degree minute seconds text.  @Published var latLongDMSString = ""   /// The UTM text.  @Published var utmString = ""   /// The USNG text.  @Published var usngString = ""   init() {  graphicsOverlay = GraphicsOverlay(graphics: [pointGraphic])  updateCoordinates(point: Point(latitude: 0, longitude: 0))  }   /// Updates the map point graphic and the corresponding coordinates.  /// - Parameter point: A `Point` used to update.  func updateCoordinates(point: Point) {  pointGraphic.geometry = point  updateCoordinateFields(mapPoint: point)  }   /// Generates and updates the coordinate strings using the coordinate formatter.  /// - Parameter mapPoint: A `Point` to get coordinates from.  private func updateCoordinateFields(mapPoint: Point) {  latLongDDString = CoordinateFormatter.latitudeLongitudeString(  from: mapPoint,  format: .decimalDegrees,  decimalPlaces: 4  )   latLongDMSString = CoordinateFormatter.latitudeLongitudeString(  from: mapPoint,  format: .degreesMinutesSeconds,  decimalPlaces: 1  )   utmString = CoordinateFormatter.utmString(  from: mapPoint,  conversionMode: .latitudeBandIndicators,  addSpaces: true  )   usngString = CoordinateFormatter.usngString(  from: mapPoint,  precision: 4,  addSpaces: true  )  }  } }  #Preview {  ShowCoordinatesInMultipleFormatsView() }

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.