27

I have the following loop in my viewDidLoad:

for(int i=1; i<[eventsArray count]; i++) {
    NSArray *componentsArray = [[eventsArray objectAtIndex:i] componentsSeparatedByString:@","];
    if([componentsArray count] >=6) {
        Koordinate *coord = [[Koordinate alloc] init];
        coord.latitude = [[componentsArray objectAtIndex:0] floatValue];
        coord.longtitude = [[componentsArray objectAtIndex:1] floatValue];
        coord.magnitude = [[componentsArray objectAtIndex:2] floatValue];
        coord.depth = [[componentsArray objectAtIndex:3] floatValue];
        coord.title = [componentsArray objectAtIndex:4];
        coord.strasse = [componentsArray objectAtIndex:5];
        coord.herkunft = [componentsArray objectAtIndex:6];
        coord.telefon = [componentsArray objectAtIndex:7];
        coord.internet = [componentsArray objectAtIndex:8];
        [eventPoints addObject:coord];
    }
}

coord is of Type CLLocationCoordinate2D

How can I use coord in the following method? I need this to get distance between two coords:

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {

}
pkamb
  • 33,281
  • 23
  • 160
  • 191
Marco
  • 461
  • 1
  • 10
  • 22
  • 4
    `coord` is not a `CLLocationCoordinate2D`, `coord` is an object you created yourself, an instance of `Koordinate`. – nevan king Feb 23 '10 at 15:11

6 Answers6

43

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
pkamb
  • 33,281
  • 23
  • 160
  • 191
AlexVogel
  • 10,601
  • 10
  • 61
  • 71
15

Simple

CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
Can Ürek
  • 641
  • 12
  • 24
12

For the Swift crowd,

I have a method called "fetchCafesArroundLocation" which is refreshed after the map gets moved around. This is how I handled getting the Lat and Lon into CLLocation from CLLocationCoordiate2d and then passed the CLLocation variable to the handling method.

func mapView(mapView: MKMapView!, regionDidChangeAnimated animated: Bool){

    var centre = mapView.centerCoordinate as CLLocationCoordinate2D

    var getLat: CLLocationDegrees = centre.latitude
    var getLon: CLLocationDegrees = centre.longitude


    var getMovedMapCenter: CLLocation =  CLLocation(latitude: getLat, longitude: getLon)

    self.lastLocation = getMovedMapCenter
    self.fetchCafesAroundLocation(getMovedMapCenter)

}
Christopher Wade Cantley
  • 7,122
  • 5
  • 35
  • 48
2

“how can i use my instance as CLLocation?”

You can't, because it isn't one. You need to create one.

Don't forget to release what you alloc. See the memory management rules.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
0

Swift extension for if you're doing CLLocationCoordinate2D to CLLocation conversions a lot, with a built-in check for validity.

Strange that there is not a convenience initializer built in to Core Location.

extension CLLocation {
    convenience init?(_ coordinate2D: CLLocationCoordinate2D) {
        guard CLLocationCoordinate2DIsValid(coordinate2D) else { return nil }
        self.init(latitude: coordinate2D.latitude, longitude: coordinate2D.longitude)
    }
}

use like so:

let location2D = CLLocationCoordinate2D(latitude: 37.3346, longitude: -122.0090)
let location = CLLocation(location2D)
pkamb
  • 33,281
  • 23
  • 160
  • 191
0

If coord is a CCLocationCoordinate2D, then you can assign the newLocation from the

**- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation**

delegate by getting both of the latitude and longitude properties of coordinate property of the CLLocation as below:

coord.latitude = newLocation.coordinate.latitude;
coord.longitude = newLocation.coordinate.longitude;
Suhail kalathil
  • 2,673
  • 1
  • 13
  • 12
GeneCode
  • 7,545
  • 8
  • 50
  • 85