2016-08-22 126 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