zoom to polygon after selecting name from drop down menu

2,289 views
Skip to first unread message

Nate_LR

unread,
Feb 18, 2012, 4:28:48 PM2/18/12
to Google Maps JavaScript API v3
I've made a google map mashup displaying polygons created from an xml
file. I'm wanting the map to zoom into an individual polygon after
it's name is selected from a drop down menu. I'm trying to use a
polygon's shape or center to do this and just can't figure out the
proper syntax do it. See snippets of the script below or visit
(http://www.geology.ar.gov/test/quad24k_map.htm). Thanks!

var quadArray = [];
var q = 0;

function createPolygon(pts, html, latlng, color, name) {

var quads = new google.maps.Polygon(
{paths: pts,
strokeColor: '#FFFFFF',
strokeOpacity: 0.7,
strokeWeight: 0.5,
fillOpacity: 0.5,
fillColor: color
}
);

google.maps.event.addListener(quads, 'mouseover', function(event)
{

if (infowindow) infowindow.close();
infowindow = new google.maps.InfoWindow({content:
html});
infowindow.setPosition(latlng);
infowindow.open(map);

});

quads.setMap(map);

document.forms['QuadSelect'].quadSel.options[q] = new
Option(name,q); //poplulates drop down menu

//testing
//quads.cPt = latlng;
//quads.area = new google.maps.Polygon({paths: pts})
//quadArray.push(quads[q])
quadArray[q] = quads;
q++;

} //end createPolygon



function selectQuadZoom(qNum){

//var selectedQ =
viewQuad.options[viewQuad.selectedIndex].value;

/ map.setCenter(new
google.maps.LatLng(quadArray[selectedQ].cPt));
// map.setZoom(12);

var qBounds = new google.maps.LatLngBounds();
qBounds.extend(quadArray[qNum]);
map.fitBounds(qBounds);
//google.maps.event.trigger(quadArray[selectedQ], "click");
}



<form name="QuadSelect">
<select name="quadSel" id="quadSel"
onChange="selectQuadZoom(this.value)">
<option value=""></Option>
</select>
</form>

geoco...@gmail.com

unread,
Feb 18, 2012, 6:38:17 PM2/18/12
to Google Maps JavaScript API v3
On Feb 18, 4:28 pm, Nate_LR <razorn...@gmail.com> wrote:
> I've made a google map mashup displaying polygons created from an xml
> file.  I'm wanting the map to zoom into an individual polygon after
> it's name is selected from a drop down menu.   I'm trying to use a
> polygon's shape or center to do this and just can't figure out the
> proper syntax do it.  See snippets of the script below or visit
> (http://www.geology.ar.gov/test/quad24k_map.htm).  Thanks!
>

You mean something like this:

http://www.geocodezip.com/www_geology_ar_gov_quad24k_mapA.html

(uses a ported version of Mike Williams' epoly extension to retrieve
the bounds of the polygons)

-- Larry

geoco...@gmail.com

unread,
Feb 18, 2012, 6:43:44 PM2/18/12
to Google Maps JavaScript API v3
On Feb 18, 6:38 pm, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:
Note that for performance reasons you may want to use kml and either
KmlLayer or FusionTablesLayer to display it, which will get rendered
as tiles on Google's servers rather than native v3 polygons (but then
the zoom to polygon will change).

  -- Larry

Nate_LR

unread,
Feb 18, 2012, 7:26:43 PM2/18/12
to Google Maps JavaScript API v3
Thanks a lot Larry! :)
~ Nate_LR

On Feb 18, 5:43 pm, "geocode...@gmail.com" <geocode...@gmail.com>

xelawho

unread,
Feb 18, 2012, 7:27:13 PM2/18/12
to Google Maps JavaScript API v3
you can do this quite simply by passing your polygon to a function
that calculates and returns the bounds:

function getBoundsForPoly(poly) {
var bounds = new google.maps.LatLngBounds;
poly.getPath().forEach(function(latLng) {
bounds.extend(latLng);
});
return bounds;
}


with a function call like this (assuming your polygon is called poly):

bounds=getBoundsForPoly(poly)

and then a good ol'

map.fitBounds(bounds)

you can see it in action here http://www.xelawho.com/map/ on the
"Zones Info" dropdown

(and a belated thanks to whoever it was from the forum who I
"borrowed" the above code from)

Reply all
Reply to author
Forward
0 new messages