我是swift(一般的iOS编程)的新手,我正在试图弄清楚如何缩放地图以适应地图上的2个点。
目前我有
var zoomRect = MKMapRectNull;
var myLocationPointRect = MKMapRectMake(myLocation.longitude, myLocation.latitude, 0, 0)
var currentDestinationPointRect = MKMapRectMake(currentDestination.longitude, currentDestination.latitude, 0, 0)
zoomRect = myLocationPointRect;
zoomRect = MKMapRectUnion(zoomRect, currentDestinationPointRect);
什么也没做。
我是否必须以某种方式将zoomRect
应用于地图?
MKMapRectUnion
计算并返回一个新的rect,仅此而已。您需要告诉mapView将其可见区域设置为新的rect:
myMapView.setVisibleMapRect(zoomRect, animated: true)
self.mapView.showAnnotations(self.mapView.annotations, animated: true)
对于swift 3,iOS 10
首先获取当前位置,然后使用回调didUpdateLocations配置地图。在视图控制器中添加以下代码:
@IBOutlet weak var mapView: MKMapView!
private var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if CLLocationManager.locationServicesEnabled() {
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
}
}
public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let last = newLocation {
let userlongitude = last.coordinate.longitude
let userlatitude = last.coordinate.latitude
let newDistance = CLLocation(latitude: userlatitude, longitude: userlongitude).distance(from: CLLocation(latitude: YourAnnotation.coordinate.latitude, longitude: YourAnnotation.coordinate.longitude))
let region = MKCoordinateRegionMakeWithDistance(last.coordinate, 2 * newDistance, 2 * newDistance)
let adjustRegion = self.mapView.regionThatFits(region)
self.mapView.setRegion(adjustRegion, animated:true)
}
}
使用以下MKMapView扩展 -
extension MKMapView
{
func fitAllMarkers(shouldIncludeCurrentLocation: Bool) {
if !shouldIncludeCurrentLocation
{
showAnnotations(annotations, animated: true)
}
else
{
var zoomRect = MKMapRectNull
let point = MKMapPointForCoordinate(userLocation.coordinate)
let pointRect = MKMapRectMake(point.x, point.y, 0, 0)
zoomRect = MKMapRectUnion(zoomRect, pointRect)
for annotation in annotations {
let annotationPoint = MKMapPointForCoordinate(annotation.coordinate)
let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0)
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect)
}
}
setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(8, 8, 8, 8), animated: true)
}
}
}
-[MKMapView showAnnotations:animated:]
正如@lveselovsky所说
self.mapView.showAnnotations(self.mapView.annotations, animated: true)
工作得很好。
如果要在加载地图之前更新UI,可以将其放在委托方法中,如下所示:
func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) {
guard !mapZoomUpdatedOnce else {
return
}
self.mapView.showAnnotations(self.mapView.annotations, animated: true)
self.mapZoomUpdatedOnce = true
}
布尔值只是为了确保一旦它第一次更新,如果用户导航到地图上的其他地方,它就不会再次更新:)
Swift 3在地图中填入所有注释。这是正确的方法。
func zoomMapaFitAnnotations() {
var zoomRect = MKMapRectNull
for annotation in mapview.annotations {
let annotationPoint = MKMapPointForCoordinate(annotation.coordinate)
let pointRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0, 0)
if (MKMapRectIsNull(zoomRect)) {
zoomRect = pointRect
} else {
zoomRect = MKMapRectUnion(zoomRect, pointRect)
}
}
self.mapview.setVisibleMapRect(zoomRect, edgePadding: UIEdgeInsetsMake(50, 50, 50, 50), animated: true)
}