简体   繁体   English

CLLocationCoordinate2D 到 CLLocation

[英]CLLocationCoordinate2D to CLLocation

I have the following loop in my viewDidLoad:我的 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 is of Type CLLocationCoordinate2D coord的类型为CLLocationCoordinate2D

How can I use coord in the following method?如何在以下方法中使用coord I need this to get distance between two coords:我需要这个来获得两个坐标之间的距离:

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

}

CLLocation has an init method that takes a latitude and longitude : CLLocation有一个 init 方法,它采用latitudelongitude

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

Then use the following method to get the distance between two CLLocation objects:然后使用以下方法获取两个 CLLocation 对象之间的距离:

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

简单的

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

For the Swift crowd,对于斯威夫特人群,

I have a method called "fetchCafesArroundLocation" which is refreshed after the map gets moved around.我有一个名为“fetchCafesArroundLocation”的方法,它会在地图移动后刷新。 This is how I handled getting the Lat and Lon into CLLocation from CLLocationCoordiate2d and then passed the CLLocation variable to the handling method.这就是我如何处理将 Lat 和 Lon 从 CLLocationCoordiate2d 获取到 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)

}

“how can i use my instance as CLLocation?” “我如何将我的实例用作 CLLocation?”

You can't, because it isn't one.你不能,因为它不是一个。 You need to create one .您需要创建一个.

Don't forget to release what you alloc.不要忘记释放你分配的内容。 See the memory management rules .请参阅内存管理规则

Swift extension for if you're doing CLLocationCoordinate2D to CLLocation conversions a lot, with a built-in check for validity . Swift 扩展,如果你经常做CLLocationCoordinate2DCLLocation的转换,带有内置的有效性检查

Strange that there is not a convenience initializer built in to Core Location.奇怪的是,核心位置没有内置方便的初始化程序。

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

use like so:像这样使用:

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

If coord is a CCLocationCoordinate2D , then you can assign the newLocation from the如果 coord 是CCLocationCoordinate2D ,那么您可以从

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

delegate by getting both of the latitude and longitude properties of coordinate property of the CLLocation as below:通过获取CLLocation的坐标属性的latitudelongitude属性进行委托,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM