使用segue时,使用UITableview中的信息在MapKit中创建注释

时间:2017-04-10 23:15:57

标签: php ios swift uitableview mapkit

我目前正致力于开发一个从php Web服务中提取数据(地址和位置信息)的应用程序,并在tableview单元格中将数据显示为字符串。我的tableview嵌入在导航控制器中,当单击一个单元格时,segue会链接到地图上带有几个硬编码引脚的地图。

我有兴趣创建一个segue,它将tableview单元格(Address)链接到地图中的特定pin / annotation,作为对地址的直接引用。

此外,我有兴趣创建一个自定义注释,显示有关其位置的数据(名称,地址,照片等)。

理想情况下,我正在尝试重新创建一个更简单的SpotHero版本。

谢谢!

Heres我的一些代码:

TableController:

class TableController:UITableViewController {

var TableData:Array< String > = Array < String >()
var garagedata :Array<String> = Array < String >()


override func viewDidLoad() {
    super.viewDidLoad()

    get_data_from_url("http://cgi.soic.indiana.edu/~team20/service.php")
}



override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return TableData.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "BasicCell", for: indexPath)

    cell.textLabel?.text = TableData[indexPath.row]
    //Wrap text in tableview
    cell.textLabel?.numberOfLines=0

    return cell
}

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    //Automatic sizing of tableviewcells
    return UITableViewAutomaticDimension
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    // Segue to the second view controller
    self.performSegue(withIdentifier: "detailSegue", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

    // get a reference to the second view controller
    let mapController = segue.destination as! MapController

    // set a variable in the second view controller with the data to pass
    mapController.receivedData = "Hello"
}

func get_data_from_url(_ link:String)
{
    let url:URL = URL(string: link)!
    let session = URLSession.shared

    let request = NSMutableURLRequest(url: url)
    request.httpMethod = "GET"
    request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData


    let task = session.dataTask(with: request as URLRequest, completionHandler: {
        (
        data, response, error) in

        guard let _:Data = data, let _:URLResponse = response  , error == nil else {

           return
        }


        self.extract_json(data!)


    }) 

    task.resume()

}


func extract_json(_ data: Data)
{


    let json: Any?

    do
    {
        json = try JSONSerialization.jsonObject(with: data, options: [])
    }
    catch
    {
        return
    }

    guard let data_list = json as? NSArray else
    {
        return
    }


    if let garages_list = json as? NSArray
    {

        let locations: NSMutableArray = NSMutableArray()
        for i in 0 ..< data_list.count

        /*{
            print(TableData)
            if let garage_obj = garages_list[i] as? NSDictionary

            {
                if let garage_name = garage_obj["GName"] as? String
                {
                if let garage_address = garage_obj["Address"] as? String
                {
                    if let garage_code = garage_obj["Count"] as? String
                    {
                        if let garage_cap = garage_obj["Capacity"] as? String

                    {
                        TableData.append(garage_name + "\n"
                            + garage_address + " [" + garage_code + "/" + garage_cap+"]")



                        garagedata.append(garage_name + "\n"
                            + garage_address + " [" + garage_code + "/" + garage_cap+"]")

                        }
                    }
                }
            }
            }}*/
        {

        let location = GarageModel()

        if let garage_obj = garages_list[i] as? NSDictionary
        {
        //the following insures none of the JsonElement values are nil through optional binding
            if let name = garage_obj["GName"] as? String,
                let address = garage_obj["Address"] as? String,
                let latitude = garage_obj["Latitude"] as? String,
                let longitude = garage_obj["Longitude"] as? String,
                let count = garage_obj["Count"] as? String
        {

                location.name = name
                location.address = address
                location.latitude = latitude
                location.longitude = longitude
                location.count = count

        }

            locations.add(location)
            print(locations)

        }
        }}


    DispatchQueue.main.async(execute: {self.do_table_refresh()})

}


func do_table_refresh()
{
    self.tableView.reloadData()

}

}

GarageModel:

类GarageModel:NSObject {

//properties

var name: String?
var address: String?
var latitude: String?
var longitude: String?
var count: String?
var capacity: String?


//empty constructor

override init()
{

}

//construct with @name, @address, @latitude, and @longitude parameters

init(name: String, address: String, latitude: String, longitude: String, count: String, capacity: String) {

    self.name = name
    self.address = address
    self.latitude = latitude
    self.longitude = longitude
    self.count = count
    self.capacity = capacity

}


//prints object's current state

override var description: String {
    return "Name: \(name), Address: \(address), Latitude: \(latitude), Longitude: \(longitude), Count: \(count), Capacity: \(capacity)"

}

}

我为了审美/测试目的而对一些引脚进行了硬编码,但我没有动态显示这些引脚。

地图控制器:

类MapController:UIViewController {

@IBOutlet weak var mapView: MKMapView!

var receivedData = ""

override func viewDidLoad() {
    super.viewDidLoad()

    print(receivedData)
    let latitude: CLLocationDegrees = 39.173294

    let longitude: CLLocationDegrees = -86.520244

    let latitude2: CLLocationDegrees = 39.163589

    let longitude2: CLLocationDegrees = -86.526266

    let latitude3: CLLocationDegrees = 39.167250

    let longitude3: CLLocationDegrees = -86.515059

    let latDelta: CLLocationDegrees = 0.05

    let lonDelta: CLLocationDegrees = 0.05

    let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta)

    let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)

    let location2 = CLLocationCoordinate2D(latitude: latitude2, longitude: longitude2)

    let location3 = CLLocationCoordinate2D(latitude: latitude3, longitude: longitude3)

    let region = MKCoordinateRegion(center: location, span: span)

    mapView.setRegion(region, animated: true)

    let garage1 = MKPointAnnotation()

    garage1.title = "Fee Lane Parking Garage"

    garage1.subtitle = "Count XX/150 \n 11th and Fee"

    garage1.coordinate = location

    mapView.addAnnotation(garage1)

    let garage2 = MKPointAnnotation()

    garage2.title = "Henderson Parking Garage"

    garage2.subtitle = "Count XX/150 Fess and Atwater"

    garage2.coordinate = location2

    mapView.addAnnotation(garage2)

    let garage3 = MKPointAnnotation()

    garage3.title = "Jordan Parking Garage"

    garage3.subtitle = "Count XX/150 North Jordan Street"

    garage3.coordinate = location3

    mapView.addAnnotation(garage3)
}

}

0 个答案:

没有答案