快速放大位置



我有一个地址,我正在尝试使用该地址以图钉显示在地图上,而不是实际坐标。我可以让它在地图上显示图钉,但它在我的位置上缩放得不够近。无论如何,我可以放大位置吗?提前致谢

@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)

}
}
}
}

使用此扩展,可以满足您的需求

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)
}
}

像这样在代码中使用

@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)
}
}
}
}

希望这有帮助

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
}

选择您的地图套件并进入检查编辑器和缩放选项单击比其工作...

最新更新