import UIKit
import GoogleMaps
import Alamofire
importSwiftyJSON
classViewController:UIViewController {
override func viewDidLoad(){
super.viewDidLoad()
// Do any additional setup after loading the view.
super.viewDidLoad()
// Do any additional setup after loading the view.
// Create a GMSCameraPosition that tell the map to display the
// coordinate-33.86, 151.20 at zoom level 6.
let camera = GMSCameraPosition.camera (with Latitude:35.665751, longitude: 139.728687, zoom: 6.0)
letmapView = GMSMapView.map (withFrame:self.view.frame, camera:camera)
self.view.addSubview(mapView)
// Create a marker in the center of the map.
let marker = GMSMarker()
marker.position = CLLocationCoordinate 2D (latitude: 35.665751, longitude: 139.728687)
marker.title="Roppongi"
marker.snippet="Roppongi 7-chome 4-1 in Minato-ku, Tokyo"
marker.map=mapView
AF.request("https://map.yahooapis.jp/search/local/V1/localSearch?cid=d8a23e9e64a4c817227ab09858bc1330&lat=35.662654694078626&lon=139.73135330250383&dist=2&query=%E3%83%A9%E3%83%BC%E3%83%A1%E3%83%B3&appid=dj00aiZpPWdzNkFwb2NZRWxBbiZzPWNvbnN1bWVyc2VjcmV0Jng9MjY-&output=json").responseJSON {response in}
iflet jsonObject=response.result.value{
let json=JSON(jsonObject)
let features = json ["Feature" ]
// If json is.Dictionary
for (key, subJson): (String, JSON) in json {
// Do something you want
let name = subJson ["Name"].stringValue
let address = subJson ["Property"]["Address"].stringValue
let coordinates = subJson ["Geometry"] ["Coordinates"].stringValue
let coordinatesArray=coordinates.split(separator:",")
let lat = coordinatesArray[1]
letlon=coordinatesArray[0]
let latDouble=Double(lat)
letlonDouble=Double(lon)
let marker = GMSMarker()
marker.position = CLLocationCoordinate 2D (latitude: latDouble, longitude:lonDouble)
marker.title=name
marker.snippet="Roppongi 7-chome 4-1 in Minato-ku, Tokyo"
marker.map=mapView
}
}
}
}
}
As the error message indicates, response.result
is of the Result<Any,AFError>
type, and the Result
type does not have the property value
.
So, for example, I think the following code will work. What do you think?
iflet jsonObject=response.result.value{
↓
if case.success(let jsonObject)=response.result{
(Typical method of extracting successful values from the Result
type.)
As far as the latest source is concerned, the completion handler for the responseJSON
method now receives The type odResponse has the property value
You can use it, but you should get used to it because the Result
type will be used to the Swift library in the future.
I myself have never used anything like Alamofire, but it seems that the usage of the API is changing depending on the version.Try to find an example code for your version.
Also, many people (at least in the English version of stackoverflow) say, "It's better not to use it because it's old-fashioned."If you are dealing with data like your question, you should consider using Codable
.
© 2024 OneMinuteCode. All rights reserved.