GoogleMaps 3.0.0

Published by Soliant Consulting on April 13, 2019
Updated on April 3, 2020
Tags: Map
Download Send To Carafe
Download Send To Carafe
Created By: Jeremiah Small

Windows Tested: Yes
Offline Compatible: No

Description:

This bundle requires a valid Google Maps API key. See the reference links for more information.


To integrate this bundle in your own solution, you will need to build an array of markers. The following is and example of a single marker object.


{
"id": 13,
"title": "Encyclopedia Circle I",
"label": "Commercial",
"color": "blue",
"position": { "lat": 37.516203, "lng": -121.984664 }
}


You can load a marker object like that from a record in FileMaker with an expression like this


JSONSetElement ( $marker
; [ "id" ; Asset::AssetID ; JSONNumber ]
; [ "position.lat" ; Asset::Latitude ; JSONNumber ]
; [ "position.lng" ; Asset::Longitude ; JSONNumber ]
; [ "title" ; Asset::Title ; JSONString ]
; [ "label" ; Asset::Label ; JSONString ]
; [ "color" ; Asset::HexColor ; JSONString ]
)


With a subscript such as "Asset To Marker JSON" returning a single record in the expected format, you can create a "Map Loader" script which loops over a found set, and assembles your markers. Here is an example "Map Loader" script.


# ====================================================================================================
# Map Loader
# Assumes Asset context
# Loads current Asset found set as a JSON markers collection
#====================================================================================================
If [ Get ( LayoutTableName ) <> "Asset" ]
Exit Script [ Result: "Wrong context" ]
End If
Go to Layout [ "Asset Loading" (Asset) ]

Set Variable [ $i; Value:0 ]
Set Variable [ $markers; Value:"[]" ]
Go to Record/Request/Page [ First ]
Loop
Perform Script [ "Asset To Marker JSON" ]
Set Variable [ $marker; Value:Get ( ScriptResult ) ]
If [ JSONGetElement ( $marker ; "position.lat" ) <> 0 and JSONGetElement ( $marker ; "position.lng" ) <> 0 ]
Set Variable [ $markers; Value:JSONSetElement ( $markers ; $i ; Get ( ScriptResult ) ; JSONObject ) ]
Set Variable [ $i; Value:$i+1 ]
End If
Go to Record/Request/Page [ Next; Exit after last ]
End Loop

Set Variable [ $parameter; Value:
JSONSetElement( $parameter
; [ "googleApiKey" ; "yourApiKey" ; JSONString ]
; [ "minimumClusterSize" ; 2 ; JSONString ]
; [ "mapData" ; $markers ; JSONArray ]
; [ "fileName" ; Get ( FileName ) ; JSONString ]
; [ "scriptName" ; "Handle Map Pin Click" ; JSONString ]
)
]
Go to Layout [ "Map" (Asset) ]
Perform Script [ "Carafe GoogleMaps v3.0.0 Loader"; Parameter: $parameter ]


As you can see in the example "Map Loader" script above, the bundle parameter expects a "fileName" and "scriptName". The bundle will use these to initialize with a click handler. When the user clicks a marker on the map, it will call this script with the marker id as a script parameter, allowing you to respond to the click in any manner you like. For example, you could open a card window and display details about the record the marker represents.


Tip: During development and testing, you can register a project for testing purposes in the Google Cloud Platform Console and use a generic, unrestricted API key, but when you are ready to move your solution into production, you should register a separate Google API project for production and consider securing that according to Google's documented best practices.


This GoogleMaps bundle includes the Marker Cluster extension in the default MetaData. You can use additional extensions by adding them to the metaData.cdns.js array in the data JSON. NOTE: You must update the config with your own googleApiKey value before using this bundle.
References:

0 Comments

Leave a Comment