如何在 iOS 中使用多个注释增加 Apple Mapkit 的缩放比例
How can i increase zoom in Apple Mapkit with multiple annotations in iOS
我试过这段代码,在这里我将 MKCoordinateSpan 增加到 200 - 200,但是我的应用程序崩溃了,有人可以帮助我吗。
func setupMap(hotelListData:[HotelListData], cityLocation:
CLLocationCoordinate2D )
{
let coordinateRegion = MKCoordinateRegion(center: cityLocation, span: MKCoordinateSpan(latitudeDelta: 200, longitudeDelta: 200))
mapView.setRegion(coordinateRegion, animated: true)
//Set Multiple Annotation
for data in hotelListData {
let annotation = MKPointAnnotation()
annotation.title = data.hotel?.name
annotation.coordinate = CLLocationCoordinate2D(latitude: Double(data.hotel?.latitude ?? 0.0), longitude: Double(data.hotel?.longitude ?? 0.0))
mapView.addAnnotation(annotation)
}
}
已更新:
我从 (latitudinalMeters & longitudinalMeters) 更改了跨度并得到了想要的结果:-
现在可以在这里找到:
i)给苹果地图设置多个Annotations。
ii)根据需要的位置调整缩放。
func setupMap(hotelListData:[HotelListData], cityLocation:
CLLocationCoordinate2D ){
let coordinateRegion = MKCoordinateRegion(center: cityLocation, latitudinalMeters: CLLocationDistance(exactly: 15000)!, longitudinalMeters: CLLocationDistance(exactly: 15000)!)
mapView.setRegion(coordinateRegion, animated: true)
//Set Multiple Annotation
for data in hotelListData {
let annotation = MKPointAnnotation()
annotation.title = data.hotel?.name
annotation.coordinate = CLLocationCoordinate2D(latitude: Double(data.hotel?.latitude ?? 0.0), longitude: Double(data.hotel?.longitude ?? 0.0))
mapView.addAnnotation(annotation)
}
}
MKCoordinateSpan
的 longitudeDelta
and latitudeDelta
以度为单位。从北极到南极只有180度的纬度,所以用200这个参数不太合理。
由于您只想在地图上显示一个城市的区域,如果您知道您的应用通常处理的城市有多大,则可以使用以米为单位的 this other initialiser。
例如,
let coordinateRegion = MKCoordinateRegion(center: cityLocation, latitudinalMeters: 30000, longitudinalMeters: 30000)
如果您的城市大小不一,或者您不知道它们有多大,那么另一种方法是计算酒店的纬度和经度范围,并用它来创建 MKCoordinateSpan
.
var minLat: CLLocationDegrees = 90
var maxLat: CLLocationDegrees = -90
var minLong: CLLocationDegrees = 180
var maxLong: CLLocationDegrees = -180
for data in hotelListData {
// ... you annotation code ...
guard let hotel = data.hotel else { continue }
if hotel.latitude < minLat { minLat = hotel.latitude }
if hotel.latitude > maxLat { maxLat = hotel.latitude }
if hotel.longitude < minLong { minLong = hotel.longitude }
if hotel.longitude > maxLong { maxLong = hotel.longitude }
}
let latRange = max(0.01, maxLat - minLat) // if the range is too small, make it at least 0.01
let longRange = max(0.01, maxLong - minLong)
let coordinateRegion = MKCoordinateRegion(
center: cityLocation,
span: MKCoordinateSpan(latitudeDelta: latRange, longitudeDelta: longRange)
我试过这段代码,在这里我将 MKCoordinateSpan 增加到 200 - 200,但是我的应用程序崩溃了,有人可以帮助我吗。
func setupMap(hotelListData:[HotelListData], cityLocation:
CLLocationCoordinate2D )
{
let coordinateRegion = MKCoordinateRegion(center: cityLocation, span: MKCoordinateSpan(latitudeDelta: 200, longitudeDelta: 200))
mapView.setRegion(coordinateRegion, animated: true)
//Set Multiple Annotation
for data in hotelListData {
let annotation = MKPointAnnotation()
annotation.title = data.hotel?.name
annotation.coordinate = CLLocationCoordinate2D(latitude: Double(data.hotel?.latitude ?? 0.0), longitude: Double(data.hotel?.longitude ?? 0.0))
mapView.addAnnotation(annotation)
}
}
已更新:
我从 (latitudinalMeters & longitudinalMeters) 更改了跨度并得到了想要的结果:-
现在可以在这里找到:
i)给苹果地图设置多个Annotations。
ii)根据需要的位置调整缩放。
func setupMap(hotelListData:[HotelListData], cityLocation:
CLLocationCoordinate2D ){
let coordinateRegion = MKCoordinateRegion(center: cityLocation, latitudinalMeters: CLLocationDistance(exactly: 15000)!, longitudinalMeters: CLLocationDistance(exactly: 15000)!)
mapView.setRegion(coordinateRegion, animated: true)
//Set Multiple Annotation
for data in hotelListData {
let annotation = MKPointAnnotation()
annotation.title = data.hotel?.name
annotation.coordinate = CLLocationCoordinate2D(latitude: Double(data.hotel?.latitude ?? 0.0), longitude: Double(data.hotel?.longitude ?? 0.0))
mapView.addAnnotation(annotation)
}
}
MKCoordinateSpan
的 longitudeDelta
and latitudeDelta
以度为单位。从北极到南极只有180度的纬度,所以用200这个参数不太合理。
由于您只想在地图上显示一个城市的区域,如果您知道您的应用通常处理的城市有多大,则可以使用以米为单位的 this other initialiser。
例如,
let coordinateRegion = MKCoordinateRegion(center: cityLocation, latitudinalMeters: 30000, longitudinalMeters: 30000)
如果您的城市大小不一,或者您不知道它们有多大,那么另一种方法是计算酒店的纬度和经度范围,并用它来创建 MKCoordinateSpan
.
var minLat: CLLocationDegrees = 90
var maxLat: CLLocationDegrees = -90
var minLong: CLLocationDegrees = 180
var maxLong: CLLocationDegrees = -180
for data in hotelListData {
// ... you annotation code ...
guard let hotel = data.hotel else { continue }
if hotel.latitude < minLat { minLat = hotel.latitude }
if hotel.latitude > maxLat { maxLat = hotel.latitude }
if hotel.longitude < minLong { minLong = hotel.longitude }
if hotel.longitude > maxLong { maxLong = hotel.longitude }
}
let latRange = max(0.01, maxLat - minLat) // if the range is too small, make it at least 0.01
let longRange = max(0.01, maxLong - minLong)
let coordinateRegion = MKCoordinateRegion(
center: cityLocation,
span: MKCoordinateSpan(latitudeDelta: latRange, longitudeDelta: longRange)