按下按钮时打印用户坐标 - Swift

时间:2016-09-11 11:36:45

标签: ios swift coordinates

当我按下"分享"如何打印用户位置?按钮?这是我的代码

正如你所看到的,我有一个打印出坐标的行/代码,但是当我按下分享按钮时我想要打印出这些坐标。我可以这样做,如果,怎么样?

希望你明白我的意思。

我是Xcode和Swift的新手!

import UIKit
import MapKit
import Firebase

class MapController:  UICollectionViewController, UICollectionViewDelegateFlowLayout, CLLocationManagerDelegate, MKMapViewDelegate {


var window: UIWindow?
var mapView: MKMapView?
let locationManager = CLLocationManager()

let distanceSpan: Double = 500

var locationData: CLLocation!

let coordLabel = UILabel(frame: CGRectMake(10,100,400,50))


override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Share", style: .Plain, target: self, action: #selector(share))


    navigationItem.title = "Current Location"

    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.view.backgroundColor = UIColor.whiteColor()

    self.mapView = MKMapView(frame: CGRectMake(0, 20, (self.window?.frame.width)!, (self.window?.frame.height)!))
    self.view.addSubview(self.mapView!)

    self.locationManager.delegate = self
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    self.locationManager.requestWhenInUseAuthorization()
    self.locationManager.startUpdatingLocation()
    self.mapView!.showsUserLocation = true


    //Show User Location Button
    let button: UIButton = UIButton(type: UIButtonType.Custom)
    button.setImage(UIImage(named: "MyLocationButton"), forState: UIControlState.Normal)
    button.addTarget(self, action: #selector(locationManagerButton), forControlEvents: UIControlEvents.TouchUpInside)
    button.frame = CGRectMake(0, 0, 30, 30)
    let barButton = UIBarButtonItem(customView: button)
    self.navigationItem.leftBarButtonItem = barButton

    // user is not logged in
    if FIRAuth.auth()?.currentUser?.uid == nil {
        performSelector(#selector(handleLogout), withObject: nil, afterDelay: 0)



 }

    view.addSubview(coordLabel)

}

func share() {

    coordLabel.text = String(locationData.coordinate.longitude) + ", " +   String(locationData.coordinate.latitude)

}


func handleLogout() {

    do {
        try FIRAuth.auth()?.signOut()
    } catch let logoutError {
        print(logoutError)
    }

    let loginController = LoginController()
    presentViewController(loginController, animated: true, completion: nil)

}




func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation) {
    if let mapView = self.mapView {
        let region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, self.distanceSpan, self.distanceSpan)
        mapView.setRegion(region, animated: true)
        mapView.showsUserLocation = true
    }
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let locationData = locations.last

    let center = CLLocationCoordinate2D(latitude: locationData!.coordinate.latitude, longitude: locationData!.coordinate.longitude)

    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.mapView!.setRegion(region, animated: true)
    self.locationManager.stopUpdatingLocation()

    let locValue:CLLocationCoordinate2D = manager.location!.coordinate
    print("locations = \(locValue.latitude) \(locValue.longitude)")




}

func locationManagerButton() {

    mapView!.setCenterCoordinate(mapView!.userLocation.coordinate, animated: true)



}


func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
    print("Errors: " + error.localizedDescription)
}


}

1 个答案:

答案 0 :(得分:2)

您需要一个包含输出坐标的标签。

coordLabel = UILabel(frame: CGRectMake(10,100,400,50))
view.addSubview(coordLabel);

然后您将需要一个全局坐标变量,该变量在委派的调用中得到更新。所以,在let distanceSpan: Double = 500之后

var locationData: CLLocation!;

然后在委托方法中更新全局值...所以代替location = locations.last放置locationData = locations.last并在需要时在其他地方更新变量名称。

最后,在需要时输出位置数据。

func share(){
    coordLabel.text = String(locationData.coordinates.longitude) + ", " +   String(locationData.coordinates.latitude)
}

这是乍一看......你可能遇到一些问题......让我发布。