我有一个地址,我正在尝试使用该地址显示在带有引脚的mapview上而不是实际坐标。我可以得到地图以显示引脚但它在我的位置上没有足够的缩放。无论如何我可以放大位置吗?提前致谢
@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)
}
}
}
}
答案 0 :(得分:1)
使用此扩展程序可能对您的需求有所帮助
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)
}
}
}
}
希望这有帮助
答案 1 :(得分:1)
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
}
答案 2 :(得分:0)
选择你的mapkit并进入检查编辑器和点击缩放选项而不是它的工作...