开发者

Map GPS Coordinates to an Image and draw some GPS Points on it

I have some problems figuring out where my error is. I got the following:

Have an image and corresponding GPS coordinates of its top-left and bottom-right vertices. E.g:

topLeft.longitude = 8.235128;
topLeft.latitude = 49.632383;
bottomRight.longitude = 8.240547;
bottomRight.latitude = 49.629808;

Now a have an Point that lies in that map:

p.longitude = 8.238567;
p.latitude = 49.630664;

I draw my image in landscape fullscreen (1024*748).

Now开发者_运维问答 I want to calculate the exact Pixel position (x,y) of my point.

For doing that I am trying to use the great circle distance approach from here: Link.

CGFloat DegreesToRadians(CGFloat degrees)
{
return degrees * M_PI / 180;
};

- (float) calculateDistanceP1:(CLLocationCoordinate2D)p1 andP2:(CLLocationCoordinate2D)p2 {
double circumference = 40000.0; // Erdumfang in km am Äquator
double distance = 0.0;

double latitude1Rad = DegreesToRadians(p1.latitude);
double longitude1Rad = DegreesToRadians(p1.longitude);
double latititude2Rad = DegreesToRadians(p2.latitude);
double longitude2Rad = DegreesToRadians(p2.longitude);

double logitudeDiff = fabs(longitude1Rad - longitude2Rad);

if (logitudeDiff > M_PI)
{
    logitudeDiff = 2.0 * M_PI - logitudeDiff;
}

double angleCalculation =
acos(sin(latititude2Rad) * sin(latitude1Rad) + cos(latititude2Rad) * cos(latitude1Rad) * cos(logitudeDiff));

distance = circumference * angleCalculation / (2.0 * M_PI);
NSLog(@"%f",distance);

return distance;
}

Here is my code for getting the Pixel position:

- (CGPoint) calculatePoint:(CLLocationCoordinate2D)point {
float x_coord;
float y_coord;

CLLocationCoordinate2D x1;
CLLocationCoordinate2D x2;

x1.longitude = p.longitude;
x1.latitude = topLeft.latitude;
x2.longitude = p.longitude;
x2.latitude = bottomRight.latitude;

CLLocationCoordinate2D y1;
CLLocationCoordinate2D y2;

y1.longitude = topLeft.longitude;
y1.latitude = p.latitude;
y2.longitude = bottomRight.longitude;
y2.latitude = p.latitude;

float distanceX = [self calculateDistanceP1:x1 andP2:x2];
float distanceY = [self calculateDistanceP1:y1 andP2:y2];

float distancePX = [self calculateDistanceP1:x1 andP2:p];
float distancePY = [self calculateDistanceP1:y1 andP2:p];

x_coord = fabs(distancePX * (1024 / distanceX))-1;
y_coord = fabs(distancePY * (748 / distanceY))-1;

return CGPointMake(x_coord,y_coord);

}

x1 and x2 are the points on the longitude of p and with latitude of topLeft and bottomRight. y1 and y2 are the points on the latitude of p and with longitude of topLeft and bottomRight.

So I got the distance between left and right on longitude of p and distance between top and bottom on latitude of p. (Needed for calculate the pixel position)

Now I calculate the distance between x1 and p (my distance between x_0 and x_p) after that I calculate the distance between y1 and p (distance between y_0 and y_p)

Last but not least the Pixel position is calculated and returned.

The Result is, that my point is on the red and NOT on the blue position:

Map GPS Coordinates to an Image and draw some GPS Points on it

Maybe you find any mistakes or have any suggestions for improving the accuracy.


Maybe I didn't understand your question, but shouldn't you be using the Converting Map Coordinates methods of MKMapView?


See this image

I used your co-ordinates, and simply did the following:

x_coord = 1024 * (p.longitude - topLeft.longitude)/(bottomRight.longitude - topLeft.longitude);
y_coord =  748 - (748 * (p.latitude - bottomRight.latitude)/(topLeft.latitude - bottomRight.latitude));

The red dot markes this point. For such small distances you don't really need to use great circles, and your rounding errors will be making things much more inaccurate

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜