4

I have a collection of CGPoints obtained from converting geographic coordinates obtained form Core Location frame work delegate method. The collection is a set of start/end points.

I am obtaining the coordinates from CoreLocation delegate method

CLLocationCoordinate2D coordinate;
CLLocationDegrees latitude;
CLLocationDegrees longitude;

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *location = [locations lastObject];

latitude = location.coordinate.latitude;
longitude = location.coordinate.longitude;
}

I am converting the current latitude and longitude to CGPoint as below

CGPoint startPoint = [mapView convertCoordinate:retrievedCoordinate toPointToView:self.view];
CGPoint endPoint = [mapView convertCoordinate:retrievedEndCoordinate toPointToView:self.view];

I need to draw lines on my UIView based on the values in the collection. How can I adjust/scale correctly, the CGPoints with respect to the UIView. UIView frame size is (0, 0, 768, 1004).

This is the scaling I have done

- (CGPoint)convertLatLongCoord:(CGPoint)latLong {
CGSize screenSize = [UIScreen mainScreen].applicationFrame.size;
CGFloat SCALE = MIN(screenSize.width, screenSize.height) / (2.0 * EARTH_RADIUS);
CGFloat OFFSET = MIN(screenSize.width, screenSize.height) / 2.0;

CGFloat x = EARTH_RADIUS * cos(latLong.x) * cos(latLong.y) * SCALE + OFFSET;
CGFloat y = EARTH_RADIUS * cos(latLong.x) * sin(latLong.y) * SCALE + OFFSET;

return CGPointMake(x, y);
}

where #define EARTH_RADIUS 6371

The line drawing is not correct, since somewhere the conversion to CGPoint might be wrong. What am I doing wrong. Help needed.

Xavi Valero
  • 2,047
  • 7
  • 42
  • 80
  • 2
    one thing u can do is scale your view to (width,height),then u can specify your coordinate interms of 0 to 1(means in percentage) and make some function to convert ur longitude and latitude to ur coordinates. – Rajath Shetty K Jun 20 '13 at 06:08
  • I have already converted the geographic coordinates into screen coordinates(CGPoint) and drawn lines. The scaling part is where I am having the most difficulty. Not very good in Maths. – Xavi Valero Jun 20 '13 at 06:29
  • then scale your view using CGAffineTransformMakeScale to your view size (by passing views width and height), then u specify longitude and latitude in percentage, example for longitude 0 = 0.5 , 90 = 0.75 like that... – Rajath Shetty K Jun 20 '13 at 06:53
  • Is that worked well? can you write the answer? – Rafeek May 28 '14 at 06:22

1 Answers1

1

For convert CLLocationCoordinate2D to CGPoint see answer here - https://stackoverflow.com/a/37276779/2697425

Community
  • 1
  • 1