Omnis Technical Note TNJC0011May 2021
Using SVG icons for Map Markers
for Omnis Studio 10.2 or above
By Omnis Engineering
The JS Map control allows you to place a Google® Map onto a remote form to be used in your web and mobile applications. A map is a highly interactive UI element and can present location-based data in a very compact format – and as well as showing data, a map allows the end user to interact by clicking on markers and other controls adding to the richness of your app experience.
In the JS Map control you can set a location and the zoom level when the map is initialized, you can control the map type (map view or satellite) and the controls that are displayed, and you can setup default or custom map markers – these are all shown in the sample app in the Hub in the Studio Browser. The map markers can be one of a number of standard markers supplied by Google (the default is a map pin icon), or a custom icon from an iconset, and from Studio 10.2 you can use SVG image icons to set custom markers. We have used some Google ‘material’ icons (flower, ferris wheel & museum were downloaded from the Google icons website, and colors changed) for three custom markers on the following map:
The markers on a map are specified by assigning a list to the $mapmarkers property – the mapmarkers list can have up to six columns, as the following code shows:
The LatLong column is the Latitude:Longitude location for a marker, a text Title and Tag can be specified for each marker, plus you can add HTML content for the popup – the HTML can contain any standard HTML including heading tags, font tags, and links, but must be contained inside a <div> container. The HTML could be static code or built up dynamically from data in your code using a Text: command block and assigned to the marker HTML column.
The marker icon column is a URL to a PNG or SVG image stored in an iconset (if this column is blank or missing the default map pin icon is used). You can set the icon URL using the iconurl() function, which from Studio 10.2 onwards can take a character string to select an SVG image file (in previous versions you had to use an integer ID for the icon). You can specify a different image for each marker in the marker list. (If you leave the icon column blank, you can specify one of Google’s custom markers in the sixth column – this is described in the online docs.)
The following code creates markers for three famous tourist attractions in London – in this case, the British Museum, London Eye, and Hyde Park. The icons for the markers are SVG images based on Google ‘material’ icons and contained in a ‘maps’ iconset (this name must be added to the $iconsets library property); note you can use any of the SVG icons contained in the 'material' iconset already provided in Omnis. The iconurl() function is used to return a URL of the specified image, e.g. iconurl(‘museum’), so it can be rendered on the map inside the remote form in the browser.
Setting up the demo library
We have provided the above code in a library and the iconset needed for the markers: mapmarkers.zip
When you have setup the API key and the iconset, re-open the library, and the map remote form will be opened in your default web browser. The map will be opened and centered on London and the three custom map markers will be displayed (flower, ferris wheel & museum). You can click on a map marker and the HTML content is displayed in the popup, e.g. the London Eye:
If the map is not displayed, check you have entered the API key in the $apikey property for the map, and if the map is greyed out check that the APIs are activated in your Google Cloud account. If the map markers are not displayed, check you placed the ‘maps’ iconset in the Omnis iconsets folder; you may need to close and restart Omnis to load the new iconset.
If you use SVG icons and you deploy your application, you need to remember to copy all your own icons (all iconset folders) to the Omnis Server tree, which you can manage in Studio 10.2 using the Deployment tool.
For more information about map markers or using the JS Map control, look at the Online docs: https://omnis.net/developers/resources/onlinedocs/WebDev/03jscomps.html#map-markers