我正在尝试使用CLRegion实现地理位置定位功能。但是当我运行这个应用程序时,MKCircle没有出现,有人能告诉我哪个部分出错了吗?
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")
}
}
答案 0 :(得分:1)
你已经在一个数组中放了三个坐标,但是你没有对它们做任何事情。您正在创建新的coordinate
但未初始化它,因此您的区域和叠加层将位于(0,0)。我认为您打算使用tripspotArray
添加区域/叠加层:
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")
}
}