From 73fb43712fd5cac6055f2bde7ee0f61bad8d21fc Mon Sep 17 00:00:00 2001 From: euzuro Date: Wed, 27 Sep 2006 21:56:17 +0000 Subject: [PATCH] add onScreen() function to OpenLayers.Feature, and add tests to prove it. git-svn-id: http://svn.openlayers.org/trunk/openlayers@1510 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf --- lib/OpenLayers/Feature.js | 15 +++++++++++++++ tests/test_Feature.html | 26 ++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/lib/OpenLayers/Feature.js b/lib/OpenLayers/Feature.js index e78e4e2cfc..0f3cb5760b 100644 --- a/lib/OpenLayers/Feature.js +++ b/lib/OpenLayers/Feature.js @@ -72,6 +72,21 @@ OpenLayers.Feature.prototype= { } }, + /** + * @returns Whether or not the feature is currently visible on screen + * (based on its 'lonlat' property) + * @type Boolean + */ + onScreen:function() { + + var onScreen = false; + if ((this.layer != null) && (this.layer.map != null)) { + var screenBounds = this.layer.map.getExtent(); + onScreen = screenBounds.containsLonLat(this.lonlat); + } + return onScreen; + }, + /** * @returns A Marker Object created from the 'lonlat' and 'icon' properties diff --git a/tests/test_Feature.html b/tests/test_Feature.html index a700f3eb75..aaaae5bae4 100644 --- a/tests/test_Feature.html +++ b/tests/test_Feature.html @@ -76,6 +76,32 @@ "Layer div img contains correct url" ); */ } + + function test_03_Feature_onScreen(t) { + t.plan( 2 ); + + var map = new OpenLayers.Map("map"); + + var url = "http://octo.metacarta.com/cgi-bin/mapserv"; + var wms = new OpenLayers.Layer.WMS(name, url); + + map.addLayer(wms); + + var layer = new OpenLayers.Layer("foo"); + map.addLayer(layer); + + map.zoomToExtent(new OpenLayers.Bounds(-50,-50,50,50)); + + //onscreen feature + var feature1 = new OpenLayers.Feature(layer, + new OpenLayers.LonLat(0,0)); + t.ok( feature1.onScreen(), "feature knows it's onscreen" ); + + //onscreen feature + var feature2 = new OpenLayers.Feature(layer, + new OpenLayers.LonLat(100,100)); + t.ok( !feature2.onScreen(), "feature knows it's offscreen" ); + } // -->