вот коды. Они получают банкоматы из мест Google и отображаются в виде таблицы.
/**
Calculates the distance between the user and a given location
- Parameters:
- location: ATM location coordinate
*/
func calculateDistance(location: CLLocationCoordinate2D) -> String {
let distanceLocation = CLLocation(latitude: location.latitude, longitude: location.longitude)
var distance = self.lastLocation!.distance(from: distanceLocation)
let locationInFoot = distance * 3.28084
var locationString = String(format: "%d ft", Int(locationInFoot))
if locationInFoot > 656.168 {
// Over 200ft
distance = distance / 1609.34
locationString = String(format: "%.01f miles", distance)
}
return locationString
}
class PlacesAPIManager : NSObject {
static let instance = PlacesAPIManager()
var session = URLSession.shared
/**
Used to trigger the Google Places API Request
- parameters:
- completion : Array of ATM objects
*/
func determineAtmsNearby(completion : @escaping ([ATM]) -> Void){
let placesSearchComponent = NSURLComponents()
placesSearchComponent.scheme = "https"
placesSearchComponent.host = Constants.basePlacesURL
placesSearchComponent.path = "/maps/api/place/nearbysearch/json"
// Build the query items for the api request
let radiusQueryItem = URLQueryItem(name: "radius", value: "20000")
let typeQueryItem = URLQueryItem(name: "type", value: "atm")
let apiQueryItem = URLQueryItem(name: "key", value:
Constants.APIKey)
let location = URLQueryItem(name: "location", value:
LocationManager.instance.getCoordinateString())
placesSearchComponent.queryItems = [radiusQueryItem, typeQueryItem, apiQueryItem, location]
let request = NSMutableURLRequest(url: placesSearchComponent.url!,cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
request.httpMethod = "POST"
session.dataTask(with: request as URLRequest, completionHandler: {
(data, response, error) -> Void in
var places : [ATM] = []
if let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []) as? NSDictionary {
let results = json?.object(forKey: "results") as? NSArray
for place in results! {
do {
let place = try ATM(atmDictionary: place as! NSDictionary)
places.append(place)
} catch {
completion([])
}
}
completion(places)
}
}).resume()
}
}
Банкомат класса
class ATM : NSObject, MKAnnotation {
let title : String?
let latitude : Double
let longitude : Double
var subtitle : String?
var coordinate: CLLocationCoordinate2D
init(atmDictionary : NSDictionary)throws{
// Error handling
guard let name = atmDictionary["name"] as? String else {
throw SerializationError.missing("name")
}
guard let geometry = atmDictionary["geometry"] as? NSDictionary else {
throw SerializationError.missing("gemoetry")
}
guard let location = geometry["location"] as? NSDictionary else {
throw SerializationError.missing("location")
}
guard let latitude = location["lat"] as? Double else {
throw SerializationError.missing("latitude")
}
guard let longitude = location["lng"] as? Double else {
throw SerializationError.missing("longitude")
}
// Initialise Properties
self.title = name
self.longitude = longitude
self.latitude = latitude
self.coordinate = CLLocationCoordinate2D(latitude: self.latitude, longitude: self.longitude)
// Initially this is not calculated
self.subtitle = "N/L"
}
/**
Used to update the Atm object with the distance from the user as the value for 'subtitle' property
- parameters:
- coordinate : Coordinate
*/
func updateAtmLocation(coordinate : CLLocationCoordinate2D){
self.subtitle =
LocationManager.instance.calculateDistance(location: coordinate)
}
}
СПИСОК АТМ КЛАСС
class AtmList: NSObject {
static let instance = AtmList()
var atms : [ATM] = []
func addAtm(atm : ATM){
atms.append(atm)
}
func resetAtmList(){
atms = []
}
func getAtmsCount() -> Int{
return atms.count
}
func udpateAtms(index : Int, coordinate : CLLocationCoordinate2D){
atms[index].updateAtmLocation(coordinate: coordinate)
}
}
У меня два вопроса:
- Я бы хотел расположить дисплей от самого низкого расстояния до самого высокого
- Я хотел бы получить логотип каждого банка и отобразить его рядом с каждым именем в списке табличных представлений.