In this blog, we will see how to embed a Google Map in SAP Analytics Cloud Application. This blog will primarily focus on pinning a selected member (eg. a store) in Google Map with the help of R widget. Also, we will learn how to retrieve the dimensions that are not displayed in the chart.
The scenario is to pin the selected store from the chart on to a Google Map. You can see the embedded Google Map in action below.
Defining Chart Structure
Let us consider an example of Sales data. Include Sales (SALE_DOLLARS) measure and Store (STORENUMBER) dimension in a chart. The Store is set to display Description.
Retrieving and passing the selected store to R Visualization
Add the following script to fetch the user selected store. Since you cannot directly fetch the latitude and longitude information of the selected store ID, the selected member is passed to R Visualization which can be used to extract latitude and longitude.
The ID of the user selected store is stored in a global variable store_id. Then the store ID is assigned to the R variable StoreNum using the setNumber() API. Another global variable flag is used to ensure the event onResultChange which is executed only after user selection. This will be explained later.
The global variable store_id is defaulted to zero and passed to R Visualization in the onInitialization event so that the application runs without any error at the startup. The definition of global variable store_id is also mentioned in the snapshot below.
Initializing R Visualization Structure
Select the necessary fields that are required (i.e) Latitude, Longitude, Store, and Sales. You can visit this blog to get a detailed perspective on the R data frame and how to set it up in SAP Analytics Cloud. The dimension Store is set to display ID in the R data frame as shown in the below snapshot.
As this R widget doesn’t visualize anything, you can hide it by disabling the option Show this item at view time from the Styling panel.
Extracting latitude and longitude using R script
Add the following R script to extract the latitude and longitude of the selected store.
Here the script filters to the StoreNum input parameter. Then corresponding latitude and longitude values are extracted from the data frame and stored in R variables LAT and LONG. These R variables can later be retrieved to embed Google Map.
Embedding Google Map
Add a WebPage widget to the canvas which can be used to show the Google Map. Then add the following script in the onResultChange event of R Visualization to pass the latitude and longitude through URL.
The global variable flag confirms the script is executed only after user selection. Then the R variable values (latitude and longitude) are assigned to local variables. These local variables are then appended to the Google Map URL (https://maps.google.com/maps?q=) and set to the Webpage widget.
Now save and run the application. When you select a store, the store ID is passed to the R Visualization from the onSelect event. Once the latitude and longitude are extracted using R script, the onResultChange event is executed which sets the proper Google Map URL to the Webpage widget.
You can also plot an area or city in Google Maps (or others) applying the same logic. This blog is limited to pinning one coordinate at a time based on user selection. You can also expand the functionality to multi-select and pin multiple locations.
Reach out to us here today if you are interested in evaluating if SAP Analytics Cloud is right for you.