2016-11-29 6 views
0

注釈とMKCircleで表される地域ターゲティング領域を生成するコードを作成しました。アプリは、ユーザーが地域に入ったり出たりしたときにユーザーに通知します。すべてが正常に動作しているが、私は、アプリを保持するために取得する方法を見つけ出すことはできません/表示複数の領域(一つだけ注釈/円が表示されます)ここに私のコードの抜粋です:複数の注釈をコード化するswift 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

関連する問題