Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArcGIS simple example dynamically rendering a marker

I am having a hard time trying to add a simple clickable marker to an ArcGIS, map purely using JavaScript. All of the ArcGIS Samples seem to get their marker and related popup information from the server. How can I achieve the same result with ArcGIS as this Google Maps sample code below?

<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<div id="map" style="width: 500px; height: 500px;"></div>
<script type="text/javascript">
    window.onload = function() {
        var myOptions = {
            zoom: 2,
            center: new google.maps.LatLng(40, -75),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        var map = new google.maps.Map(document.getElementById("map"), myOptions);

        var icon = new google.maps.MarkerImage("http://cinnamonthoughts.org/wp-content/uploads/2010/01/Custom-Marker-Avatar.png");

        var markerOptions = {
            icon: icon,
            map: map,
            position: new google.maps.LatLng(37.7699298, -122.4469157),
        };

        var marker = new google.maps.Marker(markerOptions);

        google.maps.event.addListener(marker, 'click', function() {
            var infoWindow = new google.maps.InfoWindow();
            infoWindow.setContent("hello world");
            infoWindow.open(map, marker);
        });
    };
</script>
like image 919
Langdon Avatar asked Mar 12 '12 17:03

Langdon


1 Answers

Try this:

  1. Create a Point for you longitude and latitude
  2. Convert the point to Web Mercator spatial reference, if it's necessary
  3. Create a PictureMarkerSymbol for you custom picture marker
  4. Create a Graphic using the point and the symbol
  5. Create a GraphicsLayer
  6. Add the graphic to the graphic layer
  7. Add the graphic layer to your map
  8. Add a custom onClick event listener to your layer

Some equivalent code:

var point = new esri.geometry.Point(longitude, latitude);
point = esri.geometry.geographicToWebMercator(point);
var symbol = new esri.symbol.PictureMarkerSymbol("marker.png", 32, 32);
var graphic = new esri.Graphic(point, symbol);
var layer = new esri.layers.GraphicsLayer();
layer.add(graphic);
map.addLayer(layer);
dojo.connect(layer, "onClick", onClick);

On the event listener you can open a custom infoWindow or whatever you like:

function onClick(event) {
    map.infoWindow(...)
...

Change "marker.png" and 32x32 to use your custom marker image and dimensions.

like image 160
Juan Mellado Avatar answered Nov 14 '22 19:11

Juan Mellado