【问题标题】:How do I make "Center location on Map" work?如何使“地图上的中心位置”起作用?
【发布时间】:2017-02-14 17:55:08
【问题描述】:

我是编程新手。 我找到了下面的代码,它除了中心位置之外什么都做。(放大、地图、蓝点都可以。) 如果我在模拟器中运行,(城市运行)蓝点就会跑出页面。

import UIKit
import MapKit
import CoreLocation
import Foundation

class ViewController: UIViewController, CLLocationManagerDelegate {
    @IBOutlet weak var map: MKMapView!

    var locationManager: CLLocationManager?

    override func viewDidLoad() {
        super.viewDidLoad()

        locationManager = CLLocationManager()
        locationManager!.delegate = self

        map.showsUserLocation = true

        if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
            locationManager!.startUpdatingLocation()
        } else {
            locationManager!.requestWhenInUseAuthorization()
        }
    }

    private func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        switch status {
        case .notDetermined:
            print("NotDetermined")
        case .restricted:
            print("Restricted")
        case .denied:
            print("Denied")
        case .authorizedAlways:
            print("AuthorizedAlways")
        case .authorizedWhenInUse:
            print("AuthorizedWhenInUse")
            locationManager!.startUpdatingLocation()
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let location = locations.first!
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500)
        map.setRegion(coordinateRegion, animated: true)
        locationManager?.stopUpdatingLocation()
        locationManager = nil
    }
}

【问题讨论】:

  • 为什么不在地图视图上使用userTrackingMode 属性?
  • 谢谢 Xoudini 我是编程新手,对 userTrackingMode 一无所知,所以我不知道如何使用它。
  • yourMapView.userTrackingMode = .follow 在定位服务开启时自动将用户居中,参见documentation

标签: ios swift core-location


【解决方案1】:

删除你的实现

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

只要获得授权,设置map.showsUserLocation = true,然后退后。

【讨论】:

    【解决方案2】:

    这是我的 Swift 3 代码,它可以工作并经过测试。它加载 mapView,询问用户的许可并放大到他的位置。

    import UIKit
    import MapKit
    
    class ViewController: UIViewController, CLLocationManagerDelegate {
    @IBOutlet weak var mapView: MKMapView!
    
    let locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
        locationManager.activityType = .automotiveNavigation
        locationManager.distanceFilter = 10.0
        mapView.showsUserLocation = true
        mapView.mapType = .standard
        self.mapView.delegate = self
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }
    
    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0] as CLLocation
        locationManager.stopUpdatingLocation()
        let location = CLLocationCoordinate2D(latitude: userLocation.coordinate.latitude, longitude: userLocation.coordinate.longitude)
        let span = MKCoordinateSpanMake(0.05, 0.05)
        let region = MKCoordinateRegion (center:  location,span: span)       
        mapView.setRegion(region, animated: true)
    }
    }
    

    【讨论】:

    • 谢谢 Mohsen 我在复制和粘贴您的代码时出错 无法将“ViewController”类型的值分配给“MKMapViewDelegate?”类型的值?
    • 将 MKMapViewDelegate 作为协议添加到您的 viewController,或从您的代码中删除 mapView.delegate = self,您应该一切顺利,让我知道会发生什么
    • 或 map.delegate = self,因为您在代码中将其命名为 map。
    猜你喜欢
    • 1970-01-01
    • 2012-10-18
    • 2013-07-19
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多