The third part, Using Events, was published yesterday. The final article in this series will be published tomorrow.
Click here to see the example and then we will discuss the code used to create this simple application. As I mentioned above the data for this application was pulled from an ArcGIS Server map service provided by ESRI, and the graphic point was generated based on an address that was geocoded by the Google Maps API. If you would like to see the code simply right click the page and then select View Page Source.
Step 1: Reference the Google Maps API
Before creating an instance of the Google Maps API geocoding object (GClientGeocoder) you must provide a reference to the API. This is accomplished as follows:
Note: You must have a Google Maps API key to access objects within the API.
Step 2: Create an Instance of GClientGeocoder
The GClientGeocoder class from the Google Maps API can be used to geocode addresses, and an instance of this class is created as follows:
Step 3: Call the getLatLng( ) method on GClientGeocoder
The getLatLng( ) method on GClientGeocoder accepts an address as the first parameter, and the second parameter is a callback function that will be run when data is returned from the method. A point object will be passed into the callback function. If the geocode of an address was successful this point will have geometry (x,y coordinates), and in the event that the object was not geocoded the point object will be null. Notice that in this case we've hard-coded an address that will be input into the getLatLng( ) method. This of course would not be acceptable in a real application, but the point here is simply to show how the objects work.
Step 4: Create ESRI Point, Symbol, and Graphic Objects
Step 5: Add the Graphic to the Display
In the code sample above, the map.graphics.add(pointGraphic) method adds our newly created graphic to the graphic list for the application. Any number of graphics of various types can be placed into this graphic list and each object is drawn on the display.
This article was written by Eric Pimpler at GeoSpatial Training Services. Geospatial Training Services provide a
range of geoweb courses.