【发布时间】:2018-01-12 14:38:02
【问题描述】:
我正在尝试使用 MapKit 框架创建一个 MapView,该框架查找当前用户位置并显示附近的餐馆。但是,当我尝试显示注释时,它们不会出现。
我已尝试打印 (response.mapItems) 以检查它是否有效,结果很好,因为它打印了有关在控制台中找到的一些餐馆的信息。
因此,我不知道为什么这些没有在地图上标注。
这是我制作的代码:
import UIKit
import MapKit
import CoreLocation
class RestaurantViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.025, 0.025)
let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
map.setRegion(region, animated: true)
self.map.showsUserLocation = true
}
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = "restaurant"
request.region = map.region
let search = MKLocalSearch(request: request)
search.start { (response, error) in
guard let response = response else {
return
}
for item in response.mapItems {
print(response.mapItems) - Console shows some restaunrant outputs that were correctly fetched
let annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
DispatchQueue.main.async {
self.map.addAnnotation(annotation)
}
}
}
}
}
【问题讨论】:
-
response.mapItems和用户位置的距离是分开的吗?
标签: ios swift xcode geolocation mapkit