我正在尝试在地图上的多个位置添加注释。我正在使用模拟器,选择iPhone 6作为设备。但是,当我将地图拖动到其他位置以添加注释时,它会一直返回到基本位置。模拟器在启动时显示的原始位置(注意:我已导入CoreLocation,添加了添加的NSLocationAlways和WheninUse到信息的.plist)。
如何让它停留在我将地图拖动到的位置?
这是代码......
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet var theMap: MKMapView!
var locManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locManager.requestWhenInUseAuthorization()
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.delegate = self
locManager.startUpdatingLocation()
let longPress = UILongPressGestureRecognizer(target: self, action: "longPressed:")
longPress.minimumPressDuration = 2
self.view.addGestureRecognizer(longPress)
}
func longPressed(theGesture: UIGestureRecognizer) {
if theGesture.state == UIGestureRecognizerState.Began {
print("recognised")
let touchPoint = theGesture.locationInView(self.theMap)
let convertedTouchPoint = theMap.convertPoint(touchPoint, toCoordinateFromView: self.theMap)
let annotation = MKPointAnnotation()
annotation.title = "Place 1"
annotation.subtitle = "Description Place 1"
annotation.coordinate = convertedTouchPoint
theMap.addAnnotation(annotation)
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation = locations[0]
let latitude: CLLocationDegrees = userLocation.coordinate.latitude
let longitude: CLLocationDegrees = userLocation.coordinate.longitude
let latDelta:CLLocationDegrees = 0.01
let longDelta:CLLocationDegrees = 0.01
let center = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: longDelta)
let region = MKCoordinateRegion(center: center, span: span)
theMap.setRegion(region, animated: true)
}
}
答案 0 :(得分:0)
刚想通了,选择了Simulator,导航到Debug - >位置和选择“无”#39;这解决了这个问题。我现在可以拖到不同的位置,而不会回到基地位置。