Difference between revisions of "OnDraw Google map"
Line 1: | Line 1: | ||
+ | [[Category:Google Maps from OnGraphDraw library functions]] | ||
''Requires release [[Analytica 5.2]] and [[Analytica Enterprise]] or better'' | ''Requires release [[Analytica 5.2]] and [[Analytica Enterprise]] or better'' | ||
Revision as of 20:21, 19 March 2019
Requires release Analytica 5.2 and Analytica Enterprise or better
Use this function to draw a google map under your plotted data to plot data on a map. You'll need to have the longitude and latitude of each or your data points.

OnDraw_GoogleMap( canv, info, roles )
This function is included in the OnGraphDraw Google Map library. Use it to display a map under your data in a result plot, where the X-axis of your plot is longitude and the y-axis of your plot is latitude.
Setup before using in your model
- Select Add Library... on the File menu, click on the "
Google Maps from OnGraphDraw.ana
" library file. You can embed or link, but we recommend linking it. - Go to the Attributes dialog by selecting Attributes... from the Object menu. With Class=Variables, make sure there is a check before the OnGraphDraw attribute.
How to structure your data
You'll need to create a plot where the x-axis is longitude (in degrees) and the y-axis is latitude. There are many ways you might accomplish this, depending on how your data is already structured. Here are two simple ways.
(a) Create separate variables for Latitude, Longitude, and the data you want to depict (e.g., by using color, symbol shape or symbol size). These should all share a common index (your "point" index).
Press the [XY] button to add in comparison variables. Add in Latitude, Longitude, and any others to be depicted.
The example model "CSV read and Google Maps plot.ana
" uses this data organization.
(b) Organize your data in a 2-D table, where one index corresponds to the individual points, and the other index contains a relative small number of labels that includes 'Latitude'
, 'Longitude'
, and any other data you want to depict.
Press the [XY] button and check "Use comparison index". Select the second index as your comparison index.
Graph setup
Bring up the Graph Setup Dialog and select the desired line type. The most common use case is a symbol-only plot, with variable symbol size selected. Use this configuration when you want symbol size to vary to depict a continuous quantity such as generation capacity in the example plot above.
On the Style tab, it looks best to set Grid and Frame to None. This is optional.
Apply Graph Setup changes, the select your main result for Symbol size, and set Horizontal to longitude and Vertical to Latitude.
Set the OnGraphDraw attribute
Now you are ready to use the function! In the Object window for your result variable, find the OnGraphDraw variable. Place a check on "before drawing'" and uncheck the others, and set the OnGraphDraw expression to:
- If phase=2 Then
- roleChanges := OnDraw_Google_map(canv, info, roles)
The If phase=2 Then
part is unnecessary if you've only checked before drawing. But this keeps it flexible in event that you later combine this with other OnGraphDraw functions that fire in other drawing phases.
Suppressing when pivot is wrong
You'll get maps of random locations in the world if your pivot is something other than latitude on the Y-axis and longitude on the X-axis. Since your model end-user might pivot the graph, you might want to configure it to not show a map when the pivot is something else.
The function itself doesn't know where the latitude and longitude dimensions are coming from, so it doesn't do this test. It is up to you to test the pivot. For example, a common way to do it is to have separate variables for Latitude and Longitude in your model. The test in OnGraphDraw would look like:
- Local ySrc := roles[ GraphingRole='Y axis',GraphFillerInfo='Source'];
- Local xSrc := roles[ GraphingRole='X axis',GraphFillerInfo='Source'];
-
- [If]] phase=2 and ySrc=Handle(Latitude) and xSrc=Handle(Longitude) Then
- roleChanges := OnDraw_Google_map(canv,info,roles)
Your API Key
The very first time you display a map result, you'll be asked for a Google API Key. You need to get this from Google and enter it. The popups provide instructions for how to obtain it.
Once you obtain your own API key and enter it, you'll never be asked for it again, with this model or other models.
If you ever need to reset it, view the main diagram for the Google Maps from OnGraphDraw library. You'll find a button there to reset it.
An example model
An example model "CSV read and Google Maps plot.ana"
, which is included with Analytica in the Data Analysis
folder, demonstrates the use of this library function.
After loading the model, view the result graph for Generation capacity. (The screen shot of this appears at the top of this page). Use the Select fuel type(s) multi-choice to explore other power plant types in California.
Enable comment auto-refresher