dimanche 8 juillet 2018

Multiple While/For condition with If condition causes time lag while iterating

I have to add Markers(around 2000 values comes from JSON but i would like to reduce it based on the radius condition written) in Map View, i have added the to reduce the array based on the overlapping distance.When i add If condition inside While/For loop statement for finding if the array not contains the element, it takes more time say more than 10 minutes.Any help would be appreciated, i am struck for more than a day in this.

    func reducePolygonArray() {
    var overlappingPolygonArrayList:[Gas] = []
    var checkedPolygonArrayList:[Gas] = []

    var iterator = self.polygonArray.makeIterator()
    while let polygon = iterator.next()  {
      checkedPolygonArrayList.append(polygon)
      let centerLatLng = CLLocation(latitude: polygon.lat, longitude: polygon.long)
      var iterator = self.polygonArray.makeIterator()
      while let Rpolygon = iterator.next()  {
        let LatLng = CLLocation(latitude: Rpolygon.lat, longitude: Rpolygon.long)
        let overlapDistance = sqrt(2) * mEpaRadius
        let coordinatesDistance = centerLatLng.distance(from: LatLng)
        if(Int(coordinatesDistance) < Int(overlapDistance) && !checkedPolygonArrayList.contains(Rpolygon)) {
          overlappingPolygonArrayList.append(Rpolygon)
          checkedPolygonArrayList.append(Rpolygon)
        }
      }
    }
    let elapsed = NSDate.timeIntervalSinceReferenceDate - start
    print("iterating through the loop took \(elapsed) seconds")
    print("overlapping PolygonArrayList count",overlappingPolygonArrayList.count)
    if(overlappingPolygonArrayList.count > 0) {
      self.mapView.clear()
      var i = 0
      for Polygon in self.polygonArray {
        i = i+1
        if(!overlappingPolygonArrayList.contains(Polygon)) {
          let aqiIndex = calculateAqiIndexEPAData(ozone: Polygon.o3, pm2_5: Polygon.pm2, pm10: Polygon.pm10, co: Polygon.co, so2: Polygon.so2, nox: Polygon.nox)
//create polygon
          createPolygonEPA(lat: Polygon.lat.roundTo(places: 2), lng: Polygon.long.roundTo(places: 2), ozone: Polygon.o3, co: Polygon.co, so2: Polygon.so2, aqiIndex: aqiIndex, no2: Polygon.no2, index:i)
        }
      }
    }

}

Aucun commentaire:

Enregistrer un commentaire