Ios 单击按钮时,如何使用用户当前位置显示批注?
因此,我正在创建一个应用程序,当单击一个按钮时,用户的当前位置将用于在Apple mapkit上创建注释。除了将按钮链接到创建注释并在地图上显示注释的代码之外,我的一切都正常工作。以下是我目前掌握的代码:Ios 单击按钮时,如何使用用户当前位置显示批注?,ios,swift,gps,annotations,mapkit,Ios,Swift,Gps,Annotations,Mapkit,因此,我正在创建一个应用程序,当单击一个按钮时,用户的当前位置将用于在Apple mapkit上创建注释。除了将按钮链接到创建注释并在地图上显示注释的代码之外,我的一切都正常工作。以下是我目前掌握的代码: import UIKit import CoreLocation import MapKit class ViewController: UIViewController, CLLocationManagerDelegate { var locationManager = CLLocati
import UIKit
import CoreLocation
import MapKit
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationManager = CLLocationManager()
@IBOutlet var Map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func locationManager(manager: CLLocationManager!, didUpdateToLocation newLocation: CLLocation!, fromLocation oldLocation: CLLocation!) {
println("Updating Location \(newLocation.coordinate.latitude) , \(newLocation.coordinate.longitude) ")
let span = MKCoordinateSpanMake(0.0009, 0.0009)
let region = MKCoordinateRegion(center: newLocation.coordinate, span: span)
Map.setRegion(region, animated: false)
}
}
正如您将看到的,我有它,所以它不断地向用户显示他们在哪里。我希望这样,但我也希望用户能够创建注释
我的问题是,我需要什么代码来创建注释,以及注释到哪里去了?我对编程相当陌生,所以我对所有的代码行话都不是很全面,所以如果你愿意的话(请帮我把事情说清楚)。此外,当用户单击添加注释的按钮时,需要删除最后一个注释。我该怎么做?我正在Xcode中使用Swift。您就快到了
- 首先,在代码顶部(就在类中)创建注释:
var annotation=MKPointAnnotation()
- 然后,使用
和newLocation.coordinate.latitude
创建.longitude
并将其设置为CLLocationCoordinate2D
annotation.coordinate
- 然后使用
和map.title=“something”
map.addAnnotation(annotation)
- 要实现这一点,您需要一些更全局的变量,但在放置注释之前,只需使用
——这样您一次只能在屏幕上显示一个注释map.removeAnnotation(annotation)
- 然后使用