Select to view content in your preferred language

'map' is null or undefined

1541
2
10-13-2010 06:58 AM
by Anonymous User
Not applicable
Original User: ereed

I have an application, and I want to be able to show a point, and zoom to that area. I have a function that uses the global variable "map" that is defined in the init procedure. When I call this variable, it gives me the message 'map' is null or undefined.

map.setExtent(newExt);

I have a textbox and button, the user will type in the textbox, and click the button.

<asp:TextBox ID="txtLocation" runat="server" style="left: 800px; position: absolute;width:200px; height:18px; top: 55px" />

<asp:Button ID="cmdAddress" runat="server" Text="Find" OnClick="cmdAddress_Click" style="left: 1110px; position: absolute;width:52px; height:24px; top: 55px"/>

In the  code behind I have a procedure that calls a web service, finds the address, and returns the coordinates. I then call the javascript function.

Code behind:

                Dim sb As New System.Text.StringBuilder()
                sb.Append("<script language='javascript'>")
                sb.Append("addAddressPointToMap(" & dblX & ", " & dblY & ");")
                sb.Append("</script>")

                ScriptManager.RegisterStartupScript(Me, Me.GetType(), "ajax", sb.ToString(), False)

Javascript Fuction:

    function addAddressPointToMap(xloc, yloc) {
        var pt = new esri.geometry.Point(xloc, yloc, new esri.SpatialReference({ wkid: 26985 }));
        var sms = new esri.symbol.SimpleMarkerSymbol().setStyle(
     esri.symbol.SimpleMarkerSymbol.STYLE_SQUARE).setColor(
     new dojo.Color([255, 0, 0, 0.5]));
        var attr = { "XCoord": xloc, "YCoord": yloc, "Address": "XXXXX" };
        var infoTemplate = new esri.InfoTemplate();
        var newExt = new esri.geometry.Extent(xloc - 100, yloc - 100, xloc + 100, yloc + 100, new esri.SpatialReference({ wkid: 26985 }));
        var graphic = new esri.Graphic(pt, sms, attr, infoTemplate);
        map.setExtent(newExt);
        var GraphicsLayer = new esri.layers.GraphicsLayer();
        GraphicsLayer.id = "ADDRESS";
        infoTemplate.setTitle("");
        infoTemplate.setContent("<b>Address: </b>" + xloc);
        GraphicsLayer.add(graphic);
        graphic = null;
        sms = null;
        map.addLayer(GraphicsLayer);
        map.centerAt(pt);
    }    

map here is undefined. It is used in other functions without error.

Any help would be greatly appreciated.
0 Kudos
2 Replies
by Anonymous User
Not applicable
Original User: Georgie.Cassar

Howdy
Did you ever solve tyhis issue because I am trying to do the same thing although I'm just trying to add the point to the map.graphics graphiclayer.  ie:  map.graphics.add(graphic)
The graphic won't draw for me.
I tried to see how many items are in the graphics array with
var num1 = map.graphics.length;
alert("num graphics ", num1);
But it tells me the map.graphics is undefined.
0 Kudos
MatthewLawton
Deactivated User
Not sure if this will help, but this is the function that I use to symbolize and zoom to a point from a result set:

function zoomToPoint(results) {
  var selFeature = results.features[0];
  var curExtent = map.extent;
  var selExtent = new esri.geometry.Extent(selFeature.geometry.x,selFeature.geometry.y,selFeature.geometry.x,selFeature.geometry.y);
  var symbol = new esri.symbol.PictureMarkerSymbol('/files/graphics/flashing_dot.gif',30,30);
  var graphic = results.features[0];
  totalExtent = selExtent.union(curExtent);
  graphic.setSymbol(symbol);
  map.graphics.add(graphic);
  map.setExtent(totalExtent.expand(1.75));
}

I am using a picture symbol, but you could easily change it to your own symbology. Good luck!
0 Kudos