Created
January 30, 2012 12:33
-
-
Save gingemonster/1704172 to your computer and use it in GitHub Desktop.
Bing Maps Ajax v7 Find Polygons in map view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
<html> | |
<head> | |
<title>Polyline default</title> | |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script> | |
<script type="text/javascript"> | |
var map = null; | |
function getMap() | |
{ | |
map = new Microsoft.Maps.Map(document.getElementById('myMap'), {credentials: 'Your Bing Maps Key',zoom:10}); | |
addPolygons(); | |
} | |
function addPolygons() | |
{ | |
latlon = map.getCenter(); | |
var polygon1 = new Microsoft.Maps.Polygon([new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude+0.15), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15)], null); | |
polygon1.name = "poly 1";// just so we can identify them later | |
latlon = {latitude:52,longitude:-0.2}; | |
var polygon2 = new Microsoft.Maps.Polygon([new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude+0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude+0.15), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude+0.05), new Microsoft.Maps.Location(latlon.latitude-0.1, latlon.longitude-0.05), new Microsoft.Maps.Location(latlon.latitude, latlon.longitude-0.15)], null); | |
polygon2.name = "poly 2";// just so we can identify them later | |
map.entities.push(polygon1); | |
map.entities.push(polygon2); | |
} | |
function findPolygonsInView(){ | |
// get current map view's bounding box | |
var bb = map.getBounds(); | |
//loop all polygons | |
for(var i = 0; i < map.entities.getLength();i++){ | |
// get entity | |
var entity = map.entities.get(i); | |
// recommend you put polygons in their own entity collection so you can just loop the polygons, not needed here as example doesnt plot anything other than polygons | |
// loop every vertex of polygon and check if it intersects the current map view | |
var locs = entity.getLocations(); | |
for(var j = 0; j < locs.length; j++){ | |
if(bb.contains(locs[j])){ | |
alert(entity.name + " is currently in view"); | |
break; //dont need to check any more vertex as if one is in view thats enough | |
} | |
} | |
} | |
} | |
</script> | |
</head> | |
<body onload="getMap();"> | |
<div id='myMap' style="position:relative; width:800px; height:600px;"></div> | |
<div> | |
<input type="button" value="Find Polygons In View" onclick="findPolygonsInView();" /> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment