如何使用 MapKit 让我的应用放大到我的位置

Posted

技术标签:

【中文标题】如何使用 MapKit 让我的应用放大到我的位置【英文标题】:How to make my app zoom into my location using MapKit 【发布时间】:2016-08-17 01:22:21 【问题描述】:

我尝试制作一个像 uber 这样的应用程序,该应用程序应该放大您的位置并显示一个圆圈来显示您的位置。它正在显示地图但它没有用蓝点显示确切的位置......

这是我的代码。

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate 

    @IBOutlet weak var mapView: MKMapView!

    let locationManager = CLLocationManager()



    override func viewDidLoad() 
        super.viewDidLoad()
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.stopUpdatingLocation()
        self.mapView.showsUserLocation = true
    

    override func didReceiveMemoryWarning() 
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) 
        let location = locations.last
        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
        self.mapView.setRegion(region, animated: true)
        self.locationManager.stopUpdatingLocation()

    

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) 
        print("Errors" + error.localizedDescription)
    

感谢您的帮助。

【问题讨论】:

【参考方案1】:

在 Interface builder 中,选择 mapView 并在 Attributes Inspector 中,选中“Show user's location”复选框,您应该已经设置好了。

【讨论】:

【参考方案2】:

位置管理器工作的必要条件之一是在 info.plist 中添加标签“NSLocationAlwaysUsageDescription”或“NSLocationWhenInUseUsageDescription”。这些只是应用程序要求您获得使用权限时所说的内容。

【讨论】:

以上是关于如何使用 MapKit 让我的应用放大到我的位置的主要内容,如果未能解决你的问题,请参考以下文章

如何放大用户当前位置?

Swift mapkit未指向位置

iPhone Mapkit 蓝点/用户位置区域缩放问题

为啥我的 MapKit 中的蓝点(用户位置)会移动?

我的 Swift MapKit 注释没有显示

不需要导入 MapKit.framework?