2016-06-29 13 views
0

Ich habe Probleme, einen transparenten Kreis über meine Karte am Standort lang: 39 und lat: 77. Ich muss auch mehrere Kreise gleichzeitig anzeigen. Ich bin neu im Umgang mit Mapkit, also sei einfach. Danke für die Hilfe im Voraus.Mapkit Overlay Problem in Swift

class FirstViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate 
{ 

@IBOutlet weak var mapView: MKMapView! 

let locationManager = CLLocationManager() 

override func viewDidLoad() 
{ 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
    self.locationManager.delegate = self 
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    self.locationManager.requestAlwaysAuthorization() 
    self.locationManager.startUpdatingLocation() 
    self.mapView.showsUserLocation = true 

} 
override func didReceiveMemoryWarning() 
{ 
    super.didReceiveMemoryWarning() 
} 
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: 1, longitudeDelta: 1)) 
    self.mapView.setRegion(region, animated: true) 
    self.locationManager.stopUpdatingLocation()// 
} 
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) 
{ 
    print("Errors: " + error.localizedDescription) 
} 

} 

Antwort

1
class FirstViewController { 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 
     self.locationManager.delegate = self 
     self.locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     self.locationManager.requestAlwaysAuthorization() 
     self.locationManager.startUpdatingLocation() 
     self.mapView.showsUserLocation = true 

    } 

    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: 1, longitudeDelta: 1)) 
     self.mapView.setRegion(region, animated: true) 
     self.locationManager.stopUpdatingLocation() 
     // Provide a radius in meters 
     showCircle(center, radius: <#CLLocationDistance#>) 
    } 

    // Radius is measured in meters 
    func showCircle(coordinate: CLLocationCoordinate2D, radius: CLLocationDistance) { 
     let circle = MKCircle(centerCoordinate: coordinate, radius: radius) 
     mapView.addOverlay(circle) 
    } 

} 

extension FirstViewController: MKMapViewDelegate { 
    func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer { 
     let circleRenderer = MKCircleRenderer(overlay: circleOverlay) 
     circleRenderer.fillColor = UIColor.blackColor() 
     circleRenderer.alpha = 0.1 

     return circleRenderer 
    } 
} 
+0

get Fehler auf line extension sagen „Erklärung in Dateigültigkeitsbereich ist nur gültig“ – Steve

+0

Ja, muss die Erweiterung außerhalb der Klasse FirstViewController sein. Ich habe es aktualisiert, um das explizit zu machen. – CodeBender