Integrating the Google Maps API Geocoder with ArcGIS Server
Click here to see the example and then we’ll 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’d 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 wouldn’t 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.
If you’d like more information on how to create ArcGIS Server and Google Maps applications with their respective APIs you might want to consider our upcoming course which still has 4 seats available. Contact eric at geospatialtraining.com for more information.