[OpenLayers-Users] Place an image in the corner?

Curtis Olson curtolson at gmail.com
Fri May 4 10:32:54 EDT 2012


On Fri, May 4, 2012 at 8:23 AM, Keith Wiley wrote:

> I want to place a "legend" on the map, effectively an image which stays at
> a particular pixel-location in the view, without being tied to geographic
> coordinates (and which doesn't scale with zoom).  I didn't see an example
> that does this.
>

I asked a similar question not too long ago and the one suggestion I got
back was that some combination of css could probably work.  Maybe you could
look for an example of how to do the google watermark or attribution I
think it's called.

I have to confess that from my perspective css is convoluted and
complicated with pieces of the puzzle getting spread through several files
-- so it can be difficult to track through all the relevant pieces and get
everything figured out -- but maybe that's just me.  And maybe that's just
the price you pay for developing apps that run inside a web browser.

In my case I wasn't just dropping a static image on the screen, I needed to
update the contents (the color, the label text, rotation, etc.)  I'm sure
there is still a css-based solution out there I could pursue, but for the
short term I did the first thing that made sense to me that I could figure
out on my own.

So bear with me here -- personally when it comes to drawing things in
open-layers, I've had (by far) the most luck using points. And then
specifying a point type (such as circle, square, or my own custom icon --
which is easy to define.)  I've also done this for placing "external
graphics" and that's worked out quite well.   Once the icon/graphic is
placed, I can update the label, rotate it, move it, change color, etc. by
keeping track of the "style" for each point.  I've messed around a bit with
polygon features for trying to draw simple things, but I always come back
to using points with custom icons or external graphics.

To actually place the object in a fixed screen location, I do something
like the following (which is a combination of copy/paste out of my own code
and manual typing):

    var pt = new OpenLayers.Geometry.Point(screen_x, screen_y);
    var pos = map.getLonLatFromPixel( pt );
    var myfeature = myLayer.getFeatureByFid("feature");
    # (note that earlier when I created myfeature and added it to myLayer I
placed it at 0, 0 and assigned it a unqiue fid)
    myfeature.move( pos )

I've done a little work with the ordering of operations when the map
recenters which helps the objects to appear fixed, but I haven't played
around with keeping them fixed during a zoom -- I know there are move &
zoom call backs so presumably you could always reposition your objects
ahead of when the map gets redrawn so they always appear fixed on screen.
 Points and external graphics maintain a fixed pixel size independent of
zooming so that's another reason they are nice to use instead of polygons
with each individual vertex defined in world coordinate space.

Another thing I played with for a different app was to create an empty map
with lon/lat coordinates that match the screen pixel space (i.e. 0,0 to
screen.width, screen.height).  That then gives you a fixed area to work in
so you can do "game" like things -- placing graphics with sensible x, y
coordinates, rotating, translating them.

What would be interesting is if there was a way to overlay a "map" with
this simple screen-matching coordinate system on top of a world map in
order to do fixed objects on top of the map, but I haven't seen a way to do
that.  Open-layers is cool and powerful and I'm very grateful for it's
existence -- but it does weave a tangled web of class inheritance and it's
always a challenge to dig through layer after layer of class documentation
to try to see if a particular feature or function or piece of data even
exists and if so, how to leverage it.

I'm just learning openlayers myself here, so I'd love to hear feedback if
there are better ways to do this stuff.

Curt
-- 
Curtis Olson:
http://www.atiak.com - http://aem.umn.edu/~uav/
http://www.flightgear.org - http://gallinazo.flightgear.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20120504/6d735422/attachment.html


More information about the Users mailing list