gpt4 book ai didi

ios - 在 Swift 中构建地理定位

转载 作者:行者123 更新时间:2023-11-29 00:59:21 25 4
gpt4 key购买 nike

我正在尝试使用 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,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")
}
}

关于ios - 在 Swift 中构建地理定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37202261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com