【问题标题】:How do I save a users location and display it on a map using a pin (or some other marker)?如何保存用户位置并使用图钉(或其他标记)将其显示在地图上?
【发布时间】:2015-07-12 01:11:37
【问题描述】:

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

我有它,所以用户也可以随时看到他们在哪里。 Bellow 是我目前拥有的代码。

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

我会在这段代码中添加什么来实现我在第一段中描述的目标?

【问题讨论】:

    标签: swift gps xcode6 mapkit core-location


    【解决方案1】:

    试试这样:

    • 为findUserLocationAndDropPin()创建您的按钮操作:
    • 捕获userLocationCoordinates 使用 CLLocationCoordinate2DMake();
    • 使用MKPointAnnotation() 创建您的pinForUserLocation;
    • 分配你的pinForUserLocation.coordinate等于 userLocationCoordinates;
    • 获取您的mapView 和addAnnotation;
    • 最后问mapView到showAnnotations。

    这段代码就是我的意思,应该这样做,至少在 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)
        }
    

    【讨论】:

    • Picture 我收到了图片中显示的这些错误。你能帮帮我吗?
    • 我检查了你的链接。要更正,您必须使用 MKMapView 变量的名称命名 mapView,记住变量的名称必须以小写字母开头。
    • Picture 感谢您这么快回复我。我试过了,我得到了这些错误。
    • 好的,在您的代码中删除名为 Map 的 IBOutlet,转到您的 Storyboard,使用鼠标左键单击您的 MAP 对象,找到 Map 出口并将其也删除。让我们重新开始,现在重新创建您的 MAP IBOutlet,将其命名为 mapView。
    • 你不知道你让我有多开心!!!!现在我还有两个问题。添加新图钉时,我需要什么代码才能删除图钉?
    【解决方案2】:

    好问题!

    当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 设置为存储在数组中的值。

    【讨论】:

      【解决方案3】:

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

      • 添加 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)

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

      希望这会有所帮助!

      【讨论】:

      • 添加新图钉后如何删除之前的图钉?
      • 您可以在顶部声明注释(因此在类中) - 并在代码之前使用 map.removeAnnotation(annotation) 来创建注释......所以你只有一个别针一次在地图上。
      猜你喜欢
      • 1970-01-01
      • 2011-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      相关资源
      最近更新 更多