【问题标题】:Zoom and current location not showing in map swift缩放和当前位置未快速显示在地图中
【发布时间】:2016-01-13 20:19:57
【问题描述】:

我正在开发一个简单的应用程序,其中将显示地图和一些标记。我还希望在此应用中显示用户的当前位置。 到目前为止,这是我的代码:

import UIKit
import MapKit
import CoreLocation

class HomePageViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var templabelforlocationtesting: UILabel!
    @IBOutlet weak var map: MKMapView!
    let locationManager = CLLocationManager()
    @IBOutlet weak var menuButton: UIBarButtonItem!

    override func viewDidLoad() {
        super.viewDidLoad()

        if self.revealViewController() != nil {
            menuButton.target = self.revealViewController()
            menuButton.action = "revealToggle:"
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }

        // Do any additional setup after loading the view.
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.startUpdatingLocation()
        self.map.showsUserLocation = true

        //temporary nearby locations list

        //temple tooth
        let templeToothMarker = MKPointAnnotation()
        let templeToothLocation = CLLocationCoordinate2D(latitude: 7.294715, longitude: 80.639858)
        templeToothMarker.coordinate = templeToothLocation
        templeToothMarker.title = "Kandy Dalada Maligawa"
        templeToothMarker.subtitle = "Historic Religious place"

        //botanical gardens

        let botanicalGardenMarker = MKPointAnnotation()
        let botanicalGardenLocation = CLLocationCoordinate2D(latitude: 7.273346, longitude: 80.595140)
        botanicalGardenMarker.coordinate = botanicalGardenLocation
        botanicalGardenMarker.title = "Royal Botanical Gardens"
        botanicalGardenMarker.subtitle = "Botanical Garden in Kandy"

        //load markers to map
        map.addAnnotations([templeToothMarker, botanicalGardenMarker])

    }

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations.last! as CLLocation

        let centerLocation = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
        templabelforlocationtesting.text = "Location: " + String(location.coordinate.latitude) + " , " + String(location.coordinate.longitude)

        let mapSpan = MKCoordinateSpanMake(0.06, 0.06)

        let mapRegion = MKCoordinateRegion(center: centerLocation, span: mapSpan)

        self.map.setRegion(mapRegion, animated: true)
        self.locationManager.stopUpdatingLocation()
    }


}

地图正在运行并且标记正在显示,但以下问题仍然存在。

  1. 我无法让地图放大到更高的级别(到let mapSpan = MKCoordinateSpanMake(0.06, 0.06) 中设置的级别)

  2. 应获取用户的当前位置并将其设置为地图的中心,并且还应显示为带有注释的当前位置。为了检查位置是否已加载,我也设置了一个标签,但这也不起作用。

我在这里做错了什么?

【问题讨论】:

  • 如果你在 放置一个断点,让 location = locations.last!作为 CLLocation 你得到什么值?

标签: ios swift mapkit


【解决方案1】:

这个代码结构总是错误的:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    // ...
    self.locationManager.stopUpdatingLocation()
}

问题在于,在您获得可用位置之前,您需要多次调用locationManager:didUpdateLocations:。但是您在 first 调用之后无条件停止,这几乎肯定是无用的。

此外,如果您将地图告诉self.map.showsUserLocation = true,则永远不应该告诉地图self.map.setRegion(mapRegion, animated: true)。如果地图的showsUserLocationtrue,地图将设置自身 以显示正确的区域。事实上,如果您只想将地图设置为用户的位置,那么您根本不应该使用位置管理器来更新位置。让地图视图完成所有工作。

【讨论】:

  • 我也使用 self.map.setRegion(mapRegion, animated: true)showsUserLocation = true 但我没有在第一次启动时设置用户的当前位置
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-06
  • 2013-01-06
  • 2015-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多