2017-09-18 5 views

Répondre

0

En utilisant MKCircle, vous pouvez facilement faire tracker de la carte.

enter image description here

import UIKit 
import MapKit 

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { 

    @IBOutlet weak var mapView: MKMapView! 
    var locationManager: CLLocationManager! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     mapView.showsUserLocation = true 

     if CLLocationManager.locationServicesEnabled() { 
      locationManager = CLLocationManager() 
      locationManager.delegate = self 
      locationManager.desiredAccuracy = kCLLocationAccuracyBest 
      locationManager.requestWhenInUseAuthorization() 
      locationManager.startUpdatingLocation() 
     } 
    } 

    // MARK: - MKMapView delegate 

    func mapView(_ mapView: MKMapView, didUpdate userLocation: MKUserLocation) { 
     let span = MKCoordinateSpanMake(0.01, 0.01) 
     let region = MKCoordinateRegionMake(userLocation.coordinate, span) 
     mapView.setRegion(region, animated:true) 

     let center = userLocation.coordinate 
     let circle = MKCircle(center: center, radius: 10) 
     mapView.add(circle) 
    } 

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer { 
     let circleRenderer : MKCircleRenderer = MKCircleRenderer(overlay: overlay); 
     circleRenderer.fillColor = .blue 
     circleRenderer.lineWidth = 1.0 
     return circleRenderer 
    } 
}