这个json正在装载餐馆,将州和美国的城市和地图和餐馆名称一起显示在地图上。 此应用程序适用于模拟器,但在搜索餐厅时无法在iPhone和iPod上运行。
do{
var strData = NSString(data: data!, encoding: NSUTF8StringEncoding)
var decodedData: NSData?
if strData == nil{
strData = NSString(data: data!, encoding: NSASCIIStringEncoding)
decodedData = strData!.dataUsingEncoding(NSUTF8StringEncoding)
}else {
decodedData = data;
}
let json = try NSJSONSerialization.JSONObjectWithData(decodedData!, options:.AllowFragments)
let annotationsToRemove = self.mapView.annotations.filter { $0 !== self.mapView.userLocation }
self.mapView.removeAnnotations( annotationsToRemove )
for dict in json as! [NSDictionary]{
self.names.append(dict["name"] as! String)
self.address.append(dict["address"] as! String)
self.label.append(dict["label"] as! String)
//For Loop
// to display address
for googleadd in self.address {
// to display restaurant name
for googlename in (self.names) {
let annotationTitle: String = googlename;
var geocoder = CLGeocoder()
geocoder.geocodeAddressString(googleadd, completionHandler: {(placemarks, error) -> Void in
if let placemark = placemarks?[0] as? CLPlacemark? {
if placemark == nil{
return
}
if let coordinate = placemark?.location?.coordinate {
let anotation = MKPointAnnotation()
anotation.coordinate = coordinate
anotation.title = googlename
anotation.subtitle = googleadd
self.mapView.addAnnotation(anotation)
self.mapView.setCenterCoordinate(coordinate, zoomLevel: 12, animated: true)
}
}
})