简体   繁体   中英

Swift - searching through dictionaries in an array in a plist

I'm making a simple app that searches through a large plist that contains geographical fixes, with corresponding lat and long coordinates. The plan is to be able to search for these fixes in a search box, and then push a button to position a mapview with the fix in the center, and a pin at the exact coordinates.

I have the interface setup, the map working and just need to enable the button to search for the fix that the user inputs, return the lat long, and forward these to the map. I've been looking through answers on here for hours now and tried versions of what I found with no luck..

This is the plist:

在此处输入图片说明

It is quite a lot of fixes as you can see... What would be the best way to go about this? I'm at a loss how to go on from here.

This is what I'm using to position the map

func zoomToRegion() {
  let location = CLLocationCoordinate2D(latitude: 62, longitude: 16)
  let region = MKCoordinateRegionMakeWithDistance(location, 700000.0, 900000.0)

  mapView.setRegion(region, animated: true)
}

As you see right now I have just manually entered a lat and long but it works anyway.

The closest I came to making it work was following this guide: http://rshankar.com/how-to-add-mapview-annotation-and-draw-polyline-in-swift/ However I don't want the fixes to be on the map until someone searches for them, and I was unable to convert that code to do what I wanted..

Thanks for any help or insight into this!

I'd rather store them lat & long as Double, but anyway, you should create an NSArray first from that .plist ( Read from .plist ) Then filter that array, whether it stores the same lat & long values that the user typed in.

guard let filePath = NSBundle.mainBundle().pathForResource("YOUR_FILE_NAME", ofType: "plist"),    
let locationsArray = NSArray(contentsOfFile:filePath) else {
    print("Couldn't load .plist as an array")
    return
}

// TODO: Replace them with the actual user input
let userInputText: String = "ALABA"

// It may not contain any items
let filteredLocations = locationsArray.filter { (element) -> Bool in
    guard let title = element["title"] as? String where
      userInputText == title else { return false }
    return true
}

// To convert String lat & long to Double
let numberFormatter = NSNumberFormatter()
numberFormatter.decimalSeparator = ","

// If you only need the first result:
guard let locationDict = filteredLocations.first as? [String:AnyObject],
    let latString = location["lat"] as? String, 
    let longString = location["long"] as? String,
    let latitude = numberFormatter.numberFromString(latString)?.doubleValue,
    let longitude = numberFormatter.numberFromString(longString)?.doubleValue else {
       print("No result"); return
    }

// Your location
let location = CLLocationCoordinate2D(
    latitude: latitude, 
    longitude: longitude
)

// Do something with your location
let region = MKCoordinateRegionMakeWithDistance(location, 700000.0, 900000.0)
mapView.setRegion(region, animated: true)

But it'd be nicer if you implemented a class, something like LocationData, with the properties given in the .plist, and you'd crate a [LocationData] array, so you could reach the lat & long by properties of an object, not by key from a dictionary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM