//in my init function, the maps extent changed handler is set using
dojo.connect(map, 'onExtentChange', Extent_Change);
function clickLink(id) {
//generic handler for all of the links in the student list
map.infoWindow.hide();
var graphic;
dojo.forEach(map.graphics.graphics, function (g) {
if (g.attributes["ID"] == id)
{
graphic = g;
}
});
//tmpGraphic and bolClickLink are global in scope
tmpGraphic = graphic;
bolClickLink = true;
map.centerAndZoom(graphic.geometry, map.getNumLevels()-3);
}
function Extent_Change(ext, delta, bolLevel,lod) {
var e = new Object;
if (bolClickLink) {
e.graphic = tmpGraphic;
e.screenPoint = map.toScreen(tmpGraphic.geometry);
e.mapPoint = tmpGraphic.geometry;
graphicClick(e);
}
bolClickLink = false;
}
function graphicClick(evt) {
map.infoWindow.setTitle("Flat Stanley");
map.infoWindow.setContent(evt.graphic.attributes["Name"] + "<br/>" + evt.graphic.attributes["Place"]);
map.infoWindow.show(evt.screenPoint, map.getInfoWindowAnchor(evt.screenPoint));
}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<title> Demo </title>
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.0/js/dojo/dijit/themes/tundra/tundra.css" />
<style type="text/css">
html, body {
height: 100%; width: 100%; margin: 0;
}
body{
background-color:white; overflow:hidden;
font-family: "Trebuchet MS";
}
#mapDiv
{
height: 100%; width: 100%; margin: 0;
}
</style>
<script type="text/javascript">
var djConfig = {
parseOnLoad: true
};
</script>
<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.0">
</script>
<script type="text/javascript">
dojo.require("esri.map");
//global for the Map
var map;
//globals for various map services
var PhotoMap = new esri.layers.ArcGISTiledMapServiceLayer("http://services.arcgisonline.com/ArcGIS/rest/services/I3_Imagery_Prime_World_2D/MapServer", { id: "Photo" });
function init() {
map = new esri.Map("mapDiv");
map.addLayer(PhotoMap);
}
function clickLink() {
//generic handler for all of the links in the student list
var pt = new esri.geometry.Point(getRandomCoord('x'), getRandomCoord('y'), new esri.SpatialReference({ wkid: 4326 }));
var sms = new esri.symbol.SimpleMarkerSymbol();
var graphic = new esri.Graphic(pt, sms);
map.graphics.add(graphic);
map.centerAndZoom(pt, map.getNumLevels() - 3);
}
function getRandomCoord(axis) {
switch (axis) {
case 'x':
return (Math.random() * -179);
case 'y':
return (Math.random()* 80) ;
default:
return -20.0;
}
}
//show map on load
dojo.addOnLoad(init);
</script>
</head>
<body class="tundra">
<a href ='' title='' onclick='clickLink();return false'>click me</a><br/>
<div id="mapDiv" ></div>
</body>
</html>
var attr = { "XCoord": pt.x, "YCoord": pt.y};
var info = new esri.InfoTemplate("title","Latitude: ${YCoord} <br/> Longitude: ${XCoord}");
var graphic = new esri.Graphic(pt, sms, attr, info);
A Graphic can contain geometry, a symbol, attributes, or an infoTemplate.
if (dojo.isIE) {
map.centerAndZoom(pt, zoomLevel);
map.setLevel(zoomLevel -2 );
map.setLevel(zoomLevel);
}
else {
map.centerAndZoom(pt, zoomLevel);
}
Graphics are not required to have all of the above items, and no one item is required