将cAshapelayer半径转换为mkcircle radius转换。
由于shapelayer基于实际视图的顶点,而mkcircle半径基于地图缩放系数,转换的最佳方法是什么?
答案 0 :(得分:2)
下面列出了将MKCircle的半径转换为视图和背面的方法之一: -
1>将MKCircle的中心点(lat,long)转换为ViewController视图的中心点。它将成为CAShapeLayer的中心。
[mapView convertCoordinate:yourCoordinate toPointToView:self.view];
2> MKCircle将半径作为地图上的距离,以iOS为单位。必须将此距离转换为UIViewController视图上的距离。可以遵循以下步骤来计算相同的步骤。
a)使用以下方法获取距离MKCircle半径r米处的(Lat,Long): -
-(CLLocationCoordinate2D)calculateCoordinateForDistanceInKm:(double)distanceKM bearing:(double)bearing initialCoordinate:(CLLocationCoordinate2D)coordinate
{
if(!CLLocationCoordinate2DIsValid(coordinate) || !isValidDouble(distanceKM) || !isValidDouble(bearing))
assert("The Inputs are not Valid");
double angularDistance = tan(distanceKM / 6371.0f);
double bearingRad = degreeToRad(bearing);
double currentLat = degreeToRad(coordinate.latitude);
double currentLong = degreeToRad(coordinate.longitude);
double newLatitude = asin((sin(currentLat) * cos(angularDistance))
+ (cos(currentLat) * sin(angularDistance) * cos(bearingRad)));
double newLongitude = currentLong + atan2(sin(bearingRad)
* sin(angularDistance) * cos(currentLat), cos(angularDistance) - (sin(currentLat) * sin(newLatitude)));
// newLongitude should be in the range -180 to +180
newLongitude = fmod((newLongitude + 3*M_PI), (2*M_PI)) - M_PI;
return CLLocationCoordinate2DMake(RadToDegree(newLatitude), RadToDegree(newLongitude));
}
bool isValidDouble(double number)
{
if(isnan(number) || isinf(number))
return NO;
return YES;
}
double degreeToRad(double degree)
{
double radians = (degree * M_PI)/ 180.0f;
return radians;
}
double RadToDegree(double rad)
{
double degree = (rad * 180.0f)/ M_PI;
return degree;
}
b)将新坐标翻译为步骤1中提到的UIViewController
视图。
c)找出这两点之间的距离。
CGPoint pointOne = [mapView convertCoordinate:yourCoordinate toPointToView:self.view];
CLLocationCoordinate2D newCoordinate = [self calculateCoordinateForDistanceInKm:1.0f bearing:youtCurrentLocationObject.course initialCoordinate:youtCurrentLocationObject.coordinate];
CGPoint pointTwo = [mapView convertCoordinate:newCoordinate toPointToView:self.view];
NSNumber *radius = [NSNumber numberWithDouble:hypotf(pointOne.x - pointTwo.x, pointOne.y - pointTwo.y)];
3)根据半径和第一个点(圆心)绘制CAShapeLayer。
4)将CAShapeLayer点bak转换为CLLocationCoordinate2D
以绘制MKCircle(在满足您的某些要求之后)。 CAShapeLayer的中心点和半径已知,因此中心将是(x,y)
,并且距中心等距的轨迹上的点将为(x + radius, y)
。
使用以下命令将视图的点转换回MKMapView
的坐标。
[mapView convertPoint:someCGPoint toCoordinateFromView:mapView]
注意: - 用于计算距离另一个坐标的距离为D的坐标的公式。
Formula: φ2 = asin( sin φ1 ⋅ cos δ + cos φ1 ⋅ sin δ ⋅ cos θ )
λ2 = λ1 + atan2( sin θ ⋅ sin δ ⋅ cos φ1, cos δ − sin φ1 ⋅ sin φ2 )
where φ is latitude, λ is longitude, θ is the bearing (clockwise from north), δ is the angular distance d/R; d being the distance travelled, R the earth’s radius.
希望它能回答你的问题。