jack
jack

Reputation: 1921

MKMapView How-to convert from longitude latitude back to cm?

1) I am using MKMapView to display a custom Image (for example width=350 cm,height =230 cm) in a MKOverlayView.

2) The center of the Image is now at longitude=0 and latitude = 0 and covers the whole world

4) I place a MKPointAnnotation at longitude = 60.749995 and latitude =56.091651

Now I want convert this Point(Longitude,Latitude) back to x,y in cm.
So that I can create a JPG on the server with the annotation on top of the Image.

So how do I calculate the x,y values?

Thanx Craig

so something like:

CLLocationCoordinate2D coordinateOrigin = CLLocationCoordinate2DMake(90, -180);
CLLocationCoordinate2D coordinateMax = CLLocationCoordinate2DMake(-90, 180);


MKMapPoint maxMap=MKMapPointForCoordinate(coordinateMax);
MKMapPoint minMap=MKMapPointForCoordinate(coordinateOrigin);

double width = maxMap.x-minMap.x;
double height = maxMap.y-minMap.y;

MKMapPoint p =  MKMapPointForCoordinate(wanted_coord);//wanted_coord is the one needed
double pixel_x=p.x/width;
double pixel_y=p.y/height;

Upvotes: 0

Views: 845

Answers (1)

Craig
Craig

Reputation: 8304

1) You're not really dealing with cm, you're dealing with pixels. An image has a certain number of pixels in each direction, the physical measurement of cm depends on how big your screen/printer's pixels are.

2) to convert from lat long to pixels use MKMapPoints via the MKMapPointForCoordinate function. That will give you a x/y coordinate and you'll need to scale those values to fit your custom image, therefore you need to work out what MKMapPoints it covers. For example if your image covered the entire world you could find the minimum values for MKMapPoint by using MKCoordinateForMapPoint with (-180,-90) and the maximum values with (180,90). Now you'll have the max/min for MKMapPoint's x and y, you know the max/min for your image, so it's trivial to scale from one to the other.

Upvotes: 2

Related Questions