OpenLayers: How to set up alert() on polygons on click - vector

I tried the last week very hard to fix my problem, but I didn't find an answer. I hope you can help me:
I want to install several polygons (resembling buildings on my campus), using a OpenStreetMap as base layer. When a polygon is clicked with the mouse, I want to produce a specific alert() or other output, which belongs to exactly the clicked polygon (I want to give informations about the clicked building). All examples I found just worked with ONE click event for each polygon, because all polygons were set in one layer. When setting up a layer for each polygon, just the top layer remained clickable.
With Markers it works perfect, because I can identify every marker, even in the same layer:
llm = new OpenLayers.LonLat(Lon2Merc(lonm), Lat2Merc(latm));
var marker2 = new OpenLayers.Marker(llm, icon);
marker2.events.register("mousedown", marker2,
function() { alert("Hello marker 2"); });
layer_markers.addMarker(marker2);
with polygons its different...
I hope you can help me,
greets
Philip

Polygons are vector objects (features) and placed on vector layers (OpenLayers.Layer.Vector). The common way to manage clicks on polygons in OpenLayers is using SelectFeature control.
You should try this demo (and look into the source code):
http://openlayers.org/dev/examples/select-feature-openpopup.html

Related

how to know in which region is the markers using leaflet map

I want to the count of markers based on location using leaflet map
ie,
Say in india
Number of markers in bangalore
number of makers in mysore so on...
only thing i would think of to do this is using contains( point )
which is in the doc
But how to use it to find the maker is in that region as i will not be aware of the coordinates of the region
Is there a way to check like
region.contains(markerLatLang)
Leaflet .contains() works on L.LatLngBounds objects, which are actually a rectangular region. So it tells if your point / marker is within a rectangle or not.
You would need first to retrieve yourself the coordinates of your administrative areas of interest (India, etc.). See https://gis.stackexchange.com/questions/199765/where-to-get-world-wide-detailed-administrative-boundaries
BTW, a simple version of countries boundaries can be: https://github.com/johan/world.geo.json
Then you would probably want to use a more accurate detection method, for example leaflet-pip plugin or turf.js, which will actually check if your point is within arbitrarily shaped polygons, not just a rectangle.
See for instance Leaflet event: how to propagate to overlapping layers

How do I place the point of a leaflet marker on given lat/lon coordinates?

I have a leaflet map with lots of basic markers (about 1000 in germany, to be precise). The map is at
http://geotools.gurkensalat.com/osm-mosques/map
I will add a better link when I get permalinks to work, until then, the node which I am asking about is the one slightly to the east and north of the red marker.
When I create a marker for coordinates lat="48.1364" lon="11.3872928" and put it on the map, the marker is shown slightly to the left and bottom. The coordinates are exactly those of the OSM node itself. Any ideas how I can move the marker so the tip of the marker is exactly on the node, or at least pretty close to it?
It's impossible to tell without a live example, but my best bet would be that you have some CSS on your page that is interfering with the map and shifting your tiles up and to the right, or your markers down and to the left.
Nope, that's not it - you're truncating the numbers.
Desired:
[48.1364, 11.3872928, "OSM / Germering / Germering Camii"],
On this page:
[48.136, 11.387, "OSM / Germering / Germering Camii"],
So, the thing causing the problem is your source data, not Leaflet.

Grid based Clustering

I am trying to do a grid based clustering, specifically where each U.S. state is a grid. What I am doing now is just setting strategy option of ClusterProvider to STRATEGY_GRID_BASED, but I don't think this is what I think it is.
I tried looking into nokia.maps.clustering.IGridOptions, but there isn't anything documentation on that.
Can someone point me in the right direction? Thanks.
A Grid-based clustering strategy just breaks the map up into squares and calculates how many markers are found in each square. What you are after is known as a Chloropleth Map. The most efficient way to do this would be to cluster server-side, return a key,value pair (i.e. state, number of markers) and just display polygons for the US States. An example of such a map can be found in the HERE Maps community examples which displays the accession dates of states to the union.
If you insist on doing everything client side then you'd have to use the following pseudo code instead:
Iterate though each marker in your list:
Check to see if it is within the hit area of an existing polygon.
If it is - increment the markers counter (an additional attribute you have added.)
If it isn't make a WKT State-level shape geocoding request, and parse the result -add a new markers counter attribute to the Polygon
repeat
You will then end up with a series of polygon objects each holding an attribute with the number of markers within found within the state.

Checking mouse event on line in Context2d

If I have line like this (a xy plot in the future)
`Canvas {
id:canvas
onPaint:{
var ctx = canvas.getContext('2d');
ctx.moveTo(0,0);
ctx.lineTo(50,20);
ctx.lineTo(50,70);
// etc...
}
}`
so, is there a good way to check mouse press event on this line? i.e. If I have a plot and want to show context menu on right click on graph line
Unfortunately, AFAIK, you should keep track of what you draw. Canvas doesn't store drawn stuff as vector graphics(again, AFAIK), but on a raster, so there's no way to get it to tell you where the lines/points are.
You could push each point you pass to ctx.moveTo and ctx.lineTo to a list or other structure. Then, on click, iterate over all line segments (designated by pairs of points you stored) and check if distance of the clicked point to the line segment is within some selection tolerance distance you want.
To check the distance, you can use this:
Shortest distance between a point and a line segment
I don't know if this is the simplest way, but it works.

How does a non-tile based map works?

Ok, here is the thing. Recently i decided i wanted to understand how Random map generation works. I found some papers and some arguments. The most interesting one was "Diamond Square algorithm" and "Midpoint Displacement". I still have to try to apply those to a software, but other than that, i ran into this site: http://www-cs-students.stanford.edu/~amitp/game-programming/polygon-map-generation/
As you can see, the idea is to use polygons. But i have no idea how to apply that a Tile-Based map, not even how to create those polygons using the tools i have (c++ and sdl). I am assuming there is no way to do it ( please correct me if i am wrong.) But if i am not, how does a non-tile map works, and how are these polygons generated?
This answer will not give you directly the answers you're looking for, but hopefully will get you close enough!
The Problem
I think what blocks you is how to represent the data. You're probably used to a 2D grid that simply represent the type of each tile. As you know, this is fine to handle a tile-based map, but doesn't properly allow you to model worlds where tiles are of a different shape.
Graphs
What I suggest to you, is to see the problem a bit differently. A grid is nothing more than a graph (more info) with nodes that have 4 (or 8 if you allow diagonals) implicit neighbor nodes. So first, what I would do if I was you, would be to move from your strict standard 2D grid to a more "loose" graph, where each node has a position, a position, a list of neighbors (in most cases you'll have corners with 2 neighbors, borders with 3 and "middle" tiles with 4) and finally a rendering component which simply draws your tile on screen at the given position. Once this is done, you should be able to have the exact same results on screen that you currently have with your "2D Tile-Based" engine by simply calling the rendering component with each node who's bounding box (didn't touch it in what you should add to your node, but I'll get back to this later) intersects with the camera's frustum (in a 2D world, it would most likely if the position +/- the size intersects the RECT currently being drawn).
Search
The more generic approach will also help you doing stuff like pathfinding with generic algorithms that explore nodes until they find a valid path (see A* or Dijkstra). Even if you decided to stick to a good old 2D Tile Map game, these techniques would still be useful!
Yeah but I want Polygons
I hear you! So, if you want polygons, basically all you need to do, is add to your nodes a list of vertices and the appropriate data that you might need to render your polygons (either vertex color, textures and U/V maps, etc...) and update your rendering component to do the appropriate OpenGL (this for example should help) calls to draw your nodes. Once again, the first step to iteratively upgrade your 2D Tile Engine to a polygon map engine would be to, for each tile in your map, give each of your nodes two triangles, a texture resource (the tile), and U/V mappings (0,0 - 0,1 - 1,0 and 1,1). Once again, when this step is done, you should have a "generic" polygon based tile map engine. The creation of most of this data can be created procedurally by calculating coordinates based on tile position, tile size, etc...
Convex Polygons
If you decide that you ever might need NPCs to navigate on your map or want to allow your player to navigate by clicking the map, I would suggest that you always use convex polygons (the triangle being the simplest for of a convex polygon). This allows your code that assume that two different positions on the same polygon can be navigated to in straight line.
Complex Maps
Based on the link you provided, you want to have rather complex maps. In this case, the author used Voronoi Diagrams to generate the polygons of the map. There are already solutions to do triangulation like that, but you might also want to use other techniques that are easier to work with if you're just switching to 3D like this one for example. Once you have interesting results, you should consider implementing serialization to save/open your map data from the game. If you want to create an editor, be aware that it might be a lot of work but can be worth it if you want people to help you creating maps or to add elements to the maps (like geometry that's not part of the terrain).
I went all over the place with this answer, but hopefully it helps!
Just iterate over all the tiles, and do a hit-test from the centre of the tile to the polys. Turn the type of the tile into the type of the polygon. Did you need more than that?
EDIT: Sorry, I realize that probably isn't helpful. Playing with procedural algorithms can be fun and profitable. Start with a loop that iterates over all tiles and chooses randomly whether or not the tile is occupied. Then, iterate over them again and choose whether it is occupied or its neighbour is.
Also, check out the source code for this: http://dustinfreeman.org/toys/wall7-dustin.html

Resources