Display multiple markers in the mapView - ios

I'm working with google-maps and i created this function
func addMarker(place:EClass) {
guard let coordinates = place.location else {
return
}
self.destination = coordinates
marker = GMSMarker()
marker?.position = coordinates
marker?.title = place.name
marker?.map = mapView
mapView.selectedMarker = marker
}
to add a marker in my mapView at the position of an object of type EClass; but in my case i need to show different markers on the mapView at the position of each elements of var places: [EClass] = []. To do this i tried to add something like this in my viewDidLoad
let srt = places
srt.forEach { addMarker(place: $0) }
but it doesn't working, so how can i do?
this is my class EClass
import UIKit
import CoreLocation
private let geometryKey = "geometry"
private let locationKey = "location"
private let latitudeKey = "lat"
private let longitudeKey = "lng"
private let nameKey = "name"
private let vicinityKey = "vicinity"
class EClass: NSObject {
var location: CLLocationCoordinate2D?
var name: String?
var vicinity: String?
var placeId: String
init(placeInfo:[String: Any]) {
placeId = placeInfo["place_id"] as! String
// coordinates
if let g = placeInfo[geometryKey] as? [String:Any] {
if let l = g[locationKey] as? [String:Double] {
if let lat = l[latitudeKey], let lng = l[longitudeKey] {
location = CLLocationCoordinate2D.init(latitude: lat, longitude: lng)
}
}
}
// name
name = placeInfo[nameKey] as? String
}
//UPDATE
static func getNearbyPlaces(by category:String, coordinates:CLLocationCoordinate2D, radius:Int, token: String?, completion: #escaping (QNearbyPlacesResponse?, Error?) -> Void) {
var params : [String : Any]
if let t = token {
params = [
"key" : AppDelegate.googlePlacesAPIKey,
"pagetoken" : t,
]
} else {
params = [
"key" : AppDelegate.googlePlacesAPIKey,
"radius" : radius,
"location" : "\(coordinates.latitude),\(coordinates.longitude)",
"type" : category.lowercased()
]
}
Alamofire.request(searchApiHost, parameters: params, encoding: URLEncoding(destination: .queryString)).responseJSON { response in
if let error = response.error {
completion(nil, error)
}
if let response = QNearbyPlacesResponse(dic : response.result.value as? [String : Any]) {
completion(response, nil)
}
else {
completion(nil, QNearbyPlacesResponseError.noParsingDone)
}
}
}

Related

Nil in OpenWeather API

I've just started to learn Swift and iOS and I have problem in my weather app. I'm trying to get weather data and print in console, but there is still nil and I no have idea what could be wrong. I tried to change the model, pasted all url and is still nil. Sometimes app sends data and there is no answer, but at other times is answer or no sending, no answer. In console always nil...
Generated model class:
import Foundation
class CurrentWeather : NSObject, NSCoding{
var base : String!
var clouds : Cloud!
var cod : Int!
var coord : Coord!
var dt : Int!
var id : Int!
var main : Main!
var name : String!
var sys : Sy!
var visibility : Int!
var weather : [Weather]!
var wind : Wind!
/**
* Instantiate the instance using the passed dictionary values to set the properties values
*/
init(fromDictionary dictionary: [String:Any]){
base = dictionary["base"] as? String
cod = dictionary["cod"] as? Int
dt = dictionary["dt"] as? Int
id = dictionary["id"] as? Int
name = dictionary["name"] as? String
visibility = dictionary["visibility"] as? Int
if let cloudsData = dictionary["clouds"] as? [String:Any]{
clouds = Cloud(fromDictionary: cloudsData)
}
if let coordData = dictionary["coord"] as? [String:Any]{
coord = Coord(fromDictionary: coordData)
}
if let mainData = dictionary["main"] as? [String:Any]{
main = Main(fromDictionary: mainData)
}
if let sysData = dictionary["sys"] as? [String:Any]{
sys = Sy(fromDictionary: sysData)
}
if let windData = dictionary["wind"] as? [String:Any]{
wind = Wind(fromDictionary: windData)
}
weather = [Weather]()
if let weatherArray = dictionary["weather"] as? [[String:Any]]{
for dic in weatherArray{
let value = Weather(fromDictionary: dic)
weather.append(value)
}
}
}
/**
* Returns all the available property values in the form of [String:Any] object where the key is the approperiate json key and the value is the value of the corresponding property
*/
func toDictionary() -> [String:Any]
{
var dictionary = [String:Any]()
if base != nil{
dictionary["base"] = base
}
if cod != nil{
dictionary["cod"] = cod
}
if dt != nil{
dictionary["dt"] = dt
}
if id != nil{
dictionary["id"] = id
}
if name != nil{
dictionary["name"] = name
}
if visibility != nil{
dictionary["visibility"] = visibility
}
if clouds != nil{
dictionary["clouds"] = clouds.toDictionary()
}
if coord != nil{
dictionary["coord"] = coord.toDictionary()
}
if main != nil{
dictionary["main"] = main.toDictionary()
}
if sys != nil{
dictionary["sys"] = sys.toDictionary()
}
if wind != nil{
dictionary["wind"] = wind.toDictionary()
}
if weather != nil{
var dictionaryElements = [[String:Any]]()
for weatherElement in weather {
dictionaryElements.append(weatherElement.toDictionary())
}
dictionary["weather"] = dictionaryElements
}
return dictionary
}
/**
* NSCoding required initializer.
* Fills the data from the passed decoder
*/
#objc required init(coder aDecoder: NSCoder)
{
base = aDecoder.decodeObject(forKey: "base") as? String
clouds = aDecoder.decodeObject(forKey: "clouds") as? Cloud
cod = aDecoder.decodeObject(forKey: "cod") as? Int
coord = aDecoder.decodeObject(forKey: "coord") as? Coord
dt = aDecoder.decodeObject(forKey: "dt") as? Int
id = aDecoder.decodeObject(forKey: "id") as? Int
main = aDecoder.decodeObject(forKey: "main") as? Main
name = aDecoder.decodeObject(forKey: "name") as? String
sys = aDecoder.decodeObject(forKey: "sys") as? Sy
visibility = aDecoder.decodeObject(forKey: "visibility") as? Int
weather = aDecoder.decodeObject(forKey: "weather") as? [Weather]
wind = aDecoder.decodeObject(forKey: "wind") as? Wind
}
/**
* NSCoding required method.
* Encodes mode properties into the decoder
*/
#objc func encode(with aCoder: NSCoder)
{
if base != nil{
aCoder.encode(base, forKey: "base")
}
if clouds != nil{
aCoder.encode(clouds, forKey: "clouds")
}
if cod != nil{
aCoder.encode(cod, forKey: "cod")
}
if coord != nil{
aCoder.encode(coord, forKey: "coord")
}
if dt != nil{
aCoder.encode(dt, forKey: "dt")
}
if id != nil{
aCoder.encode(id, forKey: "id")
}
if main != nil{
aCoder.encode(main, forKey: "main")
}
if name != nil{
aCoder.encode(name, forKey: "name")
}
if sys != nil{
aCoder.encode(sys, forKey: "sys")
}
if visibility != nil{
aCoder.encode(visibility, forKey: "visibility")
}
if weather != nil{
aCoder.encode(weather, forKey: "weather")
}
if wind != nil{
aCoder.encode(wind, forKey: "wind")
}
}
}
WeatherService:
import Foundation
class WeatherSerice {
let weatherAPIKey: String
let weatherBaseURL: URL?
init(APIKey: String) {
self.weatherAPIKey = APIKey
weatherBaseURL = URL(string: "https://api.openweathermap.org/data/2.5/weather?")
}
func getCurrentWeather(city: String, completion: #escaping (CurrentWeather?) -> Void) {
if let weatherURL = URL(string: "\(weatherBaseURL!)q=\(city)&appid=\(weatherAPIKey)") {
let networkProcessor = NetworkProcessor(url: weatherURL)
networkProcessor.downloadJSONFromURL({(jsonDictionary) in
if let currentWeatherDictionary = jsonDictionary?["currently"] as?
[String : Any] {
let currentWeather = CurrentWeather(fromDictionary: currentWeatherDictionary)
completion(currentWeather)
} else {
completion(nil)
}
})
}
}
}
Part of the UIViewController:
let weatherService = WeatherSerice(APIKey: "52e6ff60bba8613b4850e065dcd3d0ac")
weatherService.getCurrentWeather(city: "London") {
(currentWeather) in
print (currentWeather)
}
Please, please drop NSCoding in favor of Codable, you will get rid of all that ugly boilerplate code
struct WeatherData : Decodable {
let coord : Coordinate
let cod, visibility, id : Int
let name : String
let base : String
let weather : [Weather]
let clouds: Clouds
let sys : Sys
let main : Main
let wind : Wind
let dt : Date
}
struct Coordinate : Decodable {
let lat, lon : Double
}
struct Weather : Decodable {
let id : Int
let icon : String
let main : MainEnum
let description: String
}
struct Sys : Decodable {
let type, id : Int
let sunrise, sunset : Date
let message : Double
let country : String
}
struct Main : Decodable {
let temp, tempMin, tempMax : Double
let pressure, humidity : Int
}
struct Wind : Decodable {
let speed : Double
let deg : Int
let gust : Double?
}
struct Clouds: Decodable {
let all: Int
}
enum MainEnum: String, Decodable {
case clear = "Clear"
case clouds = "Clouds"
case rain = "Rain"
}
As you can see the dates are decoded as Date and the main values in Weather are decoded as enum.
I don't know the API NetworkProcessor so I replaced it with traditional URLSession. The added URLComponents provide proper URL encoding for example if the city contains space characters
import Foundation
class WeatherSerice {
let weatherAPIKey: String
let weatherBase = "https://api.openweathermap.org/data/2.5/weather"
init(APIKey: String) {
self.weatherAPIKey = APIKey
}
func getCurrentWeather(city: String, completion: #escaping (WeatherData?) -> Void) {
var urlComponents = URLComponents(string: weatherBase)!
let queryItems = [URLQueryItem(name: "q", value: city),
URLQueryItem(name: "appid", value: weatherAPIKey)]
urlComponents.queryItems = queryItems
if let weatherURL = urlComponents.url {
let task = URLSession.shared.dataTask(with: weatherURL) { (data, response, error) in
if let error = error { print(error); completion(nil) }
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .secondsSince1970
let result = try decoder.decode(WeatherData.self, from: data!)
completion(result)
} catch {
print(error)
completion(nil)
}
}
task.resume()
}
}
}
I recommend to use the new Result type in Swift 5 to return the error, too.
func getCurrentWeather(city: String, completion: #escaping (Result<WeatherData,Error>) -> Void) {
var urlComponents = URLComponents(string: weatherBase)!
let queryItems = [URLQueryItem(name: "q", value: city),
URLQueryItem(name: "appid", value: weatherAPIKey)]
urlComponents.queryItems = queryItems
if let weatherURL = urlComponents.url {
let task = URLSession.shared.dataTask(with: weatherURL) { (data, response, error) in
if let error = error { completion(.failure(error)) }
do {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
decoder.dateDecodingStrategy = .secondsSince1970
let result = try decoder.decode(WeatherData.self, from: data!)
completion(.success(result))
} catch {
completion(.failure(error))
}
}
task.resume()
}
and use it
weatherService.getCurrentWeather(city: "London") { result in
switch result {
case .success(let result): print(result)
case .failure(let error): print(error)
}
}

Adding review[] with google places details

Following the Google API documentation (https://developers.google.com/places/web-service/details) I'm trying to adding for the first time review[] with google places details in my custom table view cell, but it does not work. I already added other parameters like the website and phone number and both working well, but whit review I'm definitely doing something wrong. This is my custom tableViewCell
private let authorKey = "author_name"
private let ratKey = "rating"
private let ctextKey = "text"
class MyTableViewCell: UITableViewCell {
#IBOutlet weak var myCommentsText: UILabel!
var place: EClass?
func update(place:EClass) {
self.place = place
myLabel.text = place.name
myImage.image = UIImage(named: "foto_icon.png")
if let url = place.photos?.first?.getPhotoURL(maxWidth: maxWidht) {
myImage.af_setImage(withURL: url)
}
NearZone.PlaceDetails(place: place) { (place) in
DispatchQueue.main.async {
if let phoneNumber = place.details?["international_phone_number"] as? String {
self.myLabel2.text = place.getDescription() + "\n\(phoneNumber)"
}
else {
self.myLabel2.text = place.getDescription()
}
}
// THIS PART DOES NOT WORK
if let comments = place.details?["reviews[]"] as? [[String:Any]] {
if let au = place.details?[authorKey] as? String, let ra = place.details?[ratKey] as? Float,
let ct = place.details?[ctextKey] as? [String] {
self.myCommentsText.text = "\(comments)"
}
}
}
}
and this is my func PlaceDetails
static func PlaceDetails(place:EClass, completion: #escaping (EClass) -> Void) {
guard place.details == nil else {
completion(place)
return
}
var params : [String : Any]
params = [
"key" : AppDelegate.googlePlacesAPIKey,
"placeid" : place.placeId,
]
Alamofire.request(googlePlaceDetailsHost, parameters: params, encoding: URLEncoding(destination: .queryString)).responseJSON { response in
let value = response.result.value as? [String : Any]
place.details = (value)?["result"] as? [String : Any]
print(((value)?["result"] as? [String : Any] ?? [String : Any]()).debugDescription)
completion(place)
}
}
can someone give me an help with this problem?

Managing encapsulated data in closure

Newbie here.Using Google API Nearby Search. I have problem sending encapsulated data into closure, already populated table with vicinity info, but when i try to send placeID info into closure to get Details, it gives me nil.
Here i get placeID and vicinity, and afterwards populate tableView with places array. Class Place is in separate swift file, function downloadPlaceID is inside ViewController.
class Place {
var placeId: String!
var vicinity: String!
var _placeId: String {
if placeId == nil {
placeId = ""
}
return placeId
}
var _vicinity: String {
if vicinity == nil {
vicinity = ""
}
return vicinity
}
init( place: [String:Any]) {
if let ids = place["id"] as? String {
self.placeId = ids
}
if let vicinities = place["vicinity"] as? String {
self.vicinity = vicinities
}
}
}
func downloadPlaceID (completed: #escaping DownloadComplete) {
let placeURL = URL(string: nearbyURL)
Alamofire.request(placeURL!).responseJSON { (response) in
let result = response.result
if let dictionary = result.value as? [String:Any] {
if let results = dictionary["results"] as? [[String:Any]] {
if let status = dictionary["status"] as? String {
if status == "OK" {
for obj in results {
place = Place(place: obj)
// here i get all the placeID's
places.append(place)
}
}
}
}
}
completed()
}
}
Then i try to get details, into which I put placeID:
func downloadDetails( input: String, completed: DownloadComplete) {
let details = "\(detailsBaseURL)\(detailsPlaceId)\(input)\(detailsKey)\(detailsSearchAPIKey)"
print(placeID)
Alamofire.request(details).responseJSON { response in
let result = response.result
if let dictionary = result.value as? [String:Any] {
if let result = dictionary["result"] as? [String:Any] {
if let phoneNumber = result["formatted_phone_number"] as? String {
self.phone = phoneNumber
print(self.phone!)
}
if let geometry = result["geometry"] as? [String:Any] {
if let location = geometry["location"] as? [String:Any] {
if let latitude = location["lat"] as? Double {
self.lat = latitude
print(self.lat!)
}
if let longitude = location["lng"] as? Double {
self.lng = longitude
print(self.lng!)
}
}
}
if let openingHours = result["opening_hours"] as? [String:Any] {
if let openNow = openingHours["open_now"] as? Bool {
self.workHours = openNow
print(self.workHours!)
}
}
}
}
}
}
Here is code inside viewDidLoad that i'm trying to use to get details.
override func viewDidLoad() {
super.viewDidLoad()
downloadPlaceID {
detail.downloadDetails(input: place.placeId, completed: {
})
}
}
It should be "place_id" instead of "id"
class Place {
var placeId: String!
var vicinity: String!
var _placeId: String {
if placeId == nil {
placeId = ""
}
return placeId
}
var _vicinity: String {
if vicinity == nil {
vicinity = ""
}
return vicinity
}
init( place: [String:Any]) {
if let ids = place["place_id"] as? String {
self.placeId = ids
}
if let vicinities = place["vicinity"] as? String {
self.vicinity = vicinities
}
}
}

Access array of dictionaries Swift 3

I need to access the work data which is inside an array of dictionaries and I'm a little bit confuse with this. I'm using swift 3. Some one can give-me some piece of coding to make it done?
I'm using this
let work: NSArray! = fbData.value(forKey: "work") as! NSArray
if let position: NSArray = work[0] as! NSArray {
let positionName: String = position.value(forKey: "name") as! String
self.userWorkExpLabel.text = "\(positionName)" as String
}
but I'm having this answer:
Could not cast value of type '__NSDictionaryI' (0x1106c7288) to 'NSArray' (0x1106c6e28).
there's the API
{
"work": [
{
"employer": {
"id": "93643283467",
"name": "Oracast"
},
"location": {
"id": "111983945494775",
"name": "Calgary, Alberta"
},
"position": {
"id": "146883511988628",
"name": "Mobile Developer"
},
"start_date": "2017-04-30",
"id": "1446626725564198"
}
],
Ok guys. I tried what you posted and what I have now is something like this:
a structs class:
import Foundation
struct Worker{
let employer: Employer
let location: Location
let position: Position
let startDate:String
let id: String
init?(fromDict dict: Dictionary<String, Any>){
guard let employer = Employer(fromDict: dict["employer"] as? Dictionary<String, String>),
let location = Location(fromDict: dict["location"] as? Dictionary<String, String>),
let position = Position(fromDict: dict["position"] as? Dictionary<String, String>),
let startDate = dict["start_date"] as? String,
let id = dict["id"] as? String else {
return nil
}
self.employer = employer
self.location = location
self.position = position
self.startDate = startDate
self.id = id
}
}
struct Employer{
let id: String
let name: String
init?(fromDict dict:Dictionary<String, String>?){
guard let id = dict?["id"],
let name = dict?["name"] else{
return nil
}
self.id = id
self.name = name
}
}
struct Location {
let id:String
let name:String
init?(fromDict dict:Dictionary<String, String>?) {
guard let id = dict?["id"],
let name = dict?["name"] else {
return nil
}
self.id = id
self.name = name
}
}
struct Position {
let id:String
let name:String
init?(fromDict dict:Dictionary<String, String>?) {
guard let id = dict?["id"],
let name = dict?["name"] else {
return nil
}
self.id = id
self.name = name
}
}
Ive created a class called facebookGraphRequest.
import Foundation
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
import FBSDKShareKit
class facebookGraphRequest: NSObject {
class func graphRequestWork(completion: #escaping(_ error: Error?, _ facebookUserWork: Worker)-> Void){
if ((FBSDKAccessToken.current()) != nil){
let parameters = ["fields": "name, picture.width(198).height(198), location{location}, work{employer}, education, about, id"]
let graphRequest: FBSDKGraphRequest = FBSDKGraphRequest(graphPath: "me", parameters: parameters)
graphRequest.start { (connection, result, error) in
if ((error) != nil ){
print(error!)
}else {
print(result!)
func workersArray(data:Dictionary<String, Any>)->[Worker]?{
guard let arrayOfDict = data["work"] as? Array<Dictionary<String, Any>> else {
return nil
}
return arrayOfDict.flatMap({ Worker(fromDict: $0)})
}
}
}
}
}
}
and I'm calling this data inside the viewController with:
func facebookLogin(){
facebookGraphRequest.graphRequestWork { (error: Error?, facebookUserWork: Worker) in
self.userNameJobPositionLabel.text = "\(facebookUserWork.position)"
self.companyNameLabel.text = "\(facebookUserWork.employer)"
}
}
Somebody knows what's happening? There's nothing happening with the labels.
I thought this apis was easier than that. I'm really confused with this process... Sorry if it looks like stupid questions but I'm really messing my mind because of this things... I really need your help guys. My work depends on that :(
After experimenting with Swift 4 and going in the direction that #PuneetSharma demonstrated I found it's even easier when you use raw JSON text, Codable, and JSONDecoder:
import Foundation
// define the nested structures
struct Work: Codable {
let work: [Worker]
}
struct Worker: Codable {
let employer: Employer
let location: Location
let position: Position
let startDate: String
let id: String
// needed a custom key for start_date
enum CodingKeys : String, CodingKey {
case employer, location, position, startDate = "start_date", id
}
}
struct Employer: Codable {
let id: String
let name: String
}
struct Location: Codable {
let id: String
let name: String
}
struct Position: Codable {
let id: String
let name: String
}
// turn the text into `Data` and then
// decode as the outermost structure
if let jsonData = json.data(using: .utf8),
let work = try? JSONDecoder().decode(Work.self, from: jsonData) {
print(work)
}
The result is a Work structure with all the data:
Work(work: [
Model.Worker(employer : Model.Employer(id : "93643283467",
name: "Oracast"),
location : Model.Location(id : "111983945494775",
name: "Calgary, Alberta"),
position : Model.Position(id : "146883511988628",
name: "Mobile Developer"),
startDate: "2017-04-30",
id : "1446626725564198")
])
(I formatted the output a bit to clarify the structures produced.)
You get a lot of functionality for free just by using Codable. It's also simple to go the other way and produce JSON text from any of the structures.
You should ideally introduce model classes like this:
struct Worker {
let employer:Employer
let location:Location
let position:Position
let startDate:String
let id:String
init?(fromDict dict:Dictionary<String, Any>) {
guard let employer = Employer(fromDict: dict["employer"] as? Dictionary<String, String>), let location = Location(fromDict: dict["location"] as? Dictionary<String, String>), let position = Position(fromDict: dict["position"] as? Dictionary<String, String>), let startDate = dict["start_date"] as? String, let id = dict["id"] as? String else {
return nil
}
self.employer = employer
self.location = location
self.position = position
self.startDate = startDate
self.id = id
}
}
struct Employer {
let id:String
let name:String
init?(fromDict dict:Dictionary<String, String>?) {
guard let id = dict?["id"], let name = dict?["name"] else {
return nil
}
self.id = id
self.name = name
}
}
struct Location {
let id:String
let name:String
init?(fromDict dict:Dictionary<String, String>?) {
guard let id = dict?["id"], let name = dict?["name"] else {
return nil
}
self.id = id
self.name = name
}
}
struct Position {
let id:String
let name:String
init?(fromDict dict:Dictionary<String, String>?) {
guard let id = dict?["id"], let name = dict?["name"] else {
return nil
}
self.id = id
self.name = name
}
}
Now, you can introduce a function like this:
func workersArray(data:Dictionary<String, Any>)->[Worker]?{
guard let arrayOfDict = data["work"] as? Array<Dictionary<String, Any>> else {
return nil
}
return arrayOfDict.flatMap({ Worker(fromDict: $0)})
}
Use this code
if let json = try JSONSerialization.jsonObject(with: data) as? [String: Any] {
if let workArray = json["work"] as? [[String: Any]] {
if let dictWork = workArray.first {
if let dictPosition = dictWork["position"] as? [String: String] {
print("position name : \(dictPosition["name"])")
}
}
}
}

Ambiguous reference to member 'subscript' when using Google maps route

The below code works fine in Xcode 7.1 but when I upgrade to Xcode 8 and swift 3 I get this error, I had searched on internet for same problem in stackoverflow but none of them answer my question.
func drawRoute() {
clearRoute()
// self.routePolyline = nil
// self.routePolyline.map = nil
let route = mapTk.overviewPolyline!["points"] as String //error here
//var overViewPolyLine = routes![0]["overview_polyline"]["points"].string
let path: GMSPath = GMSPath(fromEncodedPath: route)
routePolyline = GMSPolyline(path: path)
routePolyline.map = viewMap
}
here is the class mapTK for more info:
import Foundation
import UIKit
import CoreLocation
import MapKit
///This Class is designed for the detail methods that contact google maps server like GeoCode, GetDirection and Calculate distance and time
class MapTK: NSObject {
let baseURLGeocode = "https://maps.googleapis.com/maps/api/geocode/json?"
var lookupAddressResults: Dictionary<NSObject, AnyObject>!
var fetchedFormattedAddress: String!
var fetchedAddressLongitude: Double!
var fetchedAddressLatitude: Double!
let baseURLDirections = "https://maps.googleapis.com/maps/api/directions/json?"
var selectedRoute: Dictionary<NSObject, AnyObject>!
var overviewPolyline: Dictionary<NSObject, AnyObject>!
var originCoordinate: CLLocationCoordinate2D!
var destinationCoordinate: CLLocationCoordinate2D!
var originAddress: String!
var destinationAddress: String!
var totalDistanceInMeters: UInt = 0
var totalDistance: String!
var totalDurationInSeconds: UInt = 0
var totalDuration: String!
/* private let errorDictionary = ["NOT_FOUND" : "At least one of the locations specified in the request's origin, destination, or waypoints could not be geocoded",
"ZERO_RESULTS":"No route could be found between the origin and destination",
"MAX_WAYPOINTS_EXCEEDED":"Too many waypointss were provided in the request The maximum allowed waypoints is 8, plus the origin, and destination",
"INVALID_REQUEST":"The provided request was invalid. Common causes of this status include an invalid parameter or parameter value",
"OVER_QUERY_LIMIT":"Service has received too many requests from your application within the allowed time period",
"REQUEST_DENIED":"Service denied use of the directions service by your application",
"UNKNOWN_ERROR":"Directions request could not be processed due to a server error. Please try again"]
*/
override init() {
super.init()
}
///the geocodeAddress is getting information from googlemaps with JSON it receive the geocode information
func geocodeAddress(address: String!, withCompletionHandler completionHandler: #escaping ((_: String, _: Bool) -> Void)) {
if let lookupAddress = address {
var geocodeURLString = baseURLGeocode + "address=" + lookupAddress
//geocodeURLString = geocodeURLString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!
let geocodeURL = NSURL(string: geocodeURLString)
// dispatch_Dispatch.Queue.mainasync(dispatch_get_main_queue(), { () -> Void in
DispatchQueue.main.async {
let geocodingResultsData = NSData(contentsOf: geocodeURL! as URL)
// let error: NSError?
do
{
let dictionary = try JSONSerialization.jsonObject(with: geocodingResultsData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
// try NSJSONSerialization.JSONObjectWithData(directionsData!, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary
/*
if (error != nil) {
print(error)
completionHandler(status: "", success: false)
}
*/
// else {
// Get the response status.
let status = dictionary?["status"]as! String
// let status = dictionary["status"] as! [AnyObject]
if status == "OK" {
let allResults = dictionary?["results"] as! Array<Dictionary<NSObject, AnyObject>>
self.lookupAddressResults = allResults[0]
// Keep the most important values.
self.fetchedFormattedAddress = self.lookupAddressResults["formatted_address"]? as? String
let geometry = self.lookupAddressResults["geometry"] as! Dictionary<NSObject, AnyObject>
self.fetchedAddressLongitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lng"] as! NSNumber).doubleValue
self.fetchedAddressLatitude = ((geometry["location"] as! Dictionary<NSObject, AnyObject>)["lat"] as! NSNumber).doubleValue
completionHandler(status, true)
}
else {
completionHandler(status, false)
}
} catch let error as NSError {
print(error)
}
}
}
else {
completionHandler("No valid address.", false)
}
}
///getDirection method is using JSON to receive waypoints for making route for drawing polyline, as its name suggests it get the direciton inforamtion, first it send lat and lng the receive a dictionary and that dictionary give all the infrmation need to draw route like waypoints
func getDirections(origin: String!, destination: String!, waypoints: Array<String>!, travelMode: TravelModes!, completionHandler: #escaping ((_ status: String, _ success: Bool) -> Void)) {
if let originLocation = origin {
if let destinationLocation = destination {
var directionsURLString = baseURLDirections + "origin=" + originLocation + "&destination=" + destinationLocation //+ "&key=AIzaSyDsDqj0EMYZ-C4lGF3tmbntZtzurLl6_J4"
if let routeWaypoints = waypoints {
directionsURLString += "&waypoints=optimize:true"
for waypoint in routeWaypoints {
directionsURLString += "|" + waypoint
}
}
if let _ = travelMode {
var travelModeString = ""
switch travelMode.rawValue {
case TravelModes.walking.rawValue:
travelModeString = "walking"
case TravelModes.bicycling.rawValue:
travelModeString = "bicycling"
default:
travelModeString = "driving"
}
directionsURLString += "&mode=" + travelModeString
}
// directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(NSUTF8StringEncoding)!
// directionsURLString = directionsURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
// directionsURLString = directionsURLString.stringByAddingPercentEncodingWithAllowedCharacters(.URLHostAllowedCharacterSet())!
/// directionsURLString = directionsURLString.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!
directionsURLString = directionsURLString.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)!
//let encodedHost = unencodedHost.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
//var url = NSURL(urlString.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()))
//let savePath = (documentDirectory as NSString).stringByAppendingPathComponent("mergeVideo-\(date).mov")
let directionsURL = NSURL(string: directionsURLString)
// DispatchQueue.main.asynchronously(execute: { () -> Void in
DispatchQueue.main.async {
let directionsData = NSData(contentsOf: directionsURL! as URL)
// var error: NSError?
do {
let dictionary = try JSONSerialization.jsonObject(with: directionsData! as Data, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary
/*
if (error != nil) {
print(error)
completionHandler(status: "", success: false)
}
*/
// else {
let status = dictionary?["status"] as! String
if status == "OK" {
self.selectedRoute = (dictionary?["routes"] as! Array<Dictionary<NSObject, AnyObject>>)[0]
self.overviewPolyline = self.selectedRoute["overview_polyline"] as! Dictionary<NSObject, AnyObject>
let legs = self.selectedRoute["legs"] as! Array<Dictionary<NSObject, AnyObject>>
let startLocationDictionary = legs[0]["start_location"] as! Dictionary<NSObject, AnyObject>
self.originCoordinate = CLLocationCoordinate2DMake(startLocationDictionary["lat"] as! Double, startLocationDictionary["lng"] as! Double)
let endLocationDictionary = legs[legs.count - 1]["end_location"]as! Dictionary<NSObject, AnyObject>
self.destinationCoordinate = CLLocationCoordinate2DMake(endLocationDictionary["lat"] as! Double, endLocationDictionary["lng"] as! Double)
self.originAddress = legs[0]["start_address"] as! String
self.destinationAddress = legs[legs.count - 1]["end_address"] as! String
self.calculateTotalDistanceAndDuration()
completionHandler(status, true)
}
else {
completionHandler(status, false)
}
} catch let error as NSError {
print(error)
}
}
}
else {
completionHandler("Destination is nil.", false)
}
}
else {
completionHandler("Origin is nil", false)
}
}
/// this method below is for the calculation of Distance adn Duration of each location for travel
func calculateTotalDistanceAndDuration() {
let legs = self.selectedRoute["legs"] as! Array<NSDictionary>
totalDistanceInMeters = 0
totalDurationInSeconds = 0
for leg in legs {
totalDistanceInMeters += (leg["distance"] as! Dictionary<NSObject, AnyObject>)["value"] as! UInt
totalDurationInSeconds += (leg["duration"]as! Dictionary<NSObject, AnyObject>)["value"] as! UInt
}
let distanceInKilometers: Double = Double(totalDistanceInMeters / 1000)
totalDistance = "Total Distance: \(distanceInKilometers) Km"
let mins = totalDurationInSeconds / 60
let hours = mins / 60
let days = hours / 24
let remainingHours = hours % 24
let remainingMins = mins % 60
let remainingSecs = totalDurationInSeconds % 60
totalDuration = "Duration: \(days) d, \(remainingHours) h, \(remainingMins) mins, \(remainingSecs) secs"
}
}
Use
Dictionary<String, Any>
as JSON dictionary type.
JSON keys are required to be String anyway and
AnyObject has been changed to Any in Swift 3.
Edit :
I recommend to use a type alias for the JSON dictionary type
typealias JSONObject = [String:Any] // synonym of Dictionary<String, Any>
Then you can write
var lookupAddressResults: JSONObject!
...
var selectedRoute: JSONObject!
var overviewPolyline: JSONObject!
and the parsing code
...
if status == "OK" {
let allResults = dictionary?["results"] as! Array<JSONObject>
self.lookupAddressResults = allResults[0]
// Keep the most important values.
self.fetchedFormattedAddress = self.lookupAddressResults["formatted_address"] as? String
let geometry = self.lookupAddressResults["geometry"] as! JSONObject
self.fetchedAddressLongitude = ((geometry["location"] as! JSONObject)["lng"] as! NSNumber).doubleValue
self.fetchedAddressLatitude = ((geometry["location"] as! JSONObject)["lat"] as! NSNumber).doubleValue
completionHandler(status, true)
}
...
You can also replace
( ... as! NSNumber).doubleValue
with
... as! Double

Resources