CLLocationCoordinate2D to CLLocation - PullRequest
       6

CLLocationCoordinate2D to CLLocation

24 голосов
/ 23 февраля 2010

у меня есть следующий цикл в моем 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];


  }

координата: CLLocationCoordinate2D

но как я могу использовать координаты в следующем методе, потому что мне нужно это, чтобы получить расстояние между двумя координатами:

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

}

Пожалуйста, помогите мне, я застрял!

спасибо всем за помощь заранее

Ответы [ 5 ]

40 голосов
/ 23 февраля 2010

CLLocation имеет метод init с именем -(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude. Затем используйте - (CLLocationDistance)getDistanceFrom:(const CLLocation *)location, чтобы получить расстояние между двумя объектами CLLocation.

11 голосов
/ 19 июля 2014

Простой

CLLocation *location = [[CLLocation alloc] initWithLatitude:lat longitude:lon];
8 голосов
/ 11 ноября 2014

Для толпы Свифта,

У меня есть метод с именем "fetchCafesArroundLocation", который обновляется после перемещения карты. Вот как я обработал получение Lat и Lon в CLLocation из CLLocationCoordiate2d, а затем передал переменную CLLocation методу обработки.

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)

}
2 голосов
/ 24 февраля 2010

«как я могу использовать свой экземпляр в качестве CLLocation?»

Вы не можете, потому что это не один. Вам нужно создать один .

Не забудьте выпустить то, что вы выделяете. См. правила управления памятью .

0 голосов
/ 16 февраля 2012

Если координата CCLocationCoordinate2D, вы можете назначить newLocation из

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

делегировать, получая оба свойства latitude и longitude координаты свойства CLLocation, как показано ниже:

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