按下按钮中心\将MKmapView聚焦到用户的位置,Swift

时间:2016-08-22 08:00:57

标签: swift xcode mkmapview userlocation

我的代码:

请任何人!如何使这个按钮工作!?!??!

我在@IBAction括号中放置了什么以获得我想要的功能?

    import UIKit
    import MapKit
    import CoreLocation

class MapViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate{



        @IBOutlet weak var mapView: MKMapView!



        let locationManager = CLLocationManager()

        override func viewDidLoad() {
            super.viewDidLoad()

            //==========RegionLocation : =========

            // Init the zoom level
            let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 31.30, longitude: 34.45)
            let span = MKCoordinateSpanMake(125, 125)
            let region = MKCoordinateRegionMake(coordinate, span)
            self.mapView.setRegion(region, animated: true)


            //====================================\\
        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
        //Dispose of resources that can be re created.

            }

        //Mark : Location

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



        {
            let location = locations.last

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

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

            self.mapView.setRegion(region, animated: true)

            self.locationManager.stopUpdatingLocation()
        }

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

        func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
            if annotation is MKUserLocation {
                return nil
            }
            let reuseID = "pin"
            var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
            if(pinView == nil) {
                pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
                pinView!.canShowCallout = true
                pinView!.animatesDrop = true
                pinView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIButton
                let smallSquare = CGSize(width: 30, height: 30)
                let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
                button.setBackgroundImage(UIImage(named: "Car"), forState: .Normal)
                pinView?.leftCalloutAccessoryView = button
            }
            else
            {
                pinView!.annotation = annotation
            }


        return pinView

    }

但是当我按下按钮动作片段时,按下它时没有任何反应?

任何人都可以指导我如何将mapView集中到用户位置蓝点?

2 个答案:

答案 0 :(得分:3)

试试这个

@IBAction func centerLocationButton(sender: UIButton) {
    mapView.setCenterCoordinate(mapView.userLocation.coordinate, animated: true)
}

答案 1 :(得分:0)

我可以看到你需要的是一个按钮,将mapView置于当前位置的中心位置。它与vaibhab的答案完全相同,几乎没有任何修改。 只需创建按钮并将动作链接到viewcontroller类,然后在按钮的IBAction中复制相同的代码。

@IBAction func updateCurrentLocation(_ sender: AnyObject) {

    if CLLocationManager.locationServicesEnabled() {

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    } else {

        // Alert to enable location services on iphone first
    }
}

另外,您只需添加一小段代码即可显示用户位置的蓝色指示符。在func locationManager。

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

    self.mapView.showsUserLocation = true

}
  

确保在使用模拟器时只需从模拟器菜单中添加自定义位置。调试---> location ---->习惯   如果使用真实移动设备,请确保在设置--->中启用了位置服务。一般---->隐私