wiki:PHPMapscriptSnippet1

Code snippet to map image XY to map XY

Here is a code snippet from Antti Roppola, there is also another example in the GMap demo application that is a little fancier (GMap demo http://www2.dmsolutions.ca/gmap/gmap75.phtml download demo: http://dl.maptools.org/dl/).

/**
 * converts pixel units into map units
 *
 * @param int $click_x coordinate X in pixels 
 * @param int $click_y coordinate Y in pixels
 * @param array $current_extent holds the current extent of the map (XMin, YMin, XMax, YMax)
 * @global map object
 * @return array  [0]=> X in map units; [1]=> Y in map units
 */
function click2map ($click_x, $click_y, $current_extent) {
	global $map;	

	$x_pct = ($click_x / $map->width);
	$y_pct = 1 - ($click_y / $map->height);
	
	$x_map = $current_extent[0] + ( ($current_extent[2] - $current_extent[0]) * $x_pct);
	$y_map = $current_extent[1] + ( ($current_extent[3] - $current_extent[1]) * $y_pct);

	return array($x_map, $y_map);
}

Go back to: PHPMapScript

Last modified 11 years ago Last modified on Jan 29, 2009 1:13:47 PM