实现tableview子视图弹出控制器到谷歌地图查看swift

时间:2017-04-28 00:26:12

标签: ios swift

我是swift的新手,但是我尝试通过单击子视图控制器中的元素然后导航到相应的GMSCameraPosition来实现从一个视图控制器到另一个视图控制器的表视图。功能调用有效,但相机由于某种原因不会更新。

以下是代码:

 import UIKit
 import GoogleMaps

 class PopUpViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {


@IBOutlet weak var buildingsList: UITableView!

let buildings = ["MESA Building",
                 "Science and Technology Building",
                 "Library Building",
                 "Student Activity Center Building (SAC)",
                 "Founders Building",
                 "Gym Complex",
                 "CEED Building",
                 "Arts Building"]


//var camera = GMSCameraPosition()

override func viewDidLoad() {
    super.viewDidLoad()

    self.showAnimate()

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    self.removeAnimate()
    //self.view.removeFromSuperview()
    //Do thing here
}



//
//TableView Functions
//


//Manages the sections of TabelView
func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

//Adds rows to tableView
public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

    return(buildings.count)

}

//Adds text to each cell/row
public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{

    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell.textLabel!.text = buildings[indexPath.row]

    return(cell)
}
//Action performed when cell is clicked
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {


    let row = buildings[indexPath.row]
    print(row)
    if (row == "MESA Building")
    {

    MapViewController().setLocation()
    self.removeAnimate()
    }

}




//
//Popup Functions
//
func showAnimate()
{
    self.view.transform = CGAffineTransform(scaleX: 0.25, y: 0.25)
    self.view.alpha = 0.0;
    UIView.animate(withDuration: 0.25, animations: {
        self.view.alpha = 1.0
        self.view.transform = CGAffineTransform(scaleX: 1, y: 1)
    });
}

func removeAnimate()
{
    UIView.animate(withDuration: 0.25, animations: {
        self.view.transform = CGAffineTransform(scaleX: 0.25, y: 0.25)
        self.view.alpha = 0.0;
    }, completion:{(finished : Bool)  in
        if (finished)
        {
            self.view.removeFromSuperview()
        }
    });
  }
}

的MapViewController

import UIKit
import MapKit
import GoogleMaps

class MapViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var googleMaps: GMSMapView!
@IBOutlet weak var buildingsTable: UITableView!

var locManager: CLLocationManager!

static var camera = GMSCameraPosition()
static var mapView = GMSMapView()

override func viewDidLoad() {
    super.viewDidLoad()

    locManager = CLLocationManager()
    locManager.delegate = self


    //default location for google maps
    MapViewController.camera = GMSCameraPosition.camera(withLatitude: 31.888401, longitude: -102.323992, zoom: 15.5)

    //mapView allows user to interact with the UI
    MapViewController.mapView = GMSMapView.map(withFrame: self.view.bounds, camera: MapViewController.camera)
    self.view.addSubview(MapViewController.mapView)
    //self.view = mapView

    //Creates the nav button on the UI to show the buildings
    let buildingsButton = UIButton()
    let image = UIImage(named: "nav")
    buildingsButton.frame = CGRect(x: self.view.frame.size.width - 65, y: 70, width: 57, height: 57)
    //button.setTitle("Name your Button ", for: .normal)
    buildingsButton.layer.shadowOpacity = 0.8
    buildingsButton.layer.shadowOffset = CGSize(width: 0, height: 2)
    buildingsButton.layer.shadowColor = UIColor.black.cgColor
    buildingsButton.setBackgroundImage(image, for: .normal)
    buildingsButton.addTarget(self, action: #selector(buildingsButtonTapped), for: .touchUpInside)
    self.view.addSubview(buildingsButton)


    //adds the compass button at the top of the UI
    MapViewController.mapView.settings.compassButton = true

    //shows visibility to the elements in the Google Maps UI
    MapViewController.mapView.accessibilityElementsHidden = false

    //checks if user allows their location to be used, then will find location on the map
    if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
        print("All set!")
        //finds the location of the user
        MapViewController.mapView.isMyLocationEnabled = true

        //allows the user to click on a button to recented their current location
        MapViewController.mapView.settings.myLocationButton = true

        //constantly updates the users location in the UI
        locManager.startUpdatingLocation()
    }
    else{
        print("Asking user for their location!")

        //Asks the user if they would like to share their location
        locManager.requestWhenInUseAuthorization()

            //finds the location of the user
            MapViewController.mapView.isMyLocationEnabled = true

            //allows the user to click on a button to recented their current location
            MapViewController.mapView.settings.myLocationButton = true

            //constantly updates the users location in the UI
            locManager.startUpdatingLocation()
        }

    //Array of buildings to be user as markers
    let buildings = [
        Building(name: "Library", lat: 31.888199, long: -102.328775),
        Building(name: "MESA Building", lat: 31.889575, long:-102.329833),
        Building(name: "Science and Technology (ST) Building", lat: 31.888787, long:-102.327587),
        Building(name: "Student Activity Canter (SAC) Building", lat: 31.889445, long:-102.328128),
        Building(name: "Founders Building", lat: 31.885989, long:-102.3222291),
        Building(name: "Gym Complex", lat: 31.890361, long:-102.328636),
        Building(name: "CEED Building", lat: 31.970513, long:-102.249466),
        Building(name: "Arts Building", lat: 31.891409, long:-102.327194),
        Building(name: "(New)Residence and Dining Hall", lat: 31.888886, long:-102.325978),

        ]


    //for loop using the array and structure to place the markers accordingly
    for building in buildings {
        let building_marker = GMSMarker()
        building_marker.position = CLLocationCoordinate2D(latitude: building.lat, longitude: building.long)
        building_marker.title = building.name
        //  building_marker.snippet = "Hey, this is \(building.name)"
        building_marker.map = MapViewController.mapView
        building_marker.appearAnimation = kGMSMarkerAnimationPop
    }

}


//structure that initializes the variables for the markers
struct Building {
    let name: String
    let lat: CLLocationDegrees
    let long: CLLocationDegrees
}

func buildingsButtonTapped(sender: UIButton!) {
    print("Button tapped")

    let popOverVC = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "sbPopUpID") as! PopUpViewController
    self.addChildViewController(popOverVC)
    popOverVC.view.frame = self.view.frame
    self.view.addSubview(popOverVC.view)
    popOverVC.didMove(toParentViewController: self)


    }

func setLocation(){


    MapViewController.camera = GMSCameraPosition.camera(withLatitude: 31.888199, longitude: -102.328775, zoom: 15.5)
    MapViewController.mapView = GMSMapView.map(withFrame: self.view.bounds, camera: MapViewController.camera)
    self.view.addSubview(MapViewController.mapView)

  }
}

0 个答案:

没有答案