2016-08-22 129 views
1

我的代码:按下按钮中心将MKmapView聚焦到用户的位置上,Swift

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

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

+0

您的plist文件''requestAlwaysAuthorization'里面requestWhenInUseAuthorization'添加键.. – vaibhav

+0

两者或只是其中之一? :O –

+0

可以同时添加.. – vaibhav

回答

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) 
} 

这里是Google map guidelines。和useful tutorial

+0

什么都行不通.......... –

+0

你能分享你的代码吗? – vaibhav

+0

我会......... –

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 

} 

确保当您使用模拟器刚刚从模拟器菜单中添加自定义位置。调试--->的位置---->自定义 如果使用的是真正的移动只是确保位置服务是在设置 - >通用---->隐私

3

试试这个

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

感谢您! – grabury