MKMapkit XY coordinates to lat lon
I have been working a project that involves adding large numbers of overlays to the mkmapkit.
I currently have a working formula for converting lat/long into xy but have found i need to now do the inverse of this.
The code i am currently using is this:
-(CGPoint)calculateXYLatitude:(float)latitude longitude:(float)longitude zoom:(int)zoom
{
CGPoint p;
float zm = (float)zoom;
float offset = (pow(2.0, zm-1.0));
float radius = offset/M_PI;
p.x = (offset + radius * longitude * M_PI / 180.0);
p.y = (offset - radius * logf((1.0 + sinf(latitude * M_PI / 180.0)) / (1.0 - sinf(latitude * M_PI / 180.0))) / 2.0);
return p;
}
I have successfully managed to invert the first part of this function is is just the last line that has me stumped.
So far i have:
-(float)calculateLongitudeFromX:(float)x zoom:(int)zoom
{
float zm = (float)zoom;
float offset = (pow(2.0, zm-1.0));
float radius = offset/M_PI;
float 开发者_StackOverflowlongitude = (x - offset) * 180 / radius / M_PI;
return longitude;
}
Both of these functions above are tested and work perfectly in my project.
I need to create the -(float)calculateLatitudeFromY:(float)Y zoom:(int)zoom method.
Any help would be greatly appreciated.
You'll have to do the conversion to obj-c yourself, but here ya go:
http://groups.google.com/group/google-maps-api/browse_thread/thread/8c0a5291c22c0946?pli=1
Also, check out the source for this page: http://code.google.com/apis/maps/documentation/javascript/examples/map-coordinates.html
I think the two key pieces of information you're missing are the inverse of log and sin - which are e^x and atan, respectively.
The second Javascript method below is the answer to the question you're asking:
MercatorProjection.prototype.fromLatLngToPoint = function(latLng, opt_point) {
var me = this;
var point = opt_point || new google.maps.Point(0, 0);
var origin = me.pixelOrigin_;
point.x = origin.x + latLng.lng() * me.pixelsPerLonDegree_;
// NOTE(appleton): Truncating to 0.9999 effectively limits latitude to
// 89.189. This is about a third of a tile past the edge of the world tile.
var siny = bound(Math.sin(degreesToRadians(latLng.lat())), -0.9999, 0.9999);
point.y = origin.y + 0.5 * Math.log((1 + siny) / (1 - siny)) * -me.pixelsPerLonRadian_;
return point;
};
MercatorProjection.prototype.fromPointToLatLng = function(point) {
var me = this;
var origin = me.pixelOrigin_;
var lng = (point.x - origin.x) / me.pixelsPerLonDegree_;
var latRadians = (point.y - origin.y) / -me.pixelsPerLonRadian_;
var lat = radiansToDegrees(2 * Math.atan(Math.exp(latRadians)) - Math.PI / 2);
return new google.maps.LatLng(lat, lng);
};
精彩评论