2010-02-23 9 views
18

Ich habe die folgende Schleife in meinem viewDidLoad:CLLocationCoordinate2D zu CLLocation

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 ist CLLocationCoordinate2D

aber wie kann ich coord verwenden in der folgenden Methode, weil ich diese müssen Abstand zwischen zwei Koordinaten erhalten :

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

} 

bitte helfen Sie mir ich bin gestrandet!

danke Ihnen allen für -(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude vorher

+3

'coord' ist kein' CLLocationCoordinate2D', 'coord' ist ein selbst erstelltes Objekt, eine Instanz von' Koordinate'. –

Antwort

37

CLLocation hat ein Init-Methode mit dem Namen zu helfen. Verwenden Sie dann - (CLLocationDistance)getDistanceFrom:(const CLLocation *)location, um den Abstand zwischen zwei CLLocation-Objekten zu ermitteln.

+0

Was ist das für eine Entfernung? Schöner Kreis? Rhombenlinie? –

+0

Ich glaube, es ist Großkreis, aber die Methode "getDistanceFrom" ist veraltet. Es wurde durch "distanceFromLocation" ersetzt. – mpemburn

2

„wie kann ich mein Beispiel als CLLocation verwenden?“

Sie können nicht, weil es nicht ist. Sie müssen create one.

Vergessen Sie nicht, was Sie zuweisen. Siehe the memory management rules.

0

Wenn coord ein CCLocationCoordinate2D ist, dann können Sie die newLocation von die, indem sie beide der latitude und longitude Eigenschaften von Koordinaten Eigenschaft des CLLocation wie unten

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

Delegat zuweisen:

coord.latitude = newLocation.coordinate.latitude; 
coord.longitude = newLocation.coordinate.longitude; 
9

Einfach

CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lon]; 
4

Für die Swift Menge,

Ich habe eine Methode namens "fetchCafesArroundLocation", die aktualisiert wird, nachdem die Karte verschoben wurde. So behandelte ich den Lat und Lon von CLLocationCoordiate2d in CLLocation und übergab dann die Variable CLLocation an die Handlingsmethode.

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) 

}