2016-11-29 9 views
0

주석과 MKCircle로 표현 된 지역 타겟팅 영역을 생성하는 코드를 생성했습니다. 앱은 사용자가 지역을 입력하고 나갈 때이를 사용자에게 알립니다. 모든 것이 잘 작동하지만 난 여기에 몇 가지 내 코드의 조각입니다/표시 여러 영역 (하나의 주석/원이 표시됩니다)을 유지하기 위해 응용 프로그램을 얻을 방법을 알아낼 수 없습니다 :복수의 주석을 신속하게 코딩하기 2

override func viewDidLoad() { 
    super.viewDidLoad() 


//setup locationManager 
locationManager.delegate = self 
locationManager.distanceFilter = kCLLocationAccuracyNearestTenMeters 
locationManager.desiredAccuracy = kCLLocationAccuracyBest 
locationManager.requestAlwaysAuthorization() 

//setup mapView 
mapView.delegate = self 
mapView.showsUserLocation = true 
mapView.userTrackingMode = .Follow 

//setup test data will need to link coredata to pass in (LocationLabel, radius, address) 
    setupData("Test1", radius: 100, Address: "735 Main Rd, Clemson") 
    setupData("Test2", radius: 100, Address: "821 Main Rd, Clemson") 
    setupData("Test3", radius: 100, Address: "720 Main Rd, Clemson") 
} 

func setupData(Label: String, radius: Double, Address: String) { 
    // check if system can monitor regions 
    if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion.self) { 

     //region data need to put in its own class to read multiple regions 
     let title = Label 
     let regionRadius = radius // in meters 
     let address = Address // street, city, state zip 

     //takes in the address of a location and converts it into 2d coordinates (lat/long) 
     let geocoder = CLGeocoder() 
     geocoder.geocodeAddressString(address) { (placemarks, error) in 
      if let placemarks = placemarks { 
       if placemarks.count != 0 { 
        let coordinates = placemarks.first!.location 
        let coordinate = coordinates?.coordinate 

     //setup region this will read an object with a saved coordinate and name 
     var region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate!.latitude, 
      longitude: coordinate!.longitude), radius: regionRadius, identifier: title) 
     self.locationManager.startMonitoringForRegion(region) 

     //setup annotation 
     let annotation = MKPointAnnotation() 
     annotation.coordinate = coordinate!; 
     annotation.title = "\(title)"; 
     self.mapView.addAnnotation(annotation) 

     //setup circle 
     let circle = MKCircle(centerCoordinate: coordinate!, radius: regionRadius) 
     self.mapView.addOverlay(circle) 
    } 
    else { 
     print("System can't track regions") 
    } 
      } 
     } 
    } 
} 

답변

1

당신은 구현해야 MKMapViewDelegate의 renderForOverlay 함수는 사용자가 추가 한 오버레이를 실제로 표시합니다. 확대하지 않고 바로 볼 수있게하려면 반경을 늘리십시오.

func mapView(mapView: MKMapView, rendererForOverlay overlay: MKOverlay) -> MKOverlayRenderer 
{ 
    if let overlay = overlay as? MKCircle 
    { 
    let circleRenderer = MKCircleRenderer(circle: overlay) 
    circleRenderer.fillColor = UIColor.blueColor() 
    return circleRenderer 
    } 

    return MKOverlayRenderer(overlay: overlay) 
} 
+1

고맙습니다. – bgibers