Search code examples
c#javascriptmapsarcgis

Using c# with JavaScript in ArcGIS


I am new to ArcGIS. I am using an API to bookmark locations on a map. When a specific location is clicked (eg. Galway bay), the map will then zoom in to that location.

                      <script>
    dojo.require("dijit.layout.BorderContainer");
    dojo.require("dijit.layout.ContentPane");
    dojo.require("dijit.form.DropDownButton");
    dojo.require("esri.map");
    dojo.require("esri.dijit.Bookmarks");
    dojo.require("esri/dijit/HomeButton");
    dojo.require("dojo/domReady!");

    var map, bookmarks, home;
    function init() {
        map = new esri.Map("map", {
            basemap: "oceans",
            center: [-7.5, 53],
            zoom: 6
        });

The list of location, which a user can select is generated using JavaScript like the example below. You use spatial reference to locate a point on a map and then give it a name.

        // Bookmarks can be specified as an array of objects with the structure:
        // { extent: <esri.geometry.Extent>, name: <some string> }
        var bookmarks_list = [{
            "extent": {
                "spatialReference": {
                    "wkid": 102100
                },
                "xmin": -1882000,
                "ymin": 6638000,
                "xmax": -316000,
                "ymax": 7583000
            },
            "name": "Full View....."
        }, {
            "extent": {
                "spatialReference": {
                    "wkid": 102100
                },
                "xmin": -1020761,
                "ymin": 7009798,
                "xmax": -996800,
                "ymax": 7048100
            },
            "name": "Galway Bay"
        }, {
            "extent": {
                "spatialReference": {
                    "wkid": 102100
                },
                "xmin": -939400,
                "ymin": 6756000,
                "xmax": -890400,
                "ymax": 6785500
            },
            "name": "Cork Harbour"
        }, {
            "extent": {
                "spatialReference": {
                    "wkid": 102100
                },
                "xmin": -1123000,
                "ymin": 6839100,
                "xmax": -1074100,
                "ymax": 6868600
            },
            "name": "Tralee Bay"
        } ];


        // Create the bookmark widget
        bookmarks = new esri.dijit.Bookmarks({
            map: map,
            bookmarks: bookmarks_list
        }, dojo.byId('bookmarks'));


    //show map on load 
    dojo.ready(init);
    home.startup();


</script>

My question is, how do I get the JavaScript "name" value e.g. Galway Bay and pass it to a c# function ? How do you related c# and JavaScript? I am new to this so I would appreciated any advice thanks


Solution

  • As you've mentioned C# and JavaScript my guess is you're hosting your application in a .Net environment. However, the JavaScript API runs client side only so I'm not sure what you want to use C# to actually do? If it is that you want the server side to do something in response to something happening on the client, then you'd probably want to call a web server running on your server and supply it any client side properties. I use the .Net Web API for this.

    You probably want to read up a bit more on:

    1. the ESRI JavaScript API (https://developers.arcgis.com/javascript/),
    2. the dojo request module for calling a webservice (http://dojotoolkit.org/documentation/tutorials/1.10/ajax/)
    3. the ASP.Net Web API (http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api).

    One other thing: I note you are using older Dojo syntax (pre v1.7) which is an easy mistake to make given that ESRI and even Dojo themselves haven't updated all their tutorials to the 1.7+ syntax. But if you are starting a new project I would make sense to use the new syntax from the start. The dojo site has another tutorial about the differences (same link as above, just go up a level).