Search code examples
objective-cswiftcllocation

CLLocation object in swift with latitude and longitude


I would try to set the latitude and the longitude property of a CLLocation object but I get this error on running:

Cannot convert CLlocation object to EMTrackPoint Object

enter image description here

for element in objectsArray {
    let item = CLLocation(latitude: element.latitude, longitude: element.longitude) as! EMTrackPoint

    item.mElevation = element.ele
    item.mOrientation = element.orientation
    item.mDate = Date()
    if element.poiActive != ""{
         GPXActive.add(element.poiActive.components(separatedBy: ","))
    }
    GPXDanger.add(element.dangerActive.components(separatedBy: ","))
    item.mActivePOIs = GPXActive
    item.mActiveDangers = GPXDanger
    GPXPoints.add(item)
}

The EMTrackPoint class in Objective C:

@interface EMTrackPoint : CLLocation

@property (nonatomic, assign) double mElevation;
@property (nonatomic, retain) NSDate *mDate;
@property (nonatomic, retain) NSMutableArray *mActivePOIs;
@property (nonatomic, retain) NSMutableArray *mActiveDangers;
@property (nonatomic, assign) double mOrientation;
@property (nonatomic, assign) double mSpeed;

Please help.


Solution

  • Each EMTrackPoint is also a CLLocation so why don't you initialize a EMTrackPoint instance in the first place instead of doing the typecasting:

    for element in objectsArray {
        let item = EMTrackPoint(latitude: element.latitude, longitude: element.longitude)
        item.mElevation = element.ele
        item.mOrientation = element.orientation
        item.mDate = Date()
        ...