MBS FileMaker Advent calendar - Door 11 - Know Your Way
Day 11 - Know Your Way |
Today we are already on day 11 of our advent calendar. Our little monkey would like to take a look at the region where the recipients live. The best way to do this is to take a look at the map. MBS has the MapView functions for Mac and iOS to display maps from Apple Maps.
To do this, we mark the position where we want to display our map with a rectangle. We name this rectangle Map. Now we want to create our map. We can either use the MapView.CreateWithSize function to place the map with a specific size at specific coordinates on the screen or we can use the MapView.CreateWithControl function and place our map on a control that we have previously placed on your layout. We want to place our map on our rectangle with the name Map. So we use the second option. In the function, we first specify the window in which we want to create this map. As we want to place the button in the same window in which we want to display the map and the window must be the frontmost one, we can enter a 0 for the window index, followed by the name of the control on which we want to place the map. Optionally, we can now enter x and y values by an amount that we would like to move the map in relation to the control. We don't want to do this, so we will leave it out.
Set Variable [ $$Mapview ; Value: MBS("MapView.CreateWithControl"; 0; "Map") ]
Now that we have a map, we want to display the customer's address on the map. To do this, we use the MapView.ShowAddress function to set the focus of our map on the address. In this function, we first enter our Mapview reference and then our address, which we have put together from the street, city, state and country, separated by commas. But we don't just want to display the address, we also want to mark it with a picture of our monkey. We already have this image in the global variable $$Overlay_Image. To mark the point of the address on the map, we use the function MapView.AddPoint. We pass this function a JSON with the information about this point. We have already seen how a JSON is structured on day 8. Now we create new information in an empty object with the functions JSON.AddStringToObject and JSON.AddNumberToObject. In these two functions, we first specify the JSON object to which we want to add the information and then the key and the value. In our case, we want to specify the address, the title of the annotation, the image of the annotation and the size of this image in the JSON. For our title, we take the first and last name. The title is displayed next to the icon when we move the mouse over it. The part of the script then looks like this:
Set Variable [ $r ; Value: MBS("MapView.ShowAddress"; $$Mapview; $address) ] Set Variable [ $PointJSON ; Value: "{}" ] Set Variable [ $PointJSON ; Value: MBS("JSON.AddStringToObject"; $PointJSON; "address" ; $address ) ] Set Variable [ $PointJSON ; Value: MBS("JSON.AddStringToObject"; $PointJSON; "title" ; Giftee::first_name & " " & Giftee::last_name ) ] Set Variable [ $PointJSON ; Value: MBS("JSON.AddStringToObject"; $PointJSON; "image" ; Base64Encode ( $$Overlay_Image ) ) ] Set Variable [ $PointJSON ; Value: MBS("JSON.AddNumberToObject"; $PointJSON; "imageWidth"; 40) ] Set Variable [ $PointJSON ; Value: MBS("JSON.AddNumberToObject"; $PointJSON; "imageHeight"; 40) ] Set Variable [ $r ; Value: MBS( "MapView.AddPoint"; $$Mapview; $PointJSON ) ]
Of course, we would also like to have the option of hiding this card again. As already described in other doors for references, we have the option here of releasing a single reference or all at the same time. Again, we choose MapView.ReleaseAll because we don't need to know the exact reference. We also place it at the beginning of the script that creates a map, before the Create function, so that any legacy data is removed.
Now we can make settings for the display of the map. This means we can show and hide certain things on the map. We can show and hide traffic, buildings, our own location and points of interest on the map.The functions MapView.SetShowsTraffic, MapView.SetShowsBuildings, MapView.SetShowsUserLocation and MapView.SetShowsPointsOfInterest are available for this purpose.But controls can also be displayed. For example, we can display the compass, the scale and the zoom buttons. For this we use the functions MapView.SetShowsCompass, MapView.SetShowsScale and MapView.SetShowsZoomControls. To be able to make these settings in a space-saving way, we use a popup button. On this popup we place for each of these settings the name and to the right and left of the name two buttons that turn this option on and off. For each of these options, we also create a global field in our database in the Data table. We can define whether a field is global in the field settings. This field has the same values for each data record and can be seen from anywhere in the solution. The values in the fields are specific to the user and session and have no influence on other users.
We also create a script for each setting. First we get the script parameter that we pass directly with the button settings and save it in the $param variable. Then we call the function to set the property and enter the reference of the card and the read value $param. Then we set the global field that matches the property with the value in $param. This is what the script for points of interest looks like, for example:
Set Variable [ $param ; Value: Get(ScriptParameter) ] Set Variable [ $r ; Value: MBS("MapView.SetShowsPointsOfInterest"; $$Mapview; $param) ] Set Field [ Data::POI ; $param ]
These scripts are then passed to the buttons next to the respective properties, with the difference that the On buttons in the dialog of the button are given a 1 as a parameter to activate the properties and the Off buttons a 0 to deactivate the properties.
So that we know what is currently switched on and what is switched off, we now have to color the buttons using conditional formatting. To do this, we take advantage of the fact that we set the global fields when making the settings. Once these are set, we look at the conditional formatting and ask whether they are 1 for the On buttons and 0 for the Off buttons. This is what the query looks like for the On button for Points of Interest: Data::POI=1
If this condition is correct, the button should turn light blue.
We repeat this with each button with the corresponding field and value.
We have now also integrated these functions into our solution. I hope you have fun with it. See you tomorrow
10 👈 | 11 of 24 | 👉 12 |