2017-07-26 104 views
-1

您好我有几个图上的点上的服务器JSON格式的文件:创建MapKit点从JSON数据(SWIFT)

[{"ID":"34","CompanyName":"Sruper","Latitude":"33.8886954","Longitude":"35.4911273","Location":"USA","Category":"Automobile "},{"ID":"47","CompanyName":"TechMe","Latitude":"33.8869625","Longitude":"35.5131741","Location":"USA","Category":"Home "},{"ID":"48","CompanyName":"ElectroKey","Latitude":"33.8786214","Longitude":"35.4863271","Location":"USA","Category":"Health & Fitness "}] 

我希望把这些点作为标记在我的地图在我的iOS应用。我看了几页,但在这里,但我没有找到任何东西,我试过的东西不起作用。我正在使用swift 3和Xcode。

我能够检索数据作为数组,但不能把它们放在地图视图,因为它们是数组而不是双倍我怎么能做到这一点,循环?我得到的错误无法投类型的值“__NSArrayI”(0x112f16dd8)为“NSNumber的”

我使用雨燕找不到快速的答案,但他们对目标C iOS JSON Array and MapKit

let url = URL(string: "http://file.php") 
    let data = try? Data(contentsOf: url!) 

    let quotesData = try! JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSArray 

    let CompanyNameArray = quotesData.value(forKey: "CompanyName") 
    let LocationLatitudeArray = quotesData.value(forKey: "Latitude") 
    let LocationLongitudeArray = quotesData.value(forKey: "Longitude") 
struct Location { 
     let title: String 
     let latitude: Double 
     let longitude: Double 
    } 

    let locations = [ 
     Location(title:CompanyNameArray as! String, latitude: LocationLatitudeArray as! Double, longitude: LocationLongitudeArray as! Double), 

    ] 

    let annotations = locations.map { location -> MKAnnotation in 
     let annotation = MKPointAnnotation() 
     annotation.title = location.title 
     annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude) 
     return annotation 
    } 
    mapView.addAnnotations(annotations) 

请帮忙吗?

回答

0

要访问数组值,必须使用array [index],如:CompanyNameArray[index]。您应该遍历数组中的项目,并使用位置填充阵列。

+0

'title:CompanyNameArray [index] as!字符串'不起作用它说类型任何没有下标成员。我使用Swift,无法找到答案为迅速,但他们是为客观的C [https://stackoverflow.com/questions/14802618/ios-json-array-and-mapkit] – CoderJ13

+0

我认为你没有得到数组,您是否已经完成调试以检查'CompanyNameArray'值?它显示了什么? –