我有一个具有 SwiftUI 生命周期的 SwiftUI 应用程序。我已将 MKMapView 设置为 UIViewRepresentable。对于其中一个功能,用户点击地图上的两个地方并输入搜索条件以在由两个点击所界定的区域中查找地点。
地图有效,水龙头的注释有效,我确实从 MKLocalSearch 获得了结果,但结果不完整,不反映我在请求中设置的区域。充其量,我从该地区的大致中心得到一组结果。边缘很少或没有。我是创建圆形区域还是矩形区域似乎并不重要。我把这个区域做得多大似乎并不重要。我根本没有在终点得到有效的结果。
除了显示问题的要点外,我已经删除了所有内容,因此可以运行此代码进行演示。将光标放在文本字段中并点击返回,然后点击齿轮将结果打印到控制台
这是地图:
struct MapView: UIViewRepresentable {
typealias UIViewType = MKMapView
@State private var myMapView: MKMapView?
@State private var sfCoord = CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194)
class Coordinator: NSObject, MKMapViewDelegate {
var control: MapView
init(_ control: MapView) {
self.control = control
}
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
if let annotationView = views.first {
if let annotation = annotationView.annotation {
if annotation is MKUserLocation {
let region = MKCoordinateRegion(center: annotation.coordinate, latitudinalMeters: 5000, longitudinalMeters: 5000)
mapView.setRegion(region, animated: true)
}//if user
}//if annotation
}//if views first
}//did add
}//class
func makeUIView(context: Context) -> MKMapView {
let map = MKMapView()
map.showsUserLocation = true
map.delegate = context.coordinator
DispatchQueue.main.async {
self.myMapView = map
}
return map
}//make ui view
func makeCoordinator() -> Coordinator {
Coordinator(self)
}//coord
func updateUIView(_ view: MKMapView, context: Context) {
}//update ui view
}//struct
以及带有支持文件的 ContentView:
struct ContentView: View {
@State private var search = "Coffee"
@State private var myLandmarks: [MyLandmark] = [MyLandmark]()
@State private var mapView: MapView?
var body: some View {
NavigationView {
ZStack(alignment: .top) {
MapView()
.ignoresSafeArea()
TextField("Search", text: $search, onEditingChanged: { _ in })
{
self.getNearbyMyLandmarks()
}
.textFieldStyle(RoundedBorderTextFieldStyle())
.padding()
.offset(y: -20)
} //z
.navigationBarItems(leading: Button(action: {
self.printMyLandmarks()
}) {
Image(systemName: "gear")
.resizable()
.frame(width: 25, height: 25, alignment: .center)
}, trailing: Button(action: {
self.search = "Coffee"
}) {
Image(systemName: "arrow.counterclockwise.circle")
.resizable()
.frame(width: 25, height: 25, alignment: .center)
}
)//nav bar items
}//nav
.navigationViewStyle(StackNavigationViewStyle())
}//body
}// content view
private extension ContentView {
private func getNearbyMyLandmarks() {
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = search
let sfRegion = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194), latitudinalMeters: 100000, longitudinalMeters: 100000)
request.region = sfRegion
let search = MKLocalSearch(request: request)
search.start { (response, error) in
if let response = response {
let mapItems = response.mapItems
self.myLandmarks = mapItems.map {
MyLandmark(mapItem: $0)
}
}
}
}//get nearby landmarks
//used of testing only
func printMyLandmarks() {
let sfCoord = CLLocationCoordinate2D(latitude: 37.7749, longitude: -122.4194)
for m in 0..<self.myLandmarks.count {
print(myLandmarks[m].name + ", " + distanceFromCenter(center: sfCoord, site: myLandmarks[m].coordinate))
}
}//print my landmarks
func distanceFromCenter(center: CLLocationCoordinate2D, site: CLLocationCoordinate2D) -> String {
let centerLocation = CLLocation(latitude: center.latitude, longitude: center.longitude)
let siteLocation = CLLocation(latitude: site.latitude, longitude: site.longitude)
let distanceInMeters: Double = siteLocation.distance(from: centerLocation)
let distanceString = String(format: "%.0f", distanceInMeters)
return distanceString
}//distance from center
}//extension Content View
struct MyLandmark {
let mapItem: MKMapItem
var id: UUID {
return UUID()
}
var name: String {
self.mapItem.placemark.name ?? "No Name Listed"
}
var coordinate: CLLocationCoordinate2D {
self.mapItem.placemark.coordinate
}
}//struct my landmark
控制台针对两个不同的区域跨度显示以下内容(第 119 行)
第一个 10 万米的列表:
Peet's Coffee, 31672 Hippie's Brew, 31796 The Early Bird Coffee Co, 31707 Starbucks, 31573 Anna's Family Coffee Shop, 32050 Aj's Coffee, 32959 Brewja Coffee, 26822 Starbucks, 31480 Cafe 4, 30126 Eon Coffee, 31241 Frodo Joe's Coffee & Tea, 28 Peet's Coffee, 31642 Snappy's Cafe, 31613 Starbucks, 32015 Atlas Cafe, 31855 Dunkin', 32050 Zocalo Coffeehouse, 24113 Starbucks, 31334 Pampas Cafe, 31647 Starbucks, 30666 Donut Express, 32026 Starbucks, 32827 Starbucks, 32827 Starbucks, 160370 Coffee Valley Java 2 &茶, 30348
1000 米区域的第二个列表:
SimplexiTea, 237 Blue Bottle Coffee, 371 Ritual Coffee Roasters, 455 Mavelous Coffee Bar and Little Griddle, 122 Urban Ritual, 576 All Star Cafe, 412 Peet's Coffee, 272 Mercury Cafe, 445 Cumaica, 317 Sextant Coffee Roasters, 633 Planet Cafe SF, 293 Philz Coffee, 734 Sightglass Coffee, 985 Javalencia Cafe, 115 Nina's Cafe, 219 La Boulangerie, 526 NaYa Dessert Cafe, 549 四桶咖啡, 900 Coffee Cultures, 436 V Cafe, 603 Starbucks, 309 Starbucks, 256 Johnny Doughnuts, 564 Focaccia市场和面包店,645
我希望我在这里遗漏了一些简单的东西。我收到的结果显然没有用。任何指导将不胜感激。