[OpenLayers-Users] Circle around clicked point

Alberto Najera anajera at bicimapas.com.mx
Sat May 12 06:26:10 PDT 2012


Hello

The circle drawing is working almost well but it gets triggered also when
double clicking to zoom in, which should not happen. I am thinking of 2
solutions, either ssign the drawing function to a right mouse click (if this
is possible?). If so, how should this statement

map.events.register('click', map, Circulo


or add a toggle button that would only allow the circle to be drawn when it
is activated.



Alberto Najera

-----Mensaje original-----
De: Alberto Najera [mailto:anajera at bicimapas.com.mx] 
Enviado el: viernes, 11 de mayo de 2012 01:41 p.m.
Para: 'openlayers-users at lists.osgeo.org'
Asunto: RE: [OpenLayers-Users] Circle around clicked point

Arnd,

Thanks, that helped a lot. I also found that the object holding the vector
layer was not instantiated as global, contributing to the problem.
Fortunately Firebug got it.

Alberto Najera
 

-----Mensaje original-----
De: Arnd Wippermann [mailto:arnd.wippermann at web.de] Enviado el: jueves, 10
de mayo de 2012 12:13 p.m.
Para: 'Alberto Najera'
CC: users at openlayers.org
Asunto: AW: [OpenLayers-Users] Circle around clicked point

First argument of createRegularPolygon is the origin like var origin =
{x:lonlat.lon, y:lonlat.lat}; var circleout = new
OpenLayers.Geometry.Polygon.createRegularPolygon(origin,
0.0096, 50);  

Arnd

-----Ursprüngliche Nachricht-----
Von: openlayers-users-bounces at lists.osgeo.org
[mailto:openlayers-users-bounces at lists.osgeo.org] Im Auftrag von Alberto
Najera
Gesendet: Donnerstag, 10. Mai 2012 18:09
An: openlayers-users at lists.osgeo.org
Betreff: [OpenLayers-Users] Circle around clicked point


Hello

I need to draw a circle around a click selected point on the map. Looking at
samples in this forum, some web search as well as the OpenLayers page, I
have put the  following code together, but the circle is not drawn. The map
is in EPSG 4326 so units are decimal degrees.

I added a vector layer where the circle shall be drawn:

   var vector_layer = new OpenLayers.Layer.Vector("Zona Influencia")
    //Add layers to the map
    map.addLayers([layer1, layer2, vector_layer]);

//Register the click event to get the clicked point coordinates

    map.events.register('click', map, Circulo);    
                
   var map_center = new OpenLayers.LonLat(-99.24801,19.49775); 
	 if(!map.getCenter()){
			map.setCenter(map_center);
		}
  	}

// Function to draw the circle around the clicked point

function Circulo(evt)
    {
       var lonlat = map.getLonLatFromViewPortPx(evt.xy);
       // use lonlat
	var circleout = new
OpenLayers.Geometry.Polygon.createRegularPolygon(lonlat, 0.0096, 50); 
	vector_layer.addFeatures(new OpenLayers.Feature.Vector(circleout)); 
    } 


What am I missing?

Full page code is as follows:

<!DOCTYPE html>
<html lang='en'>
<head> 
    <meta charset='utf-8' /> 
	<title>Open Layers Test</title>	
    <script type='text/javascript'
src="http://www.openlayers.org/api/OpenLayers.js"></script>
    <script type='text/javascript'>

    var map;
    
	function init() {
        // Setup our map object
        map = new OpenLayers.Map('map_element', {
        	controls: [
        	new OpenLayers.Control.Navigation(),
        	new OpenLayers.Control.PanZoomBar(),
        	new OpenLayers.Control.LayerSwitcher(),
        	new OpenLayers.Control.ScaleLine()
        	],
        	maxExtent: new OpenLayers.Bounds (-99.269569,19.479910,
-99.224060, 19.524080),
        	maxScale: 15000,
        	minScale: 2500
        	});
        
       // Setup our layer objects
       
   var layer1 = new OpenLayers.Layer.WMS(
		"Parques",
		"http://myhost/cgi-bin/mapserv?", 
		{map: '/home/bicimapas/mapdata/BiciMapasMS.map', 
	layers:'parque'});	 
      
   var layer2 = new OpenLayers.Layer.WMS(
		"Caminos",
		"http://myhost/cgi-bin/mapserv?", 
		{map: '/home/bicimapas/mapdata/BiciMapasMS.map', 
		layers:'caminos', transparent: true}, {isBaseLayer:false});

   
// Vector layer to draw the circle

   var vector_layer = new OpenLayers.Layer.Vector("Zona Influencia")
		  
		       
    
    //Add layers to the map
    map.addLayers([layer1, layer2, vector_layer]);

//Register the click event to get the clicked point coordinates

    map.events.register('click', map, Circulo);    
                
   var map_center = new OpenLayers.LonLat(-99.24801,19.49775); 
	 if(!map.getCenter()){
			map.setCenter(map_center);
		}
  	}

// Function to draw the circle around the clicked point

function Circulo(evt)
    {
       var lonlat = map.getLonLatFromViewPortPx(evt.xy);
       // use lonlat
	var circleout = new
OpenLayers.Geometry.Polygon.createRegularPolygon(lonlat, 0.0096, 50); 
	vector_layer.addFeatures(new OpenLayers.Feature.Vector(circleout)); 
    } 

    </script>
</head>

<body	onload='init();'> mapa de prueba
    <div id='map_element' style='width: 700px; height: 500px; border: 1px
solid black'>
	</div>
</body>
</html>

Thanks in advance for your help

Alberto Najera


_______________________________________________
Users mailing list
Users at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/openlayers-users




More information about the Users mailing list