MapKit zoom to user current location

后端 未结 8 531
慢半拍i
慢半拍i 2021-02-01 17:59

I am trying to simply show user\'s location on the map, but I need to when app launches, the map should zoom to current location ,but I don\'t know why map doesn\'t zoom at all

8条回答
  •  北荒
    北荒 (楼主)
    2021-02-01 18:31

    In swift 4.1. To change the Zoom level you need to change the span value i.e MKCoordinateSpan(latitudeDelta: 0.95, longitudeDelta: 0.95)

     let lat =  "33.847105"
        let long = "-118.2673272"
        let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: Double(lat)!, longitude: Double(long)!), span: MKCoordinateSpan(latitudeDelta: 0.95, longitudeDelta: 0.95))
        DispatchQueue.main.async {
            self.mapView.setRegion(region, animated: true)
        }
    

    Swift 5.0

    let span = MKCoordinateSpan.init(latitudeDelta: 0.01, longitudeDelta:
    0.01)
    let coordinate = CLLocationCoordinate2D.init(latitude: 21.282778, longitude: -157.829444) // provide you lat and long 
    let region = MKCoordinateRegion.init(center: coordinate, span: span)
    mapView.setRegion(region, animated: true)
    

提交回复
热议问题