I see what happened: There were copies of your original message in both the
V2 and V3 groups, and I replied over in the V3 group. Here for the sake of
completeness is a combined and slightly edited copy of my two messages from
that group, with the correction I noted in the code. You've already figured
out the first part, of course...
The immediate cause of your problem is that the 'k' and 'points2' variables
don't have the values you expect when your 'click' listener function runs.
Note the order of execution:
1) When your code first runs, it all runs except for the click listener (or
any other event listeners). Your two big outer loops both run to completion.
GEvent.addListener() is called each time through the second loop, but it
just saves a reference to the click listener function instead of calling it.
Time passes, and eventually the user clicks...
2) Now, the click listener function runs.
What are the values of 'k' and 'points2' in step 2? Whatever they were last
set by that big loop - because that loop ran to completion during step 1.
Some other problems I saw in the code:
* One big function does two different things. Break it into two or more
functions.
* "Parallel arrays/objects" - several arrays or objects that are all indexed
by the same key - regionPolys, regionBounds, etc. Instead, create a single
array (or object) containing individual objects that have the properties you
need. So you'd have a list of region objects, each of which has properties
like region.polys, region.bounds, etc.
* Confusion between objects {} and arrays []. Don't use "for( key in array
)" on an array, only on an object. Don't use an array when you'll be
accessing it with arbitrary string keys and you don't need the "array-like"
behavior of things like .push(), .length, etc. - use an object instead.
* Not a problem per se, but is the use of XML a requirement? If you have the
option of using JSON for your data instead of XML, you could remove most of
the XML parsing code.
So assuming the use of XML, here is a possible way to structure the code:
function processXML( xmlDoc ) {
if( ! xmlDoc ) {
alert( 'Invalid XML file' );
return;
}
var regions = parseRegionsXML( xmlDoc );
addRegions( regions );
}
function parseRegionXML( xmlDoc ) {
var doc = xmlDoc.documentElement;
var xmlRegions = doc.getElementsByTagName('region');
var nRegions = xmlRegions.length;
var regions = {};
for( var i = 0; i < nRegions; i++ ) {
var xmlRegion = xmlRegions[i];
// TODO: Should this use xmlRegion.getElementsByTagName?
var xmlPoints = xmlRegion.childNodes;
var nPoints = xmlPoints.length;
if( nPoints == 0 )
continue;
var points = [];
for( var j = 0; j < nPoints; j++ ) {
var xmlPoint = xmlPoints[j];
points.push( new GLatLng(
+xmlPoint.getAttribute('lat'),
+xmlPoint.getAttribute('lng')
) );
}
points.push( points[0] );
var id = xmlRegion.getAttribute('id')
regions[id] = {
id: id,
points: points
};
}
return regions;
}
function addRegions( regions ) {
for( var id in regions ) {
addRegion( regions[id] );
}
}
function addRegion( region ) {
region.poly = new GPolygon(
region.bounds, '#ff0000', 3, 0.5, '#0000ff', 0.3,
{ clickable:true }
);
region.polyTransparent = new GPolygon(
region.bounds, '#ff0000', 3, 0.5, '#0000ff', 0.0,
{ clickable:false }
);
map.addOverlay( region.poly );
var labelAnchorPoint = region.poly.getBounds().getCenter();
var label = region.label = new ELabel( labelAnchorPoint, region.id,
'style1' );
label.pixelOffset = new GSize( -40, 0 );
map.addOverlay( label );
GEvent.addListener( region.poly, 'click', function(zoomToZone) {
var bounds = new GLatLngBounds();
var points = region.bounds;
for( var q = 0; q < points.length; q++ ) {
bounds.extend( points[q] );
}
map.setZoom( map.getBoundsZoomLevel(bounds) );
// map.setCenter( bounds.getCenter() );
map.removeOverlay( region.poly );
map.removeOverlay( region.label);
// map.addOverlay( region.polyTransparent );
});
}
Give a shout with any questions about any of the changes in that code... :-)
-Mike
On Mon, May 2, 2011 at 4:06 PM, knieriemrt <[email protected]> wrote:
> Mike,
>
> The only reply I can see of yours is this last one. If you have a minute
> can you repost, it sounds like they would be very useful to me.
>
> Thanks!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google Maps API V2" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-maps-api?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Google Maps API V2" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-maps-api?hl=en.