将地理坐标转换为CGPoint并在UIView中绘制线条

时间:2013-06-20 05:36:44

标签: iphone ipad ios6 uiview coordinate-systems

我有一个CGPoints集合,它是通过转换从Core Location框架工作委托方法获得的地理坐标获得的。该集合是一组起点/终点。

我从CoreLocation委托方法

获取坐标
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;
}

我正在将当前纬度和经度转换为CGPoint,如下所示

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

我需要根据集合中的值在我的UIView上绘制线条。如何正确调整/缩放,关于UIView的CGPoints。 UIView帧大小为(0,0,768,1004)。

这是我做过的缩放

- (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);
}

其中#define EARTH_RADIUS 6371

线条图不正确,因为某处转换为CGPoint可能是错误的。我究竟做错了什么。需要帮助。

1 个答案:

答案 0 :(得分:1)

将CLLocationCoordinate2D转换为CGPoint,请参阅此处的答案 - https://stackoverflow.com/a/37276779/2697425