简体   繁体   English

使用Swift和Market查找附近的酒吧

[英]Find nearby bars using swift and market

I have this piece of code and I want to save the locations which I get from the MKLocalSearch in an array, so I can use them later. 我有这段代码,我想将从MKLocalSearch获得的位置保存在一个数组中,以便以后使用。 Do you have any ideas how I can do that? 您有任何想法我该怎么做吗?

func searchForBarsAndRestaurants(searchFor: String){
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = typeOfPlace   //or whatever you're searching for

    request.region = MKCoordinateRegionMakeWithDistance(location.coordinate, 300, 300)


    let search = MKLocalSearch(request: request)
    search.start { response, error in
        guard let response = response
        else {
            print("There was an error searching for: \(String(describing: request.naturalLanguageQuery)) error: \(String(describing: error))")
            return
        }

        print(response.mapItems.count)
        print("There are \(response.mapItems.count)" , searchFor)
        for item in response.mapItems {
            // You may be able to match the address to what the geoCode gives you
            // or present the user with a list of options
            print("\(String(describing: item.name))")
            var totalDistances: Array<Double> = Array()
            let distance = self.location.distance(from: item.placemark.location!)
            totalDistances += [distance]
            print("distance is " ,distance)
            print(totalDistances.count)
        }
    }
}

Sure, you've just to use a singleton. 当然,您只需要使用一个单例。

What you want is use a global variable. 您想要的是使用全局变量。 Here is an example of how to do that, w 这是一个如何做到这一点的例子,w

let sharedNetworkManager = NetworkManager(baseURL: API.baseURL)

class NetworkManager {

    // MARK: - Properties

    let baseURL: URL

    // Initialization

    init(baseURL: URL) {
        self.baseURL = baseURL
    }

}

You need to define totalDistances as Class Property. 您需要将totalDistances定义为Class Property。

var totalDistances: Array<Double> = Array()

func searchForBarsAndRestaurants(searchFor: String){
    let request = MKLocalSearchRequest()
    request.naturalLanguageQuery = typeOfPlace   //or whatever you're searching for

    request.region = MKCoordinateRegionMakeWithDistance(location.coordinate, 300, 300)


    let search = MKLocalSearch(request: request)
    search.start { response, error in
        guard let response = response
        else {
            print("There was an error searching for: \(String(describing: request.naturalLanguageQuery)) error: \(String(describing: error))")
            return
        }

        print(response.mapItems.count)
        print("There are \(response.mapItems.count)" , searchFor)
        for item in response.mapItems {
            // You may be able to match the address to what the geoCode gives you
            // or present the user with a list of options
            print("\(String(describing: item.name))")
            let distance = self.location.distance(from: item.placemark.location!)
            self.totalDistances += [distance]
            print("distance is " ,distance)
            print(self.totalDistances.count)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM