         
         //TODO: handle the wrap condition
         let latitudeDelta = max(0.01, 1.3*(latitudeMax - latitudeMin))
-        let longitudeDelta = max(0.01, 1.3*(longitudeMax - longitudeMin))
-        
-        let averageLatitude = latitudeSum/CLLocationDegrees(articles.count)
-        let averageLongitude = longitudeSum/CLLocationDegrees(articles.count)
+        let longitudeDelta = max(0.01,1.3*(longitudeMax - longitudeMin))
+        
+        let averageLatitude = latitudeSum/CLLocationDegrees(coordinates.count)
+        let averageLongitude = longitudeSum/CLLocationDegrees(coordinates.count)
         
         let center = CLLocationCoordinate2D(latitude: averageLatitude, longitude: averageLongitude)
         let span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta)

    var articleStore: WMFArticleDataStore!
    var dataStore: MWKDataStore!
    var segmentedControl: UISegmentedControl!
    
    var currentGroupingPrecision: QuadKeyPrecision = 1
    
    let searchHistoryGroup = "PlaceSearch"

        case .top:
            break
        case .location:
            let fail = {
                dispatchOnMainQueue({
                    if let region = search.region {
                        self.mapRegion = region
                    }
                    self.searching = false
                    self.currentSearch = PlaceSearch(type: .top, sortStyle: WMFLocationSearchSortStyleLinks, string: nil, region: nil, localizedDescription: localizedStringForKeyFallingBackOnEnglish("places-search-top-articles-nearby"), articleKey: nil)
                })
            }
            guard let key = search.articleKey,
                    let articleURL = URL(string: key),
                    let title = (articleURL as NSURL).wmf_title,
                    let language = (articleURL as NSURL).wmf_language  else {
                fail()
                return
            }
            var components = URLComponents()
            components.host = "wikidata.org"
            components.path = "/w/api.php"
            components.scheme = "https"
            let actionQueryItem = URLQueryItem(name: "action", value: "wbgetentities")
            let titlesQueryItem = URLQueryItem(name: "titles", value: title)
            let sitesQueryItem = URLQueryItem(name: "sites", value: "\(language)wiki")
            let formatQueryItem = URLQueryItem(name: "format", value: "json")
            components.queryItems = [actionQueryItem, titlesQueryItem, sitesQueryItem, formatQueryItem]
            
            guard let requestURL = components.url else {
                fail()
                return
            }
            
            let northernmostPointKey = "P1332"
            let southernmostPointKey = "P1333"
            let easternmostPointKey = "P1334"
            let westernmostPointKey = "P1335"
            let keys = [northernmostPointKey, southernmostPointKey, easternmostPointKey, westernmostPointKey]
            URLSession.shared.dataTask(with: requestURL, completionHandler: { (data, response, error) in
                guard let data = data else {
                    fail()
                    return
                }
                do {
                    guard let jsonObject = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any] else {
                        fail()
                        return
                    }
                    guard let entities = jsonObject["entities"] as? [String: Any] else {
                        fail()
                        return
                    }
                    guard let entity = entities.values.first as? [String: Any] else {
                        fail()
                        return
                    }
                    guard let claims = entity["claims"] as? [String: Any] else {
                        fail()
                        return
                    }
                    
                    let coordinates = keys.flatMap({ (key) -> CLLocationCoordinate2D? in
                        guard let values = claims[key] as? [Any] else {
                            return nil
                        }
                        guard let point = values.first as? [String: Any] else {
                            return nil
                        }
                        guard let mainsnak = point["mainsnak"] as? [String: Any] else {
                            return nil
                        }
                        guard let datavalue = mainsnak["datavalue"] as? [String: Any] else {
                            return nil
                        }
                        guard let value = datavalue["value"] as? [String: Any] else {
                            return nil
                        }
                        guard let latitude = value["latitude"] as? CLLocationDegrees,
                            let longitude = value["longitude"] as? CLLocationDegrees else {
                                return nil
                        }
                        return CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
                    })
                    
                    guard coordinates.count > 0 else {
                        fail()
                        return
                    }
                    
                    dispatchOnMainQueue({ 
                        self.mapRegion = self.regionThatFits(coordinates: coordinates)
                        self.searching = false
                         self.currentSearch = PlaceSearch(type: .top, sortStyle: WMFLocationSearchSortStyleLinks, string: nil, region: nil, localizedDescription: localizedStringForKeyFallingBackOnEnglish("places-search-top-articles-nearby"), articleKey: nil)
                    })
                } catch let parseError {
                    DDLogError("error parsing JSON \(parseError)")
                }
            }).resume()
            return
        case .text:
            fallthrough
        default:
