Ios 单击按钮时,如何使用用户当前位置显示批注?

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

因此,我正在创建一个应用程序,当单击一个按钮时,用户的当前位置将用于在Apple mapkit上创建注释。除了将按钮链接到创建注释并在地图上显示注释的代码之外,我的一切都正常工作。以下是我目前掌握的代码:

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)
      ——这样您一次只能在屏幕上显示一个注释


好的,下面是我拥有的:导入UIKit导入CoreLocation导入MapKit类ViewController:UIViewController,CLLocationManagerDelegate{var-locationManager=CLLocationManager()//新代码添加了var-annotation=MKPointAnnotation()//@IBOutlet-var-Map:MKMapView!覆盖函数viewDidLoad(){super.viewDidLoad()//加载视图后执行任何其他设置,通常从nib.locationManager.delegate=self locationManager.requestWhenUseAuthorization()locationManager.startUpdatingLocation()}覆盖func didReceiveMemoryWarning(){super.didReceiveMemoryWarning()//处理所有可以重新创建的资源。}func locationManager(manager:CLLocationManager!、didUpdateToLocation newLocation:CLLocation!、fromLocation oldLocation:CLLocation!){println(“更新位置(newLocation.coordinate.latitude)),(newLocation.coordinate.longitude)“//新代码添加了newLocation.coordinate.latitude newLocation.coordinate.longitude让span=MKCoordinateSpanMake(0.0009,0.0009)让region=MKCoordinateRegion(中心:newLocation.coordinate,span:span)Map.setRegion(区域,动画:false)}好的,这样代码就会不断地将地图集中在用户的位置上。要设置注释,首先设置一个类范围内的变量,名为currentLocation,类型为CLLocation,并在didUpdateToLocation方法中将currentLocation设置为new location。