2017-09-18 7 views

答えて

0

MKCircleを使用すると、マップトラッカーを簡単に作成できます。

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 
    } 
} 
関連する問題