【问题标题】:How to capture filtered values in new array如何在新数组中捕获过滤值
【发布时间】:2017-09-04 15:59:52
【问题描述】:

我正在实现一个“在我周围搜索”功能,类似于可以在谷歌地图上找到的功能。我有一个名为“locations”的数组,其中填充了如下值:

mapPoints(Borough: "Manhattan", Neighborhood: "Battery Park City", Latitude: 40.7117, Longitude: -74.0158),

函数“myPlaces()”是实现“搜索我”功能的地方。当按下按钮时,“myPlaces()”被触发。里面是 func myPlaces(){

        for location in locations{


            let userLocation = mapView.userLocation
            let userLat = userLocation?.coordinate.latitude
            let userLong = userLocation?.coordinate.longitude

            let coordinateOne = CLLocation(latitude: userLat!, longitude: userLong!)
            let coordinateTwo = CLLocation(latitude: location.latitude, longitude: location.longitude)

            let distanceFromPoints = coordinateOne.distance(from: coordinateTwo)
            let convertToMiles = distanceFromPoints*0.00062137
            if convertToMiles >= 2 {
                print("It's greater than set amount")
            } else{
                let annotation = MGLPointAnnotation()
                filteredLocations = locations
                print("amount to come out", filteredLocations.count)
                annotation.title = location.Neighborhood
                annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
                mapView.addAnnotation(annotation)
                mapView.setCenter((mapView.userLocation?.coordinate)!, zoomLevel: 11, animated: true)

                }
            }
    animateOut()
}

现在的问题是我需要取小于 2 的值,并将它们放在一个名为“filteredLocationsmapPoints”的新数组中,这样我就可以在我的其余代码中访问它们

【问题讨论】:

    标签: ios arrays swift sorting filter


    【解决方案1】:

    你在寻找这样的东西吗?

    let filteredLocations = locations.filter {
       (location) in 
       let userLocation = mapView.userLocation
       let userLat = userLocation?.coordinate.latitude
       let userLong = userLocation?.coordinate.longitude
    
       let coordinateOne = CLLocation(latitude: userLat!, longitude: userLong!)
       let coordinateTwo = CLLocation(latitude: location.latitude, longitude: location.longitude)
    
       let distanceFromPoints = coordinateOne.distance(from: coordinateTwo)
       let convertToMiles = distanceFromPoints*0.00062137
       return convertToMiles < 2
    }
    

    使用过滤器会将您的数组构建为单个功能操作,而不是命令式循环方法。这很有效,因为您已经有了识别所需逻辑的逻辑。

    使用 map 语句获取您的注释,这会遍历一个集合以产生另一个东西。

    let annotations = filteredLocations.map {
       (location) in
       let annotation = MGLPointAnnotation()
       annotation.title = location.Neighborhood
       annotation.coordinate = // etc
       return annotation
    }
    

    然后,您可以使用 for 语句简单地将注释添加到地图中(或者,如果它符合您的喜好,我猜在映射函数中,但由于这是“副作用”而不起作用)

    for annotation in annotations { 
       mapView.addAnnotation(annotation) 
    }
    
    mapView.setCenter((mapView.userLocation?.coordinate)!, zoomLevel: 11, animated: true)
    

    这还有一个好处是设置地图视图的中心一次,而不是在 for 循环中!

    【讨论】:

    • 感谢您的回复!我如何能够添加filteredLocations中的注释点[原始函数中“else”语句之后的部分]
    • 已更新注释!
    【解决方案2】:

    在类的顶部创建一个新数组,将其命名为 filtersLocationsmapPoints

    var filteredLocationsmapPoints = [CGFloat]()
    

    现在在你的 for 循环中添加小于 2 的那些

    for location in locations{
    
    
            let userLocation = mapView.userLocation
            let userLat = userLocation?.coordinate.latitude
            let userLong = userLocation?.coordinate.longitude
    
            let coordinateOne = CLLocation(latitude: userLat!, longitude: userLong!)
            let coordinateTwo = CLLocation(latitude: location.latitude, longitude: location.longitude)
    
            let distanceFromPoints = coordinateOne.distance(from: coordinateTwo)
            let convertToMiles = distanceFromPoints*0.00062137
            if convertToMiles >= 2 {
                print("It's greater than set amount")
            } else{
                // here you append the values which are less than 2
                filteredLocationsmapPoints.append(convertToMiles)
    
                let annotation = MGLPointAnnotation()
                filteredLocations = locations
                print("amount to come out", filteredLocations.count)
                annotation.title = location.Neighborhood
                annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
                mapView.addAnnotation(annotation)
                mapView.setCenter((mapView.userLocation?.coordinate)!, zoomLevel: 11, animated: true)
    
                }
            }
    animateOut()
    

    }

    【讨论】:

      猜你喜欢
      • 2016-12-10
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 2016-11-06
      • 2011-11-06
      • 1970-01-01
      • 2021-11-06
      • 1970-01-01
      相关资源
      最近更新 更多