简体   繁体   中英

What specific data should I store in DB when saving Locations

I would like to save/store user's current location but don't know which specific data I should store in DB and what should my database structure be? I would like to retrieve the location information and display it back on the map view. What's the best approach to achieve this? Regarding the database structure, should I have separate latitude and longitude columns on my database table?

You can save it as NSData using NSKeyedarchiver and NSKeyedUnarchiver as follow:

let locationData = NSKeyedArchiver.archivedDataWithRootObject(yourLocation)
NSUserDefaults.standardUserDefaults().setObject(locationData, forKey: "myLocation")


if let loadedData = NSUserDefaults.standardUserDefaults().dataForKey("myLocation") {
    if let loadedLocation = NSKeyedUnarchiver.unarchiveObjectWithData(loadedData) as? CLLocation {
        println(loadedLocation.description)
    }
}

You should store latitude and longitude as separate columns.

If you archive the location into an object that contains both values you can't access the individual values on the database level. You have to get the object and extract latitude and longitude from it.

While saving latitude and longitude in individual colums makes your code a couple of lines longer you have the advantage that you can use the coordinates in a database query.

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