经纬度坐标转换为位置信息

27

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

}

4
“coord” 不是一个 CLLocationCoordinate2D,它是你自己创建的一个对象,一个 Koordinate 实例。 - nevan king
6个回答

43

CLLocation有一个初始化方法,它接受一个latitudelongitude

-(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude

然后使用以下方法来获取两个CLLocation对象之间的距离:

- (CLLocationDistance)getDistanceFrom:(const CLLocation *)location

这是什么类型的距离?大圆距离?航向线距离? - Maciej Swic
1
我认为这是Great Circle,但方法“getDistanceFrom”已被弃用。它已被“distanceFromLocation”取代。 - mpemburn

15

简单

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

12

对于Swift的开发者,

我有一个名为"fetchCafesArroundLocation"的方法,当地图移动时会进行刷新。这是我处理将纬度和经度从CLLocationCoordinate2d转换成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)

}

2
“如何将我的实例用作CLLocation?”
你不能这样做,因为它不是CLLocation类型的。你需要创建一个
不要忘记释放你所分配的内存。请查看内存管理规则

0
如果 coord 是一个 CCLocationCoordinate2D,那么你可以将 newLocation 赋值给它。
**- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation**

通过获取 CLLocation 的 coordinate 属性中的 latitudelongitude 属性来委托,如下所示:

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

0

如果您经常进行 CLLocationCoordinate2DCLLocation 的转换,并带有内置的 有效性检查,则可以使用 Swift 扩展。

奇怪的是,Core Location 中没有方便的初始化程序。

extension CLLocation {
    convenience init?(_ coordinate2D: CLLocationCoordinate2D) {
        guard CLLocationCoordinate2DIsValid(coordinate2D) else { return nil }
        self.init(latitude: coordinate2D.latitude, longitude: coordinate2D.longitude)
    }
}

使用方式如下:

let location2D = CLLocationCoordinate2D(latitude: 37.3346, longitude: -122.0090)
let location = CLLocation(location2D)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接