[英]How do I display an annotation using the Users current location when a button is clicked?
So I am creating an app that when a button is clicked, the users current location is used to create an annotation on Apples mapkit. 因此,我正在创建一个应用程序,当单击按钮时,将使用用户当前位置在Apples mapkit上创建注释。 I have everything working except for linking the button up to code that will create the annotation and display the annotation on the map.
除了将按钮链接到将创建注释并在地图上显示注释的代码之外,我已经完成了所有工作。 Bellow is the code I have so far:
贝娄是我到目前为止的代码:
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)
}
}
As you will see, I have it so it constantly shows the user where they are. 如您所见,我拥有它,因此它不断向用户显示他们的位置。 I want this but I also want the user to be able to create the annotation.
我想要这个,但是我也希望用户能够创建注释。
My question is what code do I need to create the annotations and where does it go? 我的问题是创建注释需要什么代码,它在哪里? I am fairly new to programing so I'm not completely comprehensive of all of the code lingo so if you would (dumb things down for me please).
我是编程的新手,所以我对所有的代码术语并不完全了解,所以请(对我来说愚蠢的东西)。 Also, when the user clicks the button that adds the annotation, the last annotation needs to be deleted.
同样,当用户单击添加注释的按钮时,需要删除最后一个注释。 How would I do this?
我该怎么做? I am using Swift in Xcode.
我在Xcode中使用Swift。
You're almost there! 你快到了!
First, create an annotation at the top of the code (just within the class): 首先,在代码顶部(在类内)创建一个注释:
var annotation = MKPointAnnotation()
Then, use newLocation.coordinate.latitude
and .longitude
to create a CLLocationCoordinate2D
and set that to annotation.coordinate
. 然后,使用
newLocation.coordinate.latitude
和.longitude
创建一个CLLocationCoordinate2D
并将其设置为annotation.coordinate
.longitude
。
Then use map.title = "something"
and map.addAnnotation(annotation)
. 然后使用
map.title = "something"
和map.addAnnotation(annotation)
。
You'll need to have some of the variables more global to achieve this, but before you place the annotation, simply use map.removeAnnotation(annotation)
- so you only ever have one annotation on the screen at a time. 您需要使某些变量具有全局性才能实现此目的,但是在放置注释之前,只需使用
map.removeAnnotation(annotation)
-这样您一次只能在屏幕上拥有一个注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.