CLLocationCoordinate2D到CLLocation

时间:2010-02-23 13:49:16

标签: iphone cocoa-touch cllocationmanager

我在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是CLLocationCoordinate2D

但我如何在下面的方法中使用coord,因为我需要这个以获得两个坐标之间的距离:

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

}

请帮助我,我被困了!

感谢大家提前帮助

5 个答案:

答案 0 :(得分:40)

CLLocation有一个名为-(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude的init方法。 然后使用- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location获取两个CLLocation对象之间的距离。

答案 1 :(得分:11)

简单

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

答案 2 :(得分:8)

对于斯威夫特人群,

我有一个名为“fetchCafesArroundLocation”的方法,在地图移动后刷新。这就是我如何处理从CLLocationCoordiate2d将Lat和Lon引入CLLocation然后将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)

}

答案 3 :(得分:2)

  

“我如何将我的实例用作CLLocation?”

你不能,因为它不是一个。您需要create one

不要忘记释放你分配的内容。请参阅the memory management rules

答案 4 :(得分:0)

如果coord是CCLocationCoordinate2D,那么您可以从中分配newLocation

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

通过获取latitude坐标属性的longitudeCLLocation属性进行委托,如下所示:

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