0

I had been struggling to store CLLocationCoordinate2D data from markers on a google maps to CoreData. This cannot be done directly but I found a work around where I take the coordinates, split into CLLocationDegrees, convert it into a string text and store it. I do this by the following:

 let marker = GMSMarker() marker.position = CLLocationCoordinate2DMake(place.coordinate.latitude, place.coordinate.longitude) let newPlaceLatitude = place.coordinate.latitude print(newPlaceLatitude) var latitudeText:String = "\(newPlaceLatitude)" self.latitudeText = "\(newPlaceLatitude)" let newPlaceLongitude = place.coordinate.longitude print(newPlaceLongitude) var longitudeText:String = "\(newPlaceLongitude)" self.longitudeText = "\(newPlaceLongitude)" 

Storing into CoreData:

 let appDelegate = UIApplication.shared.delegate as! AppDelegate let context = appDelegate.persistentContainer.viewContext let newPlace = NSEntityDescription.insertNewObject(forEntityName: "StoredPlace", into: context) newPlace.setValue(latitudeText, forKeyPath: "latitude") newPlace.setValue(longitudeText, forKeyPath: "longitude") 

However now I am struggling to reconstruct the strings back into CLLocationCoordinates. How would I turn a string to a CLLocationDegree/CLLocationCoordinate2D ? This is supposedly pretty simple but I have found that the following method doesn't work:

 let latitude: CLLocationDegrees = Double(latitudeText)! let longitude: CLLocationDegrees = Double(longitudeText)! let markers = GMSMarker() print(latitude) print(longitude) markers.position = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 

any other suggestions about how to change string to coordinate?

3
  • 1
    Probably not related but why do you store a Double as String to convert it back to Double? Core Data supports Double as well. Commented Dec 31, 2016 at 13:02
  • What became of this? Commented Jan 1, 2017 at 14:51
  • The reason why I wasn't storing it as double straight away is because I kept getting errors and couldn't get round it so thought this method of double to string- string to double would work just as well.. Adrian I didn't fully understand your answer so ended up using the method of converting the string back to double in the viewDidLoad when fetching the data from coredata by using: if let lat = result.value(forKey: "latitude") as? NSString { let latitude = (lat as NSString).doubleValue } and using that to reconstruct markers Commented Jan 1, 2017 at 18:45

2 Answers 2

1

CLLocation's latitude and longitude are doubles, so with that in mind, you might consider having latitude and longitude properties that are doubles on your StoredPlace object. I called the properties coordinateX and coordinateY so it's easier to remember that they're custom coordinates, not "factory" properties.

You could create an extension in a file called StoredPlace+Extension.swift that looks like this:

import CoreData import CoreLocation extension StoredPlace { func location() -> CLLocation { let location = CLLocation(latitude: self.coordinateX, longitude: self.coordinateY) return location } } 

With this extension, you can then get the coordinates out of your results as follows:

for result in results { print("coordinate = \(result.location().coordinate)") print("latitude = \(result.location().coordinate.latitude)") print("longitude = \(result.location().coordinate.longitude)") } 
Sign up to request clarification or add additional context in comments.

Comments

0

You need to typecast your lat and long in decimal values, more preferable is double instead of float because of precision value which can drop pins at perfect locations.

Type casting in double using as keyword:

(yourCordinateString as NSString).doubleValue 

casting in float values:

(yourCordinateString as NSString).floatValue 

4 Comments

This works without error but something strange is happening when I do this method. The retrieved values come out as 0.0 even though when i store it and print the string it is the correct value. However when i do print after the above method it goes to 0.0, any idea why that is?
Sure, I have put the code onto the end of my question above. Please have a look. I am implementing this code in the viewDidLoad of the viewcontroller which I have my map container. the print(latitudeText) etc works fine but print(latitude) come out as 0.0 in the terminal. Any thoughts about what going on?
eh I figured out why its 0.0, because latitudeText is the wrong thing I am calling! i need result.value(forKey: "latitude")...
that's what i saw in your updated ques, lat long text is not correct from source .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.