自动调整缩放以适应 Swift 中 iOS 中的所有标记 4
Automatically adjust zoom to accommodate all markers in iOS in Swift 4
我有 name
、lon
和 lat
的字典。
原始数据
var places = [["name": "Angkor Wat", "lon": "103.8670", "lat": "13.4125"], ["name": "Pick up zone", "lon": "103.85771740610468", "lat": "13.41250067905404"], ["name": "66-35 Otto Rd", "lon": "-73.8889131530723", "lat": "40.706725952864886"], ["name": "40 Williams St", "lon": "-71.30756271909428", "lat": "42.64240728775266"], ["name": "324 Broadway Rd", "lon": "-71.29124543680823", "lat": "42.68061286243683"], ["name": "39 Kendall Pond Rd", "lon": "-71.33234704167283", "lat": "42.867489706954636"], ["name": "269 Treble Cove Rd", "lon": "-71.30049088921143", "lat": "42.55656906569715"]]
列表
代码
这是我现在的代码。
import UIKit
import MapKit
import CoreLocation
class ViewAllPinsController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var map: MKMapView!
var places = [ Dictionary<String,String>()]
override func viewDidLoad() {
super.viewDidLoad()
print(places) // it print fine
if places.count > 0 {
for i in 0..<places.count {
if let name = places[i]["name"] {
if let lat = places[i]["lat"] {
if let lon = places[i]["lon"] {
if let latitude = Double(lat) {
if let longitude = Double(lon) {
//Debug
//print(i)
//print(name)
//print(latitude)
//print(longitude)
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region = MKCoordinateRegion(center: coordinate, span: span)
self.map.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = name
self.map.addAnnotation(annotation)
}
}
}
}
}
}
}
}
}
结果
目标
我希望它们自动适合屏幕内的所有标记。
看了一个又一个帖子后,我没有看到太多关于 Swift4 的指导。一个人会怎么做这样的事情?
你可以试试
self.map.showAnnotations(self.map.annotations, animated: true)
或者如果您想自定义区域的跨度
func zoomToFitMapAnnotations(map:MKMapView)
{
if(map.annotations.count == 0)
{
return
}
var topLeftCoord = CLLocationCoordinate2D(latitude: -90, longitude: 180)
var bottomRightCoord = CLLocationCoordinate2D(latitude: 90, longitude: -180)
map.annotations.forEach {
topLeftCoord.longitude = fmin(topLeftCoord.longitude, [=11=].coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, [=11=].coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, [=11=].coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, [=11=].coordinate.latitude);
}
let resd = CLLocationCoordinate2D(latitude: topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5, longitude: topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5)
let span = MKCoordinateSpan(latitudeDelta: fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.3, longitudeDelta: fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.3)
var region = MKCoordinateRegion(center: resd, span: span);
region = map.regionThatFits(region)
map.setRegion(region, animated: true)
}
我有 name
、lon
和 lat
的字典。
原始数据
var places = [["name": "Angkor Wat", "lon": "103.8670", "lat": "13.4125"], ["name": "Pick up zone", "lon": "103.85771740610468", "lat": "13.41250067905404"], ["name": "66-35 Otto Rd", "lon": "-73.8889131530723", "lat": "40.706725952864886"], ["name": "40 Williams St", "lon": "-71.30756271909428", "lat": "42.64240728775266"], ["name": "324 Broadway Rd", "lon": "-71.29124543680823", "lat": "42.68061286243683"], ["name": "39 Kendall Pond Rd", "lon": "-71.33234704167283", "lat": "42.867489706954636"], ["name": "269 Treble Cove Rd", "lon": "-71.30049088921143", "lat": "42.55656906569715"]]
列表
代码
这是我现在的代码。
import UIKit
import MapKit
import CoreLocation
class ViewAllPinsController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var map: MKMapView!
var places = [ Dictionary<String,String>()]
override func viewDidLoad() {
super.viewDidLoad()
print(places) // it print fine
if places.count > 0 {
for i in 0..<places.count {
if let name = places[i]["name"] {
if let lat = places[i]["lat"] {
if let lon = places[i]["lon"] {
if let latitude = Double(lat) {
if let longitude = Double(lon) {
//Debug
//print(i)
//print(name)
//print(latitude)
//print(longitude)
let span = MKCoordinateSpan(latitudeDelta: 0.05, longitudeDelta: 0.05)
let coordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
let region = MKCoordinateRegion(center: coordinate, span: span)
self.map.setRegion(region, animated: true)
let annotation = MKPointAnnotation()
annotation.coordinate = coordinate
annotation.title = name
self.map.addAnnotation(annotation)
}
}
}
}
}
}
}
}
}
结果
目标
我希望它们自动适合屏幕内的所有标记。
看了一个又一个帖子后,我没有看到太多关于 Swift4 的指导。一个人会怎么做这样的事情?
你可以试试
self.map.showAnnotations(self.map.annotations, animated: true)
或者如果您想自定义区域的跨度
func zoomToFitMapAnnotations(map:MKMapView)
{
if(map.annotations.count == 0)
{
return
}
var topLeftCoord = CLLocationCoordinate2D(latitude: -90, longitude: 180)
var bottomRightCoord = CLLocationCoordinate2D(latitude: 90, longitude: -180)
map.annotations.forEach {
topLeftCoord.longitude = fmin(topLeftCoord.longitude, [=11=].coordinate.longitude);
topLeftCoord.latitude = fmax(topLeftCoord.latitude, [=11=].coordinate.latitude);
bottomRightCoord.longitude = fmax(bottomRightCoord.longitude, [=11=].coordinate.longitude);
bottomRightCoord.latitude = fmin(bottomRightCoord.latitude, [=11=].coordinate.latitude);
}
let resd = CLLocationCoordinate2D(latitude: topLeftCoord.latitude - (topLeftCoord.latitude - bottomRightCoord.latitude) * 0.5, longitude: topLeftCoord.longitude + (bottomRightCoord.longitude - topLeftCoord.longitude) * 0.5)
let span = MKCoordinateSpan(latitudeDelta: fabs(topLeftCoord.latitude - bottomRightCoord.latitude) * 1.3, longitudeDelta: fabs(bottomRightCoord.longitude - topLeftCoord.longitude) * 1.3)
var region = MKCoordinateRegion(center: resd, span: span);
region = map.regionThatFits(region)
map.setRegion(region, animated: true)
}