如何保存用户位置并使用图钉(或其他标记)将其显示在地图上?

How do I save a users location and display it on a map using a pin (or some other marker)?

我想要做的是有一个按钮,用户可以单击该按钮来保存他们的当前位置。然后,在地图上,一个图钉会出现在保存位置所在的位置。我该怎么做呢?我已经搜索了一些示例代码,但找不到任何有效的代码或在 Swift.

我有它,所以用户也可以随时看到他们所在的位置。下面是我目前的代码。

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)
}
}

为了实现我在第一段中描述的目标,我应该向这段代码添加什么?

问得好!

尝试添加一个数组,在 IBAction 发生时存储 newLocation.coordinate。然后,您可以使用此示例代码作为示例来设置引脚:

override func viewDidLoad() {
    super.viewDidLoad()

    // Set map view delegate with controller
    self.mapView.delegate = self

    let newYorkLocation = CLLocationCoordinate2DMake(40.730872, -74.003066)
    // Drop a pin
    let dropPin = MKPointAnnotation()
    dropPin.coordinate = newYorkLocation
    dropPin.title = "New York City"
    mapView.addAnnotation(dropPin)
}

但随后只需将 dropPin.coordinate 设置为存储在数组中的值。

好的,首先你要获得用户的许可:

  • 添加 CoreLocation 框架
  • 通过添加 NSLocationWhenInUseUsageDescription
  • 更新 info.plist
  • 您在 viewDidLoad 中的代码是正确的
  • 您可以使用更简单的方法:

    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { }
    
  • 在里面,设置:

    var userLocation : CLLocation = locations[0] as! CLLocation

  • 现在使用 userLocation.latitude.longitude

  • 创建一个 CLLocationCoordinate2D
  • MKPointAnnotation()创建注释,并将annotation.coordinate设置为上面的CLLocationCoordinate2D

  • 如果需要设置 .title

  • map.addAnnotation(annotation)

这实际上会在每次更新用户位置时添加一个图钉 - 因此您可能需要考虑删除之前的图钉。此外,如果您每半秒左右获得一次更新,它也不是很有效,因此请考虑简单地将 mapView 设置为在每次更新时显示 region,而不是图钉。

希望对您有所帮助!

这样试试:

  • findUserLocationAndDropPin() 创建按钮操作:
  • 捕获 userLocationCoordinates 使用 CLLocationCoordinate2DMake();
  • 使用 MKPointAnnotation() 创建您的 pinForUserLocation
  • 将你的pinForUserLocation.coordinate赋值为 userLocationCoordinates;
  • 得到你的 mapViewaddAnnotation;
  • 最后问 mapViewshowAnnotations

这段代码就是我的意思并且应该这样做,至少在 iOS 8.4 之前:

@IBAction func findUserLocationAndDropPin(sender: UIButton) {
        var userLocationCoordinates = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude)
        var pinForUserLocation = MKPointAnnotation()
        pinForUserLocation.coordinate = userLocationCoordinates
        mapView.addAnnotation(pinForUserLocation)
        mapView.showAnnotations([pinForUserLocation], animated: true)
    }