[OpenLayers-Users] layers.Markers feature events.

Nicholas Efremov-Kendall n.e.kendall at gmail.com
Mon Jun 6 08:54:00 EDT 2011


Hi all,

In trying to get my little geolocation script to work, I'm stumbling across
the following. Can anyone suggest how to modify this script in order to
listen to the addMarker event? I realize that this isn't working because the
 markers layer doesn't load, but I'm not familiar enough with the syntax in
order to modify this. Is there a simple event identifier I can pass to the
markers.events.register? Full code below.

if(! map.getCenter()){
if(markers)markers.events.register('loadend', markers, function(){
map.zoomToExtent(markers.getDataExtent());}
);
} else{
map.setCenter(new OpenLayers.LonLat(-90, 38).transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject()),12);
}

Thanks in advance!



<!doctype html>
<html>
<head>
<style>
#map{
width:400px;
height:400px;
}
</style>

</head>
<body onLoad="initGIS()">
<div id="map"></div>
<button id="btnInit" >Find my location</button>
<script type="text/javascript" language="javascript"
src="OpenLayers-2.11-rc1/OpenLayers.js"></script>
<script tyle="text/javascript" language="javascript"
src="moderinzr2.js"></script>
<script type="text/javascript" language="javascript"
src="jquery-1.6.js"></script>
<script src="http://www.openstreetmap.org/openlayers/OpenStreetMap.js
"></script>
<script type="text/javascript" language="javascript">
var markers = new OpenLayers.Layer.Markers("Markers");
var map = new OpenLayers.Map('map');
function initGIS(){
map.addControl(new OpenLayers.Control.LayerSwitcher());
var mapnik = new OpenLayers.Layer.OSM();
map.addLayer(mapnik,markers);
if(! map.getCenter()){
if(markers)markers.events.register('loadend', markers, function(){
map.zoomToExtent(markers.getDataExtent());}
);
}


jQuery(window).ready(function(){jQuery("#btnInit").click(getLoc);});
function getLoc(){
if (Modernizr.geolocation) {
 navigator.geolocation.getCurrentPosition(showMap,handle_error);
} else {
yqlgeo.get('visitor',norm_yql_resp);
}
}


function showMap(position)
{
//alert('Lat: ' + position.coords.latitude + ' ' + 'Lon: ' +
position.coords.longitude + ' ' + 'Acc: ' + position.coords.accuracy);
var lonlat = new
OpenLayers.LonLat(position.coords.longitude,position.coords.latitude).transform(new
OpenLayers.Projection("EPSG:4326"),map.getProjectionObject());
markers.addMarker(new OpenLayers.Marker(lonlat));
//map.setCenter(new OpenLayers.LonLat(lonlat, 12));
map.addLayer(markers);
}

function handle_error(error) {
switch(error.code){
case error.PERMISSION_DENIED: alert("This application reguires you to agree
to share your location. Please reload the page to see it work properly.
There is a privacy disclaimer at the bottom of this page. This site does not
currently record or store these data, and in the future will not record
unique user information without informing the user or asking their
permission. -Virtual Spatiality");
break;

case error.POSITION_UNAVAILABLE: alert("could not determine position");
break;

case error.TIMEOUT: alert("timeout retrieving position");
break;

default: alert("unknown error");
break;
}
}

function norm_yql_resp(response){
if (response.error) {
var error = {code : 0};
handle_error(error);
return;
}
var position = {
coords:
{
latitude: response.place.centroid.latitude,
longitude: response.place.centroid.longitude
},
address :
{
city: response.place.locality2.content,
region: response.place.admin1.content,
country: response.place.country.content
}
};
handle_geolocation_query(position);
}}
</script>
</body>
</html>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/openlayers-users/attachments/20110606/3c3e55a2/attachment-0001.html


More information about the Users mailing list