Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am aiming to create something simple that works like a GPS navigation display, where the marker for 'you are here' is fixed while the map moves along. I have latitude and longitude coordinates supplied but when I move the map, the marker moves too - even though I tried setting the marker's layer to isFixed: true.

Here's what I'm using to set up the map:

var map = new OpenLayers.Map('map');
var baseLayer = new OpenLayers.Layer.MapServer('base layer', 'url', {map: 'something.map'}, {layers: 'base'}, {transitionEffect: 'resize'}, {gutter:'15'});

var markerLayer = new OpenLayers.Layer.Vector('marker', {isFixed : true});
var feature = new OpenLayers.Feature.Vector(
  new OpenLayers.Geometry.Point(100, 1.5),
  {some:'data'}, {externalGraphic: 'img/marker.png', graphicHeight:25, graphicWidth:30});
markerLayer.addFeatures(feature);

map.addLayer(baseLayer);
mapp.addLayer(markerLayer);
map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);

A script at the bottom of my HTML updates lat and lon with coordinates (in degrees) pushed to it every second via a server-side event. After that, every second I am calling map.setCenter(new OpenLayers.LonLat(lon, lat), zoom) to re-center the map.

At first I thought that if I drew my feature on a new, fixed layer, it wouldn't move, but the feature seems to be being drawn at 100N 1.5E, and staying there despite me panning the map. I want the marker to just stay where it is no matter how you pan the map.

P.S. My map re-centering feels rather inefficient as I am creating a new LonLat every time, but I couldn't figure out from the API docs any way to directly update it e.g.:

center = new OpenLayers.LonLat(lon, lat);
map.setCenter(center, zoom);
FOR every second
  ... update lon and lat with SSE ...
  center = new OpenLayers.LonLat(lon, lat);
  map.setCenter(center, zoom);
END FOR

Any help or suggestions would be much appreciated! :)

share|improve this question
1  
You say you want the point to stay in the same place while you pan the map. Do you mean the same place geographically or relative to the screen/map window? If you wanted a car GPS like system you would set the marker latlng and the map centre latlng to the same value every 1 second. –  BStone Jul 2 '13 at 9:06
    
Relative to the map window. And I am currently trying to do that - but I can only see a move() function that displaces the marker (geometry.point) and no function for me to set the value directly. Unless there is a way to directly alter attributes of the object...? –  Jessica Yang Jul 2 '13 at 9:41

2 Answers 2

up vote 0 down vote accepted

A quick and easy option would be to just remove the existing point and push a new point to the layer every second, which based on your code above, would look like this:

FOR every second
    ... update lon and lat with SSE ...
    LonLat = {new values}
    map.setCenter(center, zoom);
    var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(center),
         {some:'data'}, 
         {externalGraphic: 'img/marker.png', graphicHeight:25, graphicWidth:30}
    );
    markerLayer.removeAllFeatures();
    markerLayer.addFeatures(feature);
END FOR
share|improve this answer
    
The main iffy thing is that it feels weird to be constructing a new object every second without destroying the old ones. But a bit more tinkering that's pretty much what I have. Right now there's an oddity where sometimes the feature moves a tiny bit without the map moving (I am guessing since the map is still more-or-less 'centered' on the new coordinates of the feature), and then the next second the map moves to center on the new coordinates while the feature stays in the center. –  Jessica Yang Jul 3 '13 at 7:45
    
Good point, you could destroy the layer and recreate. Not sure about the map and point not always updating. Perhaps the basemap layer has some sort of tolerance to prevent the map from reloading if it is essentially the same spot. –  BStone Jul 3 '13 at 12:29
    
I'm pretty sure that's what's happening with the basemap. I profiled things for a bit and I don't think creating a new object each second is really doing much harm, so sticking with that for now. Thanks! –  Jessica Yang Jul 4 '13 at 8:49

Here is what I'm currently using to display an icon of a plane that stays fixed while the map moves. It seems to work well.

Global variables at the top of my script: lat, lon, heading.

In the StyleMap for the iconLayer, specify rotation:'${angle}' in options. This will get the attribute angle (which I am using to store the heading of the plane) from features on that layer later.

In my updating script: every second, assign lat, lon, heading new values from the server; and then call map.setCenter([lon, lat]) and also this:

iconLayer.removeAllFeatures();
iconFeature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(lon, lat), {angle: heading});
iconLayer.addFeatures(iconFeature);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.