【问题标题】:How do I display an annotation using the Users current location when a button is clicked?单击按钮时,如何使用用户当前位置显示注释?
【发布时间】:2015-07-13 02:59:46
【问题描述】:

所以我正在创建一个应用程序,当单击按钮时,用户当前位置用于在 Apples 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。

【问题讨论】:

    标签: ios swift gps annotations mapkit


    【解决方案1】:

    你快到了!

    • 首先,在代码顶部(就在类中)创建一个注解:

      var annotation = MKPointAnnotation()

    • 然后,使用newLocation.coordinate.latitude.longitude 创建CLLocationCoordinate2D 并将其设置为annotation.coordinate

      • 然后使用map.title = "something"map.addAnnotation(annotation)

      • 1234563 /p>

    【讨论】:

    • 好的,这就是我所拥有的:
    • import UIKit import CoreLocation import MapKit class ViewController: UIViewController, CLLocationManagerDelegate { var locationManager = CLLocationManager() //新代码添加 var annotation = MKPointAnnotation() // @IBOutlet var Map: MKMapView! override func viewDidLoad() { super.viewDidLoad() // 在加载视图后做任何额外的设置,通常是从一个笔尖。 locationManager.delegate = self locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() }
    • override 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 let span = MKCoordinateSpanMake(0.0009, 0.0009)
    • 让 region = MKCoordinateRegion(center: newLocation.coordinate, span: span) Map.setRegion(region, animated: false) } }
    • OK,这样代码就可以不断地将地图集中在用户的位置上。要设置注释,首先设置一个名为 currentLocation 类型的 CLLocation 类范围的变量,然后在 didUpdateToLocation 方法中将 currentLocation 设置为新位置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多