在 Swift 中建立地理定位
Building a Geo Targeting in Swift
我正在尝试使用 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")
}
}
您已将三个坐标放入一个数组中,但您没有对它们进行任何操作。您正在创建一个新的 coordinate
但没有对其进行初始化,因此您的区域和叠加层将位于 (0,0)。我认为您打算使用 tripspotArray
添加 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")
}
}
我正在尝试使用 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")
}
}
您已将三个坐标放入一个数组中,但您没有对它们进行任何操作。您正在创建一个新的 coordinate
但没有对其进行初始化,因此您的区域和叠加层将位于 (0,0)。我认为您打算使用 tripspotArray
添加 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")
}
}