【问题标题】:CLLocationCoordinate2D to CLLocationCLLocationCoordinate2D 到 CLLocation
【发布时间】:2011-01-20 02:03:18
【问题描述】:

我的 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{

}

请帮帮我,我被困了!

谢谢大家的帮助

【问题讨论】:

  • coord 不是CLLocationCoordinate2Dcoord 是您自己创建的对象,Koordinate 的实例。

标签: iphone cocoa-touch cllocationmanager


【解决方案1】:

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

【讨论】:

  • 这是什么距离?大圈子?大拇指线?
  • 我相信它是 Great Circle,但“getDistanceFrom”方法已被弃用。它被替换为“distanceFromLocation”。
【解决方案2】:

简单

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

【讨论】:

    【解决方案3】:

    对于 Swift 人群,

    我有一个名为“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)
    
    }
    

    【讨论】:

      【解决方案4】:

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

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

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

      【讨论】:

        【解决方案5】:

        如果 coord 是 CCLocationCoordinate2D,那么您可以从

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

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

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

        【讨论】:

          猜你喜欢
          • 2015-01-08
          • 2023-03-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-12-22
          • 2013-09-25
          • 2013-07-19
          • 1970-01-01
          相关资源
          最近更新 更多