1

我的代码:

请任何人!如何使此按钮起作用!?!??!

我在@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 居中到用户位置蓝点上?

4

3 回答 3

4

尝试这个

@IBAction func centerLocationButton(sender: UIButton) {
    mapView.setCenterCoordinate(mapView.userLocation.coordinate, animated: true)
}
于 2016-12-27T19:50:05.027 回答
1

为了以MKMapView正确的方式使用,只需遵循以下代码。

// `viewDidLoad` method
@IBOutlet weak var map: MKMapView!
var locationManager: CLLocationManager!

override func viewDidLoad() {
        super.viewDidLoad()

        // code for enable location seivices
        if (CLLocationManager.locationServicesEnabled())
        {
            locationManager = CLLocationManager()
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            locationManager.requestAlwaysAuthorization()
            locationManager.startUpdatingLocation()
        }
    }

覆盖 CLLocationManager.didUpdateLocations 见下文(CLLocationManagerDelegate 的一部分),以便在位置管理器检索当前位置时得到通知。

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    let location = locations.last as CLLocation

    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)        
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))

    self.map.setRegion(region, animated: true)
}

请注意:如果您的目标是 iOS 8,则必须在 Info.plist 中包含 NSLocationAlwaysUsageDescription 键才能使位置服务正常工作。

如果您想使用按钮操作以自定义方式设置区域,只需保存latlong传递如下:

@IBAction func setMap(sender: UIButton) {
    let center = CLLocationCoordinate2D(latitude: lat, longitude: long)        
    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
    self.map.setRegion(region, animated: true)
}

这是谷歌地图指南。和有用的教程

于 2016-08-24T10:17:22.563 回答
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

}

确保在使用模拟器时只需从模拟器菜单中添加自定义位置。调试 ---> 位置 ----> 自定义 如果使用真正的手机,只需确保在设置中打开位置服务---> 常规----> 隐私

于 2016-09-23T10:51:54.733 回答