Cllocationcoordinate2D Can't Be Instantiated

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 CLLocationCoordinate2Ds:

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 CLLocationCoordinate2Ds, 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



Leave a reply



Submit