Skip to content

Instantly share code, notes, and snippets.

@nixta
Last active November 10, 2017 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nixta/4f2c013d018a3907d5e205ea1b1ec278 to your computer and use it in GitHub Desktop.
Save nixta/4f2c013d018a3907d5e205ea1b1ec278 to your computer and use it in GitHub Desktop.
How to drag a graphic with ArcGIS Runtime SDK for iOS
//
// ViewController.swift
// TestDrag
//
// Created by Nicholas Furness on 11/10/17.
// Copyright © 2017 Esri. All rights reserved.
//
import ArcGIS
class ViewController: UIViewController {
@IBOutlet weak var mapView: AGSMapView! {
didSet {
// Set the map to center on NYC
mapView.map = AGSMap(basemapType: .topographicVector, latitude: 40.73, longitude: -73.99, levelOfDetail: 14)
mapView.graphicsOverlays.add(draggableOverlay)
// We'll be the touchDelegate to handle the dragging
mapView.touchDelegate = self
}
}
lazy var draggableOverlay:AGSGraphicsOverlay = {
let overlay = AGSGraphicsOverlay()
// Add a single point to the graphics overlay
let pt = AGSPoint(x: -73.99, y: 40.73, spatialReference: AGSSpatialReference.wgs84())
let symbol = AGSSimpleMarkerSymbol(style: .triangle, color: UIColor.red, size: 20)
let graphic = AGSGraphic(geometry: pt, symbol: symbol, attributes: ["Name": "Draggable Graphic"])
overlay.graphics.add(graphic)
return overlay
}()
// Track the graphic we're dragging
var graphicBeingDragged:AGSGraphic?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
// Handle the drag
extension ViewController: AGSGeoViewTouchDelegate {
func geoView(_ geoView: AGSGeoView, didTouchDownAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint, completion: @escaping (Bool) -> Void) {
// When the user touches the map, let's figure out if they tapped the graphic - if so, don't pan the map around but
// start dragging that graphic.
geoView.identify(draggableOverlay, screenPoint: screenPoint, tolerance: 40, returnPopupsOnly: false) { identifyResult in
if let nearbyGraphicToDrag = identifyResult.graphics.first {
// We want to start dragging this graphic
// We would disconnect the graphic from the GPS location at this point and go into "Manual placement" mode so to speak.
// Track the graphic. By setting the opacity, we make it easier to precisely place the graphic using the default magnifier.
self.draggableOverlay.opacity = 0.3
self.graphicBeingDragged = nearbyGraphicToDrag
// Tell the ArcGIS Runtime that we're handling interaction for the time being.
completion(true)
} else {
// We didn't tap a graphic - the Runtime should handle interactions.
completion(false)
}
}
}
func geoView(_ geoView: AGSGeoView, didTouchDragToScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
guard let graphic = graphicBeingDragged else {
// Sanity Check: Nothing being dragged - we shouldn't be here
print("Unexpected drag event")
return
}
// Just move the graphic to follow the drag
graphic.geometry = mapPoint
}
func geoView(_ geoView: AGSGeoView, didTouchUpAtScreenPoint screenPoint: CGPoint, mapPoint: AGSPoint) {
guard let graphic = graphicBeingDragged else {
// Sanity Check: Nothing being dragged - we shouldn't be here
print("Unexpected touch up event")
return
}
// Move the graphic to its final position.
graphic.geometry = mapPoint
// Stop tracking the graphic now
graphicBeingDragged = nil
self.draggableOverlay.opacity = 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment