Saturday, April 6, 2013

Getting the coordinates of a click in OpenLayers Map

Demo

[sourcecode language="javascript"]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Getting the coordinates of a click in OpenLayers Map</title>
<script src="http://openlayers.org/api/OpenLayers.js"></script>

<script type="text/javascript">
function init(){
map = new OpenLayers.Map('map');
base_layer = new OpenLayers.Layer.WMS( "OpenLayers WMS",
"http://labs.metacarta.com/wms/vmap0", {layers: 'basic'} );
map.addLayer(base_layer);
map.setCenter(new OpenLayers.LonLat(79.8333,6.9167),7);

map.events.register('click', map, handleMapClick);
}

function handleMapClick(evt)
{
var lonlat = map.getLonLatFromViewPortPx(evt.xy);
alert("latitude : " + lonlat.lat + ", longitude : " + latitude);

}
</script>

</head>
<body onLoad="init()">
</body>
</html>

[/sourcecode]

1 comment:

  1. […] You can try using OpenLayers click, and not jquery click. Here are examples: OpenLayers-2.13.1/examples/click.html, getting-the-coordinates-of-a-click-in-openlayers-map/ […]

    ReplyDelete

How to enable CORS in Laravel 5

https://www.youtube.com/watch?v=PozYTvmgcVE 1. Add middleware php artisan make:middleware Cors return $next($request) ->header('Acces...