How to directly draw points and text with ArcGIS JavaScript API

Rock picture Rock · Oct 19, 2012 · Viewed 9.4k times · Source

I am new to ArcGIS.

I want to directly draw some points and some text on the map.

something like below. but it just show map and draw nothing.

anyone can help me fix this problem?

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Draw point and text</title>
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.1/js/dojo/dijit/themes/tundra/tundra.css">
<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis?v=3.1"></script>
<script type="text/javascript">
dojo.require("esri.map");
dojo.require("esri.layers.agsdynamic");
var map;
var layer;
var mapUrl;
var defaultSymbol;
function Init() 
{
    map = new esri.Map("mapDiv");
    mapUrl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Population_World/MapServer";
    //mapUrl = "http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Specialty/ESRI_StatesCitiesRivers_USA/MapServer";
    layer = new esri.layers.ArcGISDynamicMapServiceLayer(mapUrl);
    map.addLayer(layer);        
    defaultSymbol = new esri.symbol.SimpleMarkerSymbol(); // point
    defaultSymbol.setColor(new dojo.Color([255, 0, 0]));

    addPoint(10, 10);
    addText(10, 20, "Text");
}
function addPoint(x, y)
{
    var pt = new esri.geometry.Point(x, y, map.spatialReference);
    var graphic = new esri.Graphic(pt, defaultSymbol);
    map.graphics.add(graphic);
}
function addText(x, y, text) 
{
    var pt = new esri.geometry.Point(x, y, map.spatialReference);

    //Create font
    var font = new esri.symbol.Font();
    font.setSize(18);
    font.setWeight(esri.symbol.Font[WEIGHT_NORMAL]);

    //Create the TextSymbol
    var textSymbol = new esri.symbol.TextSymbol();
    textSymbol.setText(text);
    textSymbol.setColor(new dojo.Color([0, 255, 0]));
    textSymbol.setFont(font);
    textSymbol.setKerning(true);
    var graphic = new esri.Graphic(pt, textSymbol);
    map.graphics.add(graphic);
}

 dojo.addOnLoad(Init);
     </script>  
    </head>
   <body class="tundra">
   <div id="mapDiv" style="width:900px; height:600px; border:1px solid #000;"></div>
   </body>  
   </html>

Answer

Rock picture Rock · Nov 15, 2012

use Tiled Map as base map, then add Dynamic Map layers, change addPoint function: Note: the wkid dependent on your map spatial.

function addPoint(x, y)
{
   var point = new esri.geometry.Point(x, y, new esri.SpatialReference({ wkid: 102113 }));
   var mp = esri.geometry.geographicToWebMercator(point);
   point = new esri.geometry.Point({ "x": mp.x, "y": mp.y, "spatialReference": { "wkid": 102113} });
   var graphic = new esri.Graphic(point, defaultSymbol);
   map.graphics.add(graphic);
}