2010-02-23 22 views
18

tôi có vòng lặp sau đây trong viewDidLoad của tôi:CLLocationCoordinate2D để CLLocation

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 là CLLocationCoordinate2D

nhưng làm thế nào tôi có thể sử dụng coord trong phương pháp sau đây, vì tôi cần điều này để có được khoảng cách giữa hai coords :

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

} 

hãy giúp tôi tôi bị mắc kẹt!

cảm ơn tất cả các bạn đã giúp đỡ trước

+3

'coord' không phải là' CLLocationCoordinate2D', 'coord' là đối tượng bạn tự tạo, ví dụ' Koordinate'. –

Trả lời

37

CLLocation có một phương pháp init tên -(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude. Sau đó, sử dụng - (CLLocationDistance)getDistanceFrom:(const CLLocation *)location để nhận khoảng cách giữa hai đối tượng CLLocation.

+0

Loại khoảng cách này là gì? Vòng tròn lớn? Rhumb line? –

+0

Tôi tin rằng đó là Great Circle, nhưng phương thức "getDistanceFrom" đã không được chấp nhận. Nó đã được thay thế bằng "distanceFromLocation". – mpemburn

2

“làm thế nào tôi có thể sử dụng ví dụ của tôi như CLLocation?”

Bạn không thể, bởi vì nó không phải là một. Bạn cần phải create one.

Đừng quên tiết lộ những gì bạn phân bổ. Xem the memory management rules.

0

Nếu coord là một CCLocationCoordinate2D, sau đó bạn có thể gán newLocation từ các đại biểu

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

bằng cách cả hai latitudelongitude thuộc tính của điều phối tài sản của CLLocation như sau:

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

Đơn giản

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

Đối với đám đông Swift,

Tôi có phương thức được gọi là "fetchCafesArroundLocation" được làm mới sau khi bản đồ được di chuyển xung quanh. Đây là cách tôi xử lý nhận được Lat và Lon thành CLLocation từ CLLocationCoordiate2d và sau đó chuyển biến CLLocation đến phương thức xử lý.

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) 

} 
Các vấn đề liên quan