放大用户位置 - Swift

Cai*_*lin 5 mapkit ios swift

我无法弄清楚如何让地图从viewDidLoad放大用户位置.我尝试设置一个区域,但这不起作用.这是我的代码,任何提示?

@IBOutlet弱var mapView:MKMapView!var MapViewLocationManager:CLLocationManager!= CLLocationManager()var currentLocation:PFGeoPoint!= PFGeoPoint()

override func viewDidLoad() {
    super.viewDidLoad()
    self.mapView.showsUserLocation = true
    mapView.delegate = self
   MapViewLocationManager.delegate = self
    mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
}
Run Code Online (Sandbox Code Playgroud)

我已经查找了这个问题的答案但是没有在Swift中找到答案或者确实有效.谢谢!!

rb6*_*612 16

我会尝试这样的事情:

    let latitude:CLLocationDegrees = //insert latitutde

    let longitude:CLLocationDegrees = //insert longitude

    let latDelta:CLLocationDegrees = 0.05

    let lonDelta:CLLocationDegrees = 0.05

    let span = MKCoordinateSpanMake(latDelta, lonDelta)

    let location = CLLocationCoordinate2DMake(latitude, longitude)

    let region = MKCoordinateRegionMake(location, span)

    mapView.setRegion(region, animated: false)
Run Code Online (Sandbox Code Playgroud)

我看到你说你试过设置一个区域.也许尝试这样做.


Ami*_*itP 7

在大多数应用中,都会实现"当前位置"按钮.一个简单的方法是这样的:

@IBAction func myLocationButtonTapped(_ sender: Any) {

    mapView.showsUserLocation = true
    mapView.setUserTrackingMode(.follow, animated: true)

}
Run Code Online (Sandbox Code Playgroud)

用户平移或缩放地图后,跟踪将停止.所以这对我来说已经足够了.


Jat*_*tel 6

你必须试试这个.它会缩放地图.

let span = MKCoordinateSpanMake(0.050, 0.050)
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 23.0225, longitude: 72.5714), span: span)
mapView.setRegion(region, animated: true)`
Run Code Online (Sandbox Code Playgroud)