2010-02-23 16 views
18

Ho il seguente ciclo nel mio viewDidLoad:CLLocationCoordinate2D a 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 è CLLocationCoordinate2D

ma come posso usare Coord nel metodo seguito, perché ho bisogno di questo per ottenere la distanza tra due coordinate :

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

} 

per favore aiutami sono bloccato!

Grazie a tutti per l'aiuto in anticipo

+3

'coord' non è un' CLLocationCoordinate2D', 'coord' è un oggetto creato da te, un'istanza di' Koordinate'. –

risposta

37

CLLocation ha un metodo init chiamato -(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude. Quindi utilizzare - (CLLocationDistance)getDistanceFrom:(const CLLocation *)location per ottenere la distanza tra due oggetti CLLocation.

+0

Che tipo di distanza è questa? Grande cerchio? Linea Rhumb? –

+0

Credo che sia Great Circle, ma il metodo "getDistanceFrom" è stato deprecato. È stato sostituito con "distanceFromLocation". – mpemburn

2

“Come posso usare il mio esempio come CLLocation?”

Non è possibile, perché non è uno. È necessario create one.

Non dimenticare di rilasciare ciò che si assegna. Vedi the memory management rules.

0

Se coord è un CCLocationCoordinate2D, allora è possibile assegnare il newLocation dal delegato

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

ottenendo entrambi i latitude e longitude proprietà di coordinare le proprietà del CLLocation come di seguito:

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

Semplice

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

Per la folla Swift,

Ho un metodo chiamato "fetchCafesArroundLocation", che viene aggiornata dopo la mappa viene spostato intorno. In questo modo ho gestito il trasferimento di Lat e Lon in CLLocation da CLLocationCoordiate2d e quindi ho passato la variabile CLLocation al metodo di gestione.

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) 

} 
Problemi correlati