ios - 在 Swift 中构建地理定位

标签 ios swift annotations mapkit clregion

我正在尝试使用 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/

相关文章:

ios - 如何在Xamarin iOS项目中无需从设备手动删除App的情况下更新启动画面?

ios - DiffableDataSource - 没有动画,但在删除操作期间闪烁

java - 如何在调用方法之前使方法成为被动方法

ios - 带部分的动态 TableView

ios - 使用 SKReferenceNode 引用的 SpriteKit 场景未加载自定义类

ios - 如何让应用程序居中和/或填满屏幕?

ios - 如何在 Swift 4 中设置设备的闪光灯模式?

ios - Swift:在 Struct 中声明委托(delegate)

java - <MVC :annotation-driven/> with un-annotated controllers

java - 在 Java 中使用注解进行授权检查