gpt4 book ai didi

ios - 我可以在不使用 map 的情况下在 Swift 中进行位置搜索吗?

转载 作者:行者123 更新时间:2023-11-28 06:22:14 25 4
gpt4 key购买 nike

我试图在 Swift 3.0 中创建的是一个简单的位置搜索,它基于当前位置(或指定位置),用于预定义的范围(例如 100 公里)。我不想要超出该范围的任何结果。我不想看 map ,不想在上面放任何图钉等等。只想搜索,然后返回结果。这可能与 mapkit 吗?我找到的每个示例或教程都从 map 开始,并使用 map 获取区域/跨度,然后将结果返回到 map 。在尝试其中一些示例时,搜索不限于 map 区域,因为我在其他国家/地区获得了结果。我想要的是只返回选定位置的单屏搜索。没有 map 。没有来自世界另一端的结果。我应该使用 mapkit 以外的东西吗?或者我可以在没有 mapview 的情况下完成这个吗?

编辑:根据@Rob 提供的信息,我到这里...

extension LocationSearchTable : UISearchResultsUpdating {

func updateSearchResults(for searchController: UISearchController) {
var localRegion: MKCoordinateRegion
let distance: CLLocationDistance = 1200

let currentLocation = CLLocationCoordinate2D.init(latitude: 50.412165, longitude: -104.66087)

localRegion = MKCoordinateRegionMakeWithDistance(currentLocation, distance, distance)

print(localRegion)

guard let searchBarText = searchController.searchBar.text else { return }

let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchBarText
request.region = localRegion
let search = MKLocalSearch(request: request)

search.start { response, _ in
guard let response = response else {
return
}
self.matchingItems = response.mapItems
self.tableView.reloadData()
}
}
}

不过,我还是有点迷糊。它似乎仍然带来了来自各地的结果。例如,如果我要根据位于 SK 里贾纳的某个位置查找地址 19 Rendek Crescent,以下是我键入的结果:

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

为什么返回 Regina 以外的地点,为什么 Rae St 和 Read Ave 在 Rendek 之前出现?

最佳答案

您首先要创建一个位置类来让您的单元格井井有条。 (不要忘记将 MapKit 导入这些 View Controller )

import UIKit
import MapKit

class Location: NSObject {
var locationName: String!
var location: String!

init(locationName: String, location: String) {
self.locationName = locationName
self.location = location
}
}

然后,创建一个数组,将您的数据存储在 TableViewController 中:

var locations = [Location]()

您还需要在单元类中配置单元函数。

func configureLocationCell(locationName: String, location: String) {
self.myLocationNameLabel.text = locationName
self.myLocationLabel.text = location
}

然后,执行 MKLocalSearch 请求以查找您的位置:(将其放入文本字​​段中确实更改了函数)

func performSearchRequest() {
let request = MKLocalSearchRequest()
let locationManager = CLLocationManager()
guard let coordinate = locationManager.location?.coordinate else { return }

request.naturalLanguageQuery = "\(myTextField.text)"
request.region = MKCoordinateRegionMakeWithDistance(coordinate, 3200, 3200)
// about a couple miles around you

MKLocalSearch(request: request).start { (response, error) in
guard error == nil else { return }
guard let response = response else { return }
guard response.mapItems.count > 0 else { return }

let randomIndex = Int(arc4random_uniform(UInt32(response.mapItems.count)))
let locationItems = response.mapItems[randomIndex]

for location in response.locationItems {
let location = Location(locationName: location.name!, location: "\(gyms.placemark.subLocality!)", "\(gyms.placemark.locality!)")
self.locations.append(location)
self.myTableView.reloadData()
}
}
}

最后,在您的 TableViewController number of cells 函数中,返回 locations.count

最后一件事:在您的 cell for row at index path 函数中,通过键入以下内容访问数据:

let location = locations[indexPath.row]
cell.configureLocationCell(locationName: location.locationName, location: location)

关于ios - 我可以在不使用 map 的情况下在 Swift 中进行位置搜索吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43052172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com