2016-06-28 2 views
1

透明な円や矩形をユーザーの場所から一意の任意の場所に表示する方法を見つけるのに問題があります。私はmapkit初心者ですので、事前に感謝します。mapkitと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) 
} 

} 

答えて

2

明らかに、MapKitがインポートされていることを確認してください。

class Map { 

    func setup() { 
     // Assign delegate here. Can call the circle at startup, or at a later point using the method below. Includes <# #> syntax to simplify code completion. 
     mapView.delegate = self 
     showCircle(<#CLLocationCoordinate2D#>, radius: <#CLLocationDistance#>) 
    } 

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

extension Map: MKMapViewDelegate { 
    func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer { 
     // If you want to include other shapes, then this check is needed. If you only want circles, then remove it. 
     if let circleOverlay = overlay as? MKCircle { 
      let circleRenderer = MKCircleRenderer(overlay: circleOverlay) 
      circleRenderer.fillColor = UIColor.blackColor() 
      circleRenderer.alpha = 0.1 

      return circleRenderer 
     } 

     // You can either return your square here, or ignore the circle check and only return circles. 
     return <#Another overlay type#> 
    } 
} 
+0

別のオプションは、その円のイメージでカスタム注釈を作成することです。私はちょうどあなたがそのタイプの機能性を望んでいたという条件の下でこれを投げているだけです – Sethmr