Overview
Open Mobile Maps uses several coordinate types to represent positions, rectangular bounds, and polygon shapes. Understanding these types is essential for working with map positions and geometries.
MCCoord
MCCoord represents a single point coordinate on the map.
Creating Coordinates
Using Latitude/Longitude
The most common way to create coordinates using WGS84 latitude and longitude:
let coord = MCCoord(lat: 46.962592372639634, lon: 8.378232525377973)
Latitude in WGS84 decimal degrees (-90 to 90)
Longitude in WGS84 decimal degrees (-180 to 180)
Using Custom Coordinate System
Create coordinates in a specific coordinate system:
let coord = MCCoord(
systemIdentifier: MCCoordinateSystemIdentifiers.epsg3857(),
x: -20037508.34,
y: 20037508.34,
z: 0.0
)
Coordinate system identifier (e.g., EPSG codes)
X coordinate in the specified system
Y coordinate in the specified system
Z coordinate (elevation or height)
Usage Examples
Camera Positioning
mapView.camera.move(
toCenterPositionZoom: MCCoord(lat: 47.3769, lon: 8.5417),
zoom: 500000,
animated: true
)
Icon Placement
let icon = MCIconFactory.createIcon(
"marker",
coordinate: MCCoord(lat: 46.962592372639634, lon: 8.378232525377973),
texture: texture,
iconSize: .init(x: 32, y: 32),
scale: .FIXED,
blendMode: .NORMAL
)
MCRectCoord
MCRectCoord represents a rectangular bounding box defined by two corner coordinates.
Creating Rectangular Bounds
let identifer = MCCoordinateSystemIdentifiers.epsg3857()
let topLeft = MCCoord(
systemIdentifier: identifer,
x: -20037508.34,
y: 20037508.34,
z: 0.0
)
let bottomRight = MCCoord(
systemIdentifier: identifer,
x: 20037508.34,
y: -20037508.34,
z: 0.0
)
let bounds = MCRectCoord(
topLeft: topLeft,
bottomRight: bottomRight
)
Top-left corner coordinate of the rectangle
Bottom-right corner coordinate of the rectangle
Usage Examples
Layer Bounds Configuration
class TiledLayerConfig: MCTiled2dMapLayerConfig {
func getBounds() -> MCRectCoord {
let identifer = MCCoordinateSystemIdentifiers.epsg3857()
let topLeft = MCCoord(
systemIdentifier: identifer,
x: -20037508.34,
y: 20037508.34,
z: 0.0
)
let bottomRight = MCCoord(
systemIdentifier: identifer,
x: 20037508.34,
y: -20037508.34,
z: 0.0
)
return MCRectCoord(
topLeft: topLeft,
bottomRight: bottomRight
)
}
}
Zoom Level Info
.init(
zoom: 559082264.029,
tileWidthLayerSystemUnits: 40_075_016,
numTilesX: 1,
numTilesY: 1,
numTilesT: 1,
zoomLevelIdentifier: 0,
bounds: getBounds()
)
MCPolygonCoord
MCPolygonCoord represents a polygon shape with optional holes (interior rings).
Creating Polygons
let coords: [MCCoord] = [
MCCoord(lat: 47.0, lon: 8.0),
MCCoord(lat: 47.5, lon: 8.0),
MCCoord(lat: 47.5, lon: 9.0),
MCCoord(lat: 47.0, lon: 9.0),
MCCoord(lat: 47.0, lon: 8.0) // Close the polygon
]
let polygon = MCPolygonCoord(positions: coords, holes: [])
Array of coordinates defining the polygon’s outer boundary. The first and last coordinates should be the same to close the polygon.
Array of coordinate arrays defining holes (interior rings) within the polygon. Each hole is an array of coordinates forming a closed ring.
Polygons with Holes
let outerRing: [MCCoord] = [
MCCoord(lat: 47.0, lon: 8.0),
MCCoord(lat: 47.5, lon: 8.0),
MCCoord(lat: 47.5, lon: 9.0),
MCCoord(lat: 47.0, lon: 9.0),
MCCoord(lat: 47.0, lon: 8.0)
]
let hole: [MCCoord] = [
MCCoord(lat: 47.1, lon: 8.1),
MCCoord(lat: 47.4, lon: 8.1),
MCCoord(lat: 47.4, lon: 8.9),
MCCoord(lat: 47.1, lon: 8.9),
MCCoord(lat: 47.1, lon: 8.1)
]
let polygon = MCPolygonCoord(positions: outerRing, holes: [hole])
Usage Example
Creating a Polygon Layer
let coords: [MCCoord] = [
// Polygon coordinates
]
let polygonLayer = MCPolygonLayerInterface.create()
let polygonInfo = MCPolygonInfo(
identifier: "switzerland",
coordinates: MCPolygonCoord(positions: coords, holes: []),
color: UIColor.red.mapCoreColor,
highlight: UIColor.red.withAlphaComponent(0.2).mapCoreColor
)
polygonLayer?.add(polygonInfo)
mapView.add(layer: polygonLayer?.asLayerInterface())
Coordinate System Identifiers
Open Mobile Maps provides coordinate system identifiers through MCCoordinateSystemIdentifiers.
Common Coordinate Systems
EPSG:3857 (Web Mercator)
let identifier = MCCoordinateSystemIdentifiers.epsg3857()
The standard coordinate system used by most web mapping applications, including OpenStreetMap and Google Maps.
EPSG:2056 (Swiss Coordinate System)
let identifier = MCCoordinateSystemIdentifiers.epsg2056()
Using Custom Coordinate Systems
let mapView = MCMapView(
mapConfig: .init(
mapCoordinateSystem: MCCoordinateSystemFactory.getEpsg2056System()
)
)
Coordinate Arrays
Many operations require arrays of coordinates:
Line Coordinates
let lineCoords: [MCCoord] = [
MCCoord(lat: 47.0, lon: 8.0),
MCCoord(lat: 47.5, lon: 8.5),
MCCoord(lat: 48.0, lon: 9.0)
]
let line = MCLineFactory.createLine(
"route",
coordinates: lineCoords,
style: lineStyle
)
Polygon Coordinates
let polygonCoords: [MCCoord] = [
MCCoord(lat: 47.0, lon: 8.0),
MCCoord(lat: 47.5, lon: 8.0),
MCCoord(lat: 47.5, lon: 9.0),
MCCoord(lat: 47.0, lon: 9.0),
MCCoord(lat: 47.0, lon: 8.0)
]
let polygon = MCPolygonCoord(positions: polygonCoords, holes: [])
Best Practices
Close Polygon Rings
Always ensure polygon coordinates form a closed ring by making the first and last coordinates identical:
// Good: Closed polygon
let coords = [
MCCoord(lat: 47.0, lon: 8.0),
MCCoord(lat: 47.5, lon: 8.0),
MCCoord(lat: 47.5, lon: 9.0),
MCCoord(lat: 47.0, lon: 8.0) // Closes the ring
]
// Bad: Unclosed polygon
let coords = [
MCCoord(lat: 47.0, lon: 8.0),
MCCoord(lat: 47.5, lon: 8.0),
MCCoord(lat: 47.5, lon: 9.0)
// Missing closing coordinate
]
Validate Coordinate Ranges
Ensure latitude and longitude values are within valid ranges:
// Valid ranges:
// Latitude: -90 to 90
// Longitude: -180 to 180
let validCoord = MCCoord(lat: 46.962592372639634, lon: 8.378232525377973)
// Invalid coordinates may cause unexpected behavior
let invalidCoord = MCCoord(lat: 200, lon: 300) // Avoid!
Use Appropriate Coordinate System
Match the coordinate system to your data source:
// For WGS84 lat/lon data (most common)
let coord = MCCoord(lat: 46.962592372639634, lon: 8.378232525377973)
// For projected coordinate data
let coord = MCCoord(
systemIdentifier: MCCoordinateSystemIdentifiers.epsg3857(),
x: 931734.23,
y: 5930562.45,
z: 0.0
)