CLLocationCoordinate2D can't be instantiated
Frankenstein was right - it must be an Xcode bug. I put the same code in a "real" program and no problem.
CLLocationCoordinate2D into one array
You could create an array of CLLocationCoordinate2D
s:
var coordinateArray: [CLLocationCoordinate2D] = []
if latarray.count == lonarray.count {
for var i = 0; i < latarray.count; i++ {
let destination = CLLocationCoordinate2DMake(latarray[i], lonarray[i])
coordinateArray.append(destination)
}
}
EDIT:
In your code, neither latarray
nor lonarray
is an array. If you want to create an array of CLLocationCoordinate2D
s, you should add a variable to store your locations and your for loop should look like this:
var locations: [CLLocationCoordinate2D] = []
for coords in place{
let lat = Double(coords.latitude!)
let lon = Double(coords.longitude!)
let title = coords.title!
let destination = CLLocationCoordinate2DMake(lat, lon)
print(destination) // This prints each location separately
if !locations.contains(destination) {
locations.append(destination)
}
}
print(locations) // This prints all locations as an array
// Now you can use your locations anywhere in the scope where you defined the array.
func getLocationFromArray() {
// Loop through the locations array:
for location in locations {
print(location) // Prints each location separately again
}
}
NSMutableArray of ClLocationCoordinate2D
To stay in object land, you could create instances of CLLocation
and add those to the mutable array.
CLLocation *towerLocation = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
[currentDisplayedTowers addObject:towerLocation];
To get the CLLocationCoordinate
struct back from CLLocation
, call coordinate
on the object.
CLLocationCoordinate2D coord = [[currentDisplayedTowers lastObject] coordinate];
CLLocationCoordinate2D to CLLocation
CLLocation
has an init method that takes a latitude
and longitude
:
-(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude
Then use the following method to get the distance between two CLLocation objects:
- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location
How can I compare CLLocationCoordinate2D
Either the generic approach for comparing two instances of any given struct type:
memcmp(&cllc2d1, &second_cllc2d, sizeof(CLLocationCoordinate2D))
or
cllc2d1.latitude == cllc2d2.latitude && cllc2d1.longitude == cllc2d2.longitude
should work, if you really want to be sure they're exactly equal. However, given that latitude and longitude are defined as doubles, you might really want to do a "close enough" comparison:
fabs(cllc2d1.latitude - cllc2d2.latitude) <= epsilon && fabs(cllc2d1.longitude - cllc2d2.longitude) <= epsilon
where epsilon
is whatever level of error you want to accept.
Creating a new CLLocationCoordinate2D at custom Latitude/Longitude
What made the most sense to you (CLLocationCoordinate2DMake) is correct. You just forgot to include the CoreLocation framework in your project.
And, as others have pointed out, your lat/lon in your file are probably not integers.
Related Topics
Programmatically Screenshot | Swift 3, Macos
Retrieve Only 5 Users At a Time :Firebase [Like Instagram]
How to Use Indices.Contains() in a Collection Extension in Swift 3
How to Create a Fixed-Size Array of Objects
Upvote/Downvote System Within Swift via Firebase
Use Queue and Semaphore for Concurrency and Property Wrapper
How to Obtain the Selected Text from the Frontmost Application in MACos
Read-Only Properties of Protocols in Swift
What Is the Correct Date.Format for Mmm Dd, Yyyy Hh:Mm:Ss A? and How Convert to Dd-Mm-Yyyy Hh:Ii
Uploading Image to Firebase Storage and Database
How to Configure Contextmenu Buttons For Delete and Disabled in Swiftui
Swift: Setting an Optional Property of a Protocol
Reasons to Include Function in Protocol Definition VS. Only Defining It in the Extension
How to Make Class Methods/Properties in Swift
In Swift, How to Convert a String to an Enum
Swift: 'Hashable.Hashvalue' Is Deprecated as a Protocol Requirement;
Swift Imagepickercontroller Didfinishpickingmediawithinfo Not Fired