Adding getCurrentLocation API method so that application can get the location anytime, p=aabt, r=elemoine, (Closes #3090)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@11315 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
pgiraud
2011-02-23 13:33:14 +00:00
parent f55291bd34
commit 61452e2821
2 changed files with 48 additions and 9 deletions
+23 -9
View File
@@ -85,7 +85,7 @@ OpenLayers.Control.Geolocate = OpenLayers.Class(OpenLayers.Control, {
this.events.triggerEvent("locationuncapable"); this.events.triggerEvent("locationuncapable");
return false; return false;
} }
if (!this.active) { if (OpenLayers.Control.prototype.activate.apply(this, arguments)) {
if (this.watch) { if (this.watch) {
this.watchId = this.geolocation.watchPosition( this.watchId = this.geolocation.watchPosition(
OpenLayers.Function.bind(this.geolocate, this), OpenLayers.Function.bind(this.geolocate, this),
@@ -93,16 +93,11 @@ OpenLayers.Control.Geolocate = OpenLayers.Class(OpenLayers.Control, {
this.geolocationOptions this.geolocationOptions
); );
} else { } else {
this.geolocation.getCurrentPosition( this.getCurrentLocation();
OpenLayers.Function.bind(this.geolocate, this),
OpenLayers.Function.bind(this.failure, this),
this.geolocationOptions
);
} }
return true;
} }
return OpenLayers.Control.prototype.activate.apply( return false;
this, arguments
);
}, },
/** /**
@@ -145,6 +140,25 @@ OpenLayers.Control.Geolocate = OpenLayers.Class(OpenLayers.Control, {
}); });
}, },
/**
* APIMethod: getCurrentLocation
*
* Returns:
* {Boolean} Returns true if a event will be fired (successfull
* registration)
*/
getCurrentLocation: function() {
if (!this.active || this.watch) {
return false;
}
this.geolocation.getCurrentPosition(
OpenLayers.Function.bind(this.geolocate, this),
OpenLayers.Function.bind(this.failure, this),
this.geolocationOptions
);
return true;
},
/** /**
* Method: failure * Method: failure
* method called on browser's geolocation failure * method called on browser's geolocation failure
+25
View File
@@ -62,6 +62,31 @@
map.removeControl(control); map.removeControl(control);
map.setCenter(centerLL); map.setCenter(centerLL);
} }
function test_getCurrentLocation(t) {
t.plan(5);
var control = new OpenLayers.Control.Geolocate({
geolocation: geolocation
});
map.addControl(control);
t.eq(control.getCurrentLocation(), false, 'getCurrentLocation return false if control hasnt been activated');
control.activate();
map.setCenter(centerLL);
t.eq(control.getCurrentLocation(), true, 'getCurrentLocation return true if control has been activated');
var center = map.getCenter();
t.eq(center.lon, 10, 'bound control sets the map lon when calling getCurrentLocation');
t.eq(center.lat, 10, 'bound control sets the map lat when calling getCurrentLocation');
control.deactivate();
map.removeControl(control);
map.setCenter(centerLL);
var control2 = new OpenLayers.Control.Geolocate({
geolocation: geolocation
});
map.addControl(control2);
t.eq(control2.getCurrentLocation(), false, 'getCurrentLocation return false if control is in watch mode');
control2.deactivate();
map.removeControl(control2);
map.setCenter(centerLL);
}
function test_watch(t) { function test_watch(t) {
t.plan(2); t.plan(2);
var control = new OpenLayers.Control.Geolocate({ var control = new OpenLayers.Control.Geolocate({