0

这个 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)   

                                }
                            }


             })
4

0 回答 0