CLLocationCoordinate2D to CLLocation

假如想象 提交于 2019-12-18 04:29:17

问题


i have the following loop in my 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 CLLocationCoordinate2D

but how can i use coord in the following method, because i need this to get distance between two coords:

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

}

please help me i am stranded!

thank you all for helping beforehand


回答1:


CLLocation has an init method named -(id)initWithLatitude:(CLLocationDegrees)latitude longitude:(CLLocationDegrees)longitude. Then use - (CLLocationDistance)getDistanceFrom:(const CLLocation *)location to get the distance between two CLLocation objects.




回答2:


Simple

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



回答3:


For the Swift crowd,

I have a method called "fetchCafesArroundLocation" which is refreshed after the map gets moved around. This is how I handled getting the Lat and Lon into CLLocation from CLLocationCoordiate2d and then passed the CLLocation variable to the handling method.

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:


“how can i use my instance as 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.




回答5:


If coord is a CCLocationCoordinate2D, then you can assign the newLocation from the

**- (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:

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


来源:https://stackoverflow.com/questions/2318547/cllocationcoordinate2d-to-cllocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!