2015-07-05 50 views
0

我遇到了一個問題,我想通過GPS獲取用戶的位置。但是,我不能配合地圖視圖,但只有GPS是好的。我想擁有這個位置並將其作爲地圖視圖上的註釋。 以下是Swift中的代碼。iOS MapView無法與GPS服務合作

import UIKit 
import MapKit 
import CoreLocation 

class MapViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{ 

    @IBOutlet weak var mapView: MKMapView! 

    let locationManager = CLLocationManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 




    @IBOutlet weak var LocateBtn: UIButton! 

    @IBAction func Locate(sender: UIButton) { 
     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
     locationManager.requestWhenInUseAuthorization() 
     locationManager.startUpdatingLocation() 


    } 


    func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { 
     CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in 

      println(2) 
      if (error != nil) { 
       println("Reverse geocoder failed with error" + error.localizedDescription) 
       return 
      } 

      if placemarks.count > 0 { 
       println(3) 
       let pm = placemarks[0] as! CLPlacemark 
       self.displayLocationInfo(pm) 
      } else { 
       println("Problem with the data received from geocoder") 
      } 
     }) 
    } 


    func displayLocationInfo(placemark: CLPlacemark?) { 
     if let containsPlacemark = placemark { 
      //stop updating location to save battery life 
      locationManager.stopUpdatingLocation() 
      let locality = (containsPlacemark.locality != nil) ? containsPlacemark.locality : "" 
      let postalCode = (containsPlacemark.postalCode != nil) ? containsPlacemark.postalCode : "" 
      let administrativeArea = (containsPlacemark.administrativeArea != nil) ? containsPlacemark.administrativeArea : "" 
      let country = (containsPlacemark.country != nil) ? containsPlacemark.country : "" 
      let locations = Locations() 
      var annotations = [MKPointAnnotation]() 


      for dictionary in locations { 


       let coordinate = containsPlacemark.location.coordinate 
       let first = dictionary["firstName"] as! String 
       let last = dictionary["lastName"] as! String 
       let mediaURL = dictionary["mediaURL"] as! String 

       var annotation = MKPointAnnotation() 
       annotation.coordinate = coordinate 
       annotation.title = "\(first) \(last)" 
       annotation.subtitle = mediaURL 

       annotations.append(annotation) 
      } 
      println(annotations) 
      self.mapView.addAnnotations(annotations) 
     } 

    } 


    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

    func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 
      let reusedId = "pin" 

      var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reusedId) as? MKPinAnnotationView 

     if pinView == nil { 
      pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reusedId) 
      pinView!.canShowCallout = true 
      pinView!.pinColor = .Red 
      pinView!.rightCalloutAccessoryView = UIButton.buttonWithType(.DetailDisclosure) as! UIButton 

     } else { 
      pinView!.annotation = annotation 
     } 
     return pinView 
    } 


    func mapView(mapView: MKMapView!, annotationView: MKAnnotationView!, calloutAccessoryControlTapped control: UIControl!) { 

     if control == annotationView.rightCalloutAccessoryView { 
      let app = UIApplication.sharedApplication() 
      app.openURL(NSURL(string: annotationView.annotation.subtitle!)!) 
     } 
    } 

    func Locations() -> [[String : AnyObject]] { 
     return [ 
      [ 
       "createdAt" : "2015-7-3T21:50:00", 
       "firstName" : "Joe", 
       "lastName" : "Li", 
       "latitude" : 28.1461248, 
       "longitude" : -82.75676799999999, 
       "mapString" : "Tarpon Springs, FL", 
       "mediaURL" : "www.linkedin.com/in/jessicauelmen/en", 
       "objectId" : "kj18GEaWD8", 
       "uniqueKey" : 872458750, 
       "updatedAt" : "2015-7-4T21:00:00" 
      ] 
     ] 
    } 

} 

回答

0

看看這個教程Here

試試這個:

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