MKAnotnotation没有出现在模拟中-ISS Tracking App

时间:2018-09-08 06:08:02

标签: ios swift api mapkit mapkitannotation

我正在构建一个应用程序,在该应用程序中我从API检索ISS的纬度和经度,并将其绘制在MapView上。我遇到的问题是,每当我运行该项目时,我都会不断遇到一个错误,提示“无法从第4个角落插入合法的属性”,并且注释未在iPhone仿真中显示。这是下面的代码:

import UIKit
import CoreLocation
import MapKit
import Alamofire
import SwiftyJSON


class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    //Declare variables
    let locationManager = CLLocationManager()
    var issCoordinate  = CLLocationCoordinate2D()
    var issLocationJSON : JSON? = nil
    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        getUserLocation()
        getISSLocation()
        mapAnnotation()

    }

    func getUserLocation (){
        //Request user's permission to access location
        locationManager.delegate = self
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
        locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
    }

    //Retrive ISS location
    func getISSLocation () {
        //API URL
        let url = "http://api.open-notify.org/iss-now.json"

        //Making API request...
        Alamofire.request(url).responseJSON {
            response in
            if response.result.isSuccess {
                self.issLocationJSON = JSON(response.result.value!)
                self.getISSCoordinates(json: self.self.issLocationJSON!)
            }
        }

    }

    //Get's ISS longitude and latitude
    func getISSCoordinates(json: JSON){
        //Store the ISS's latitude and longitude
        let issLatitude = json["iss_position"]["latitude"].doubleValue
        let issLongitude = json["iss_position"]["longitude"].doubleValue
        issCoordinate = CLLocationCoordinate2D(latitude: issLatitude,longitude: issLongitude)
        print(issLongitude)
    }

    func mapAnnotation(){
        //Create ISS annotation
        let span = MKCoordinateSpanMake(0.05, 0.05)
        let region = MKCoordinateRegion(center: issCoordinate, span: span)
        mapView.setRegion(region, animated: true)
        let issAnnotation = MKPointAnnotation()
        issAnnotation.coordinate = issCoordinate
        issAnnotation.title = "ISS Location"
        issAnnotation.subtitle = "ISS Location"
        mapView.addAnnotation(issAnnotation)
    }

}

我也自由地在下面发布了我的Info.plist文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
    <dict>
        <key>NSLocationWhenInUseUsageDescription</key>
        <string>allow</string>
        <key>NSLocationAlwaysUsageDescription</key>
        <string>allow</string>
        <key>NSLocationUsageDescription</key>
        <string>allow</string>
        <key>CFBundleIdentifier</key>
        <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
        <key>CFBundleName</key>
        <string>$(PRODUCT_NAME)</string>
        <key>CFBundleInfoDictionaryVersion</key>
        <string>6.0</string>
        <key>LSApplicationCategoryType</key>
        <string></string>
        <key>CFBundleVersion</key>
        <string>1</string>
        <key>CFBundleExecutable</key>
        <string>$(EXECUTABLE_NAME)</string>
        <key>LSRequiresIPhoneOS</key>
        <true/>
        <key>UILaunchStoryboardName</key>
        <string>LaunchScreen</string>
        <key>UIMainStoryboardFile</key>
        <string>Main</string>
        <key>NSAppTransportSecurity</key>
        <dict>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
        </dict>
        <key>UIRequiredDeviceCapabilities</key>
        <array>
            <string>armv7</string>
        </array>
        <key>UISupportedInterfaceOrientations</key>
        <array>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationLandscapeLeft</string>
            <string>UIInterfaceOrientationLandscapeRight</string>
        </array>
        <key>CFBundlePackageType</key>
        <string>APPL</string>
        <key>CFBundleDevelopmentRegion</key>
        <string>$(DEVELOPMENT_LANGUAGE)</string>
        <key>UISupportedInterfaceOrientations~ipad</key>
        <array>
            <string>UIInterfaceOrientationPortrait</string>
            <string>UIInterfaceOrientationPortraitUpsideDown</string>
            <string>UIInterfaceOrientationLandscapeLeft</string>
            <string>UIInterfaceOrientationLandscapeRight</string>
        </array>
        <key>CFBundleShortVersionString</key>
        <string>1.0</string>

    </dict>
</plist>

0 个答案:

没有答案