[OpenLayers-Users] Help with setLatLng

Jesse Bushkar apt94jesse at gmail.com
Mon Oct 5 12:41:49 EDT 2009


Hello all, this is my first post as I have only recently started using
OpenLayers.  It is awesome to say the least.  I'm currently using OpenLayers
integrated with Cloudmade so I hope that this question is being posted in
the appropriate place and to the appropriate group.

I'm attempting to use the new HTML javascript functions getCurrentPosition
and watchPosition to constantly update a marker (and the center) of a map.
These two functions are great, and I've successfully tied them into the
setCenter function so that the center of the map follows your position when
using a geolocation-aware browser.  However, when I try to set a marker and
then use the setLatLng function, I have no luck.  Below is the code.  It
appears that setLatLng function breaks the rest, because whenever that line
is there, the map does not re-center or re-zoom. If I comment it out, it
works fine but of course the marker doesn't adjust to the new coords.


--
success = function(position){

var epsg4326 = new OpenLayers.Projection("EPSG:4326");
var epsg900913 = new OpenLayers.Projection("EPSG:900913");
    var center = new OpenLayers.LonLat(position.coords.longitude,
position.coords.latitude).transform(epsg4326, epsg900913);
      var zoom = parseInt(map.center.zoom);
    OL.maps[map.id].map.setCenter(center, zoom, false, false);
var markers = new OpenLayers.Layer.Markers("Markers");
        OL.maps[map.id].map.addLayer(markers);
var marker = new OpenLayers.Marker(center);
                markers.addMarker(marker);
}

 fail = function(){
      alert("Your browser does not support Geolocation");
    var center = new OpenLayers.LonLat(map.center.lon, map.center.lat);
      var zoom = parseInt(map.center.zoom);
    OL.maps[map.id].map.setCenter(center, zoom, false, false);
}

updateLocation = function(position){ // Fires everytime your location change
positions
                                var epsg4326 = new
OpenLayers.Projection("EPSG:4326");
                                var epsg900913 = new
OpenLayers.Projection("EPSG:900913");
                                var newCenter = new
OpenLayers.LonLat(position.coords.longitude,
position.coords.latitude).transform(epsg4326, epsg900913);
                           var zoom = parseInt(map.center.zoom);
            marker.setLatLng(newCenter);
            OL.maps[map.id].map.setCenter(newCenter, 18);
            };

navigator.geolocation.getCurrentPosition(success, fail);

navigator.geolocation.watchPosition(updateLocation);

--

As you can see, there are three functions, then the two geolocation
functions are called below them.  This code is admittedly ugly and
incomplete since it is being taken from a much larger file, but I believe
all the things necessary to troubleshoot the 'marker.setLatLng(newCenter);'
line are present.

I'm thinking that the problem may stem from the local variables inside each
function: ie. inside of updateLocation() there is no marker defined, but I'm
not really sure what to do about that (or even if that is accurate).  My
javascript is improving but still not up to snuff, so I'm probably (and
hopefully) missing something very obvious here.

Thanks in advance for any help provided.  You can see the live file at
www.savannahtraveler.com/maps
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20091005/9beb3963/attachment.html


More information about the Users mailing list