This is the LC translation of the linked javascript from stackoverflow: (I checked and simplfied a little bit the math, the Mercator projection is OK, very clever there!):
It works here, happy mapping: local MERCATOR_RANGE=256, pixelOrigin, pixelsPerLonDegree, pixelsPerLonRadian on mouseUp put (55.9537534,-3.1988236) into centerPoint -- LC Home coords put 10 into zom -- zoom put 600 into wdth -- image width put 450 into hght -- image height put getCorners (centerpoint,zom,wdth,hght) -- result is: "SW: 28.207002,-3.610811,NE: 28.262167,-2.786836" end mouseUp function getCorners center,zom,mapWidth,mapHeight put (MERCATOR_RANGE/2, MERCATOR_RANGE/2) into pixelOrigin put MERCATOR_RANGE/360 into pixelsPerLonDegree put pi*MERCATOR_RANGE/2 into pixelsPerLonRadian put 2^zom into scle put fromLatLngToPoint(center) into centerPx put ( (item 1 of centerPx)-(mapWidth/2)/scle, \ (item 2 of centerPx)+(mapHeight/2)/scle ) into SWPoint put fromPointToLatLng(SWPoint) into SWLatLon put ( (item 1 of centerPx)+(mapWidth/2)/scle, \ (item 2 of centerPx)-(mapHeight/2)/scle ) into NEPoint put fromPointToLatLng(NEPoint) into NELatLon return ("SW: " & SWLatLon, "NE: "& NELatLon) end getCorners function bound val, opt_min, opt_max if (opt_min is not empty) then put max(val, opt_min) into val if (opt_max is not empty) then put min(val, opt_max) into val return val end bound function degreesToRadians deg return deg * pi / 180; end degreesToRadians function radiansToDegrees rad return 180 * rad / pi end radiansToDegrees function fromLatLngToPoint latLng, opt_point put opt_point into point put pixelOrigin into origin put (item 1 of origin)+(item 2 of latLng)*pixelsPerLonDegree into item 1 of point put bound(sin(degreesToRadians(item 1 of latLng)), -0.9999, 0.9999) into siny put (item 2 of origin)+0.5*log10((1-siny)/(1+siny))*pixelsPerLonRadian into item 2 of point return point end fromLatLngToPoint function fromPointToLatLng point put pixelOrigin into origin put (item 1 of point -item 1 of origin)/pixelsPerLonDegree into lng put (item 2 of origin-item 2 of point)/pixelsPerLonRadian into latRadians put radiansToDegrees(2*atan(exp(latRadians))-pi/2) into lat return (lat,lng) end fromPointToLatLng // pixelCoordinate = worldCoordinate * 2^zoomLevel _______________________________________________ use-livecode mailing list use-livecode@lists.runrev.com Please visit this url to subscribe, unsubscribe and manage your subscription preferences: http://lists.runrev.com/mailman/listinfo/use-livecode