Swift zoom in on location

Deadly 提交于 2019-12-12 04:54:34

问题


I have an address and I'm trying to use that address to show up on a mapview with the pin instead of having the actual coordinates. I can get it the map to show up with the pin but it is not zooming close enough on my location. Is there anyway I can zoom in on the location? Thanks in advance

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    let address = (community!["address"] as AnyObject)

    let location : String = address as! String
    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(location) { (placemarks, error) in
        if let placemarks = placemarks {
            if placemarks.count != 0 {
                let annotation = MKPlacemark(placemark: placemarks.first!)
                self.mapView.addAnnotation(annotation)


            }
        }
    }
}

回答1:


Use this extension, can be helpful for your needs

extension MKMapView {
  func zoomToUserLocation() {
     self.zoomToUserLocation(latitudinalMeters: 1000, longitudinalMeters: 1000)
  }

  func zoomToUserLocation(latitudinalMeters:CLLocationDistance,longitudinalMeters:CLLocationDistance)
  {
    guard let coordinate = userLocation.location?.coordinate else { return }
    self.zoomToLocation(location: coordinate, latitudinalMeters: latitudinalMeters, longitudinalMeters: longitudinalMeters)
  }

  func zoomToLocation(location : CLLocationCoordinate2D,latitudinalMeters:CLLocationDistance = 100,longitudinalMeters:CLLocationDistance = 100)
  {
    let region = MKCoordinateRegionMakeWithDistance(location, latitudinalMeters, longitudinalMeters)
    setRegion(region, animated: true)
  }

}

Use in your code like this

@IBOutlet weak var mapView: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    let address = (community!["address"] as AnyObject)

    let location : String = address as! String
    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(location) { (placemarks, error) in
        if let placemarks = placemarks {
            if placemarks.count != 0 {
                let annotation = MKPlacemark(placemark: placemarks.first!)
                self.mapView.addAnnotation(annotation)
                //Using it
                self.mapView.zoomToLocation(location: annotation.coordinate)

            }
        }
    }
}

Hope this helps




回答2:


In viewcontoller.m

func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
    if Is_Centered == false {
        Map.setCenter(userLocation.coordinate, animated: true)
        Is_Centered = true
        OperationQueue.mainQueue.addOperation({() -> Void in
            self.setZoomOnMap(userLocation.coordinate, map: Map)
        })
    }
}


func setZoomOnMap(_ location: CLLocationCoordinate2D, map mapName: MKMapView) {
    let region: MKCoordinateRegion
    let span1: MKCoordinateSpan
    span1.latitudeDelta = 0.002
    // change as per your zoom level
    span1.longitudeDelta = 0.005
    region.span = span1
    region.center = location
    mapName.setRegion(region, animated: true)
    mapName.regionThatFits(region)
    mapName.isZoomEnabled = true
}



回答3:


select your mapkit and go in inspect editor and zooming option clicked than its work...



来源:https://stackoverflow.com/questions/45250752/swift-zoom-in-on-location

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!