Im trying to implement geo targeting functionality with CLRegion. But When I run this app the MKCircle didn't show up, can anyone tell me which part is wrong?
func setupData(){
if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion.self){
let tripspotRegion = [
tripSpot( title: "一中商圈", coordinate: CLLocationCoordinate2DMake(24.149062, 120.684891), regionRadius: 300.0, location: "台中一中", type: "food"),
tripSpot( title: "逢甲夜市", coordinate: CLLocationCoordinate2DMake(24.180407, 120.645086), regionRadius: 300.0, location:"台中逢甲", type: "food"),
tripSpot( title: "東海商圈", coordinate: CLLocationCoordinate2DMake(24.181143, 120.593158), regionRadius: 300.0, location: "東海商圈", type: "food")]
//set annotation
let coordinate = CLLocationCoordinate2D()
let regionRadius = 300.0
let title = "title"
let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: coordinate.latitude,longitude: coordinate.longitude), radius: regionRadius, identifier: title)
//set annotation
let tripSpotAnnotation = MKPointAnnotation()
tripSpotAnnotation.coordinate = coordinate
tripSpotAnnotation.title = "\(title)"
mapView.addAnnotations(tripspotRegion)
locationManager.startMonitoringForRegion(region)
// draw a circle
let circle = MKCircle(centerCoordinate: coordinate, radius: regionRadius)
mapView.addOverlay(circle)
}
// check if can monitor region
else{
print("system can't track regions")
}
}
You have put three coordinates in an array, but then you aren't doing anything with them. You are creating a new coordinate
but not initialising it, so your region and overlay will be at (0,0). I think you meant to use the tripspotArray
to add the regions/overlays:
func setupData(){
if CLLocationManager.isMonitoringAvailableForClass(CLCircularRegion.self) {
let tripspotRegion = [
tripSpot( title: "一中商圈", coordinate: CLLocationCoordinate2DMake(24.149062, 120.684891), regionRadius: 300.0, location: "台中一中", type: "food"),
tripSpot( title: "逢甲夜市", coordinate: CLLocationCoordinate2DMake(24.180407, 120.645086), regionRadius: 300.0, location:"台中逢甲", type: "food"),
tripSpot( title: "東海商圈", coordinate: CLLocationCoordinate2DMake(24.181143, 120.593158), regionRadius: 300.0, location: "東海商圈", type: "food")]
for aSpot in tripspotRegion {
//set annotation
let coordinate =aSpot.coordindate
let regionRadius = aSpot.regionRadius
let title = aSpot.title
let region = CLCircularRegion(center: coordinate, radius: regionRadius, identifier: title)
//set annotation
let tripSpotAnnotation = MKPointAnnotation()
tripSpotAnnotation.coordinate = coordinate
tripSpotAnnotation.title = title
mapView.addAnnotations([tripSpotAnnotation])
locationManager.startMonitoringForRegion(region)
// draw a circle
let circle = MKCircle(centerCoordinate: coordinate, radius: regionRadius)
mapView.addOverlay(circle)
}
} else{
print("system can't track regions")
}
}