[ka-Map-users] kaXmlOverlay.js bug
Justin George
justin.george at gmail.com
Wed Jun 7 17:34:40 EDT 2006
The problem is that your loop is modifying the array, so array size is
decreasing as the incrementer increases. For example:
for (var i=0; i < this.ovrObjects.length; i++) {
Say I have three elements in the ovrObjects array.
To start the loop
(i = 0, this.ovrObjects.length = 3)
Next iteration
(1, 2)
Then comes the problem: array length is greater than index, so it exits.
You are left with
(2, 1)
and it gets worse as more objects are in the array (eg left with 5
elements on a size-10 array)
Vs indexing from the top
(3, 3)
next iteration
(2, 2)
...
at the end you are left with:
(0, 0)
>From what I gather it's a pretty common problem when dealing with
linked lists and such, but this is the first time it's stymied me so
badly
Does that make sense?
On 6/7/06, Pg <pg.navone at gmail.com> wrote:
> Thanks. The bug is definetely there, but I don't understand your fix.
> I thik the correct method should be
>
> kaXmlOverlay.prototype.removePoint = function( pid ) {
>
> if ( (this.removePoint.arguments.length < 1) || (pid == null) ) {
> for (var i=0; i < this.ovrObjects.length; i++) {
> if (this.ovrObjects[i] != null) {
> this.ovrObjects[i].removeFromMap();
> delete this.ovrObjects[i];
> this.ovrObjects[i] = null;
> }
> delete this.ovrObjects[i];
> this.ovrObjects.splice(i,1); i--;
> }
> return;
> }
>
> var re = new RegExp(pid);
> for (var i=0; i < this.ovrObjects.length; i++) {
> if (this.ovrObjects[i] != null) {
> if (re.test(this.ovrObjects[i].pid)) {
> this.ovrObjects[i].removeFromMap();
> delete this.ovrObjects[i];
> this.ovrObjects[i] = null;
> this.ovrObjects.splice(i,1); i--;
> }
> } else {
> delete this.ovrObjects[i];
> this.ovrObjects.splice(i,1); i--;
> }
> }
> }
>
> -Pg
>
>
> 2006/6/7, Justin George <justin.george at gmail.com>:
> > I just spent quite a while tracking this one down.
> >
> > On line 558 in kaXmlOverlay.prototype.removePoint, there is a loop
> > which indexes over the contents of the overlay objects array. Each
> > time it removes an object. So the array must index from the length
> > down to zero, rather than the opposite.
> >
> > Fix:
> > for (var i=this.ovrObjects.length; i >= 0; i--) {
> >
> > Instead of
> >
> > for (var i=0; i < this.ovrObjects.length; i++) {
> >
> > Hope that helps everyone with the new overlays, they're working great for me.
> > _______________________________________________
> > ka-Map-users mailing list
> > ka-Map-users at lists.maptools.org
> > http://lists.maptools.org/mailman/listinfo/ka-map-users
> >
>
More information about the ka-Map-users
mailing list