私は、サーバーから来た場所のリストを使用して地図に注釈を追加しようとしています。注釈が地図上に表示されない
JSONデータ:
[{ "_id" : "589f3e299b64795df23a886b",
"name" : "Store 1",
"description" : "Most awesome store!",
"location" : {
"longitude" : -6.279025369998967,
"latitude" : 53.35487382895707
}
},
{
"_id" : "589f3e299b64795df23a886b",
"name" : "Store 2",
"description" : "Most awesome store!",
"location" : {
"longitude" : -6.267085527951536,
"latitude" : 53.33785738724761
}
}]
私はviewWillAppear方法における上記のJSONデータを取得するために、サーバからのGETリクエストを送信するためにアラモ火を使用しています。
override func viewWillAppear(_ animated: Bool) {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.requestLocation()
mapView.showsUserLocation = false
var currentLocation = Location()
if let location = locationManager.location {
currentLocation = Location(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
}
//default to 10 km radius for now
let params = [
"latitude": currentLocation.getLatitude(),
"longitude": currentLocation.getLongitude(),
"radius": 10.0
] as [String : Any]
Alamofire.request("http://website.com/fyp/searchNearbyStores", parameters: params).responseJSON { response in
switch response.result {
case .success(let value):
let result = JSON(value)
print(result)
self.storeList = result.arrayValue.map({
Store(name: $0["name"].stringValue, description: $0["description"].stringValue, location: CLLocationCoordinate2D(latitude: $0["location"]["latitude"].doubleValue, longitude: $0["location"]["longitude"].doubleValue))
})
case .failure(let error):
print(error)
}
}
}
最後にviewDidLoadメソッドでは、注釈を追加するためにlat/longデータとともにstoreListを使用します。
override func viewDidLoad() {
super.viewDidLoad()
//zoom to current user location
if let location = locationManager.location {
let span = MKCoordinateSpanMake(0.01, 0.01)
let region = MKCoordinateRegion(center: location.coordinate, span: span)
mapView.setRegion(region, animated: true)
}
for store in storeList {
let annotation = MKPointAnnotation()
annotation.title = store.getName()
annotation.subtitle = store.getDescription()
annotation.coordinate = store.getLocation()
mapView.addAnnotation(annotation)
}
}
何らかの理由で、注釈が実行時にマップに表示されないようです。私は注釈を追加する正しい方法をしているようだが、おそらく小さくて重要なビットを逃したかもしれない。誰も助けることができますか?
が処理されました。ありがとうございました! – kennanwho