Categories

Add your own widgets to your forms, Example 1: Google Maps API

Here is an explanation of how to add your own custom widgets to the applications generated by Bonita Open Solution. This is something a lot of Bonita Open Solution users have been asking for on our forum, and here is one solution!

In this mini-tutorial, I’ll use Bonita Open Solution 5.4 with its new HTML widget in the form designer, but the same method can be applied to older releases of BOS. You’ll simply have to use a Message widget with the “Allow HTML” flag set to true.

Example 1: Using the Google Maps API

Google provides a great JavaScript API for almost all its products. Let’s use the Google Maps API for the interaction between the Forms application of your process and a Google Map. The user will see – instead of 2 text widgets to input longitude and latitude – a Google Map with a marker to drag and drop in order to indicate a location. Let’s start with a simple process with 2 text variables: latitude and longitude.

The process with its data

Your widgets will make your pages nicer!

If you really don’t want to do it yourself, you can find this example process in the community contributions.

Step 1: Reference the API in BonitaEnvironment.html

In order to leverage the API, you need to define a reference to it in your application. In my opinion, that’s the trickiest part, although it’s still quite easy, and we’ll probably make this step even easier to do in a future release.

You need to add the following lines to the header of the BonitaApplication.html file:

<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript">
</script>

Then create a new process and run it, get the source of form page, this is BonitaApplication.html. Add the necessary lines. The result should look like this:

<!--             -->
<!-- The favicon -->
<!--             -->

<script src="application.nocache.js" type="text/javascript"></script>
 
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript">
 </script>

<!-- OPTIONAL: include this if you want history support -->

<!-- loading displayed while GWT is not yet ready -->
<div id="loading">
<div id="bottom-right-shadow">
<div id="loading-content"><img src="images/large-loading.gif" alt="" /><!-- Please wait while loading... --></div>
</div>
</div>

Then, add it to your Forms resources so that the default BonitaApplication.html is replaced by the one you just tweaked:

Add BonitaApplication.html to your application resources

Step 2: Model your form

Your form will be very simple to model. It is simply made of 2 hidden fields: latitude and longitude. These widgets add 2 input fields you’ll use to store and save the location marked in the Google Map. Since a Google Map is not a basic HTML input, we cannot directly retrieve its values to store them later. Then we’ll make the Google Map widget store the data we want to use later in those fields.

Then add your HTML widget (or Message widget allowing HTML in older versions). Put in the following code as the initial value:

<script type="text/javascript">// <![CDATA[
 var map;

   var myLatlng = new google.maps.LatLng(45.18409395583682,5.703503018447886);
   var myOptions = {
     zoom: 4,
     center: myLatlng,
     mapTypeId: google.maps.MapTypeId.HYBRID
   }
   map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

   var marker = new google.maps.Marker({
       position: myLatlng,
       map: map,
       title:"Where are you?",
    draggable: true
   });
   google.maps.event.addListener(marker, 'dragend', function() {
    var point = marker.getPosition();
    // Update fields.
    document.getElementById("latitude").getElementsByTagName("input")[0].value = point.lat();
    document.getElementById("longitude").getElementsByTagName("input")[0].value = point.lng();
   });
// ]]></script>

This script adds a map to your page and updates the hidden fields whenever you move the marker. As you can see, this is simply HTML, JavaScript and Google Maps API, nothing more!

Form overview

Step 3: Try and enjoy!

Just hit the “Run” button, and take a look at your application. It works! Wasn’t that easy?

A very cool integration!

Tags: , , , ,

4 Responses (and one trackback)

  1. #1 by Max on October 18th, 2011

    Quote

    looks good. What about if my want to get a polygon instead of a point on Google Map? Is there any example? Also possibility of passing location parameter to the map API to return the map at the right location.

    Cheers
    max

  2. #2 by Casper on November 3rd, 2011

    Quote

    I find the idea of this working great yet as I have seen from other posts, others are also struggling to get this to work…

    I have followed this step by step as well as downloaded the .bar contribution.

    I have ensured all dependencies and resources are available.

    The javascript for some reason just does not execute irrespective of the browser being used.

    All i get is a blank screen with a button…very frustrating. Yet when I take any other GoogleMap API and put that in a html page, the results always return without issues…please…need help here ASAP.

  3. #3 by Casper on November 3rd, 2011

    Quote

    I have taken your scripts and manually inserted the script into the BonitaApplication.html file and I know now that it works…no doubt. The issue still is that the Bonitasoft Stylesheet does not execute the scritpt at all..now I have the map on the screen yet not within the area that you get it which is rather frustrating. Do you have any pointers?

  4. #4 by Luiz Paulo Coelho Ferreira on January 27th, 2012

    Quote

    I downloaded the process and it didn’t worked( using BP 5.6 on Windows 7.), do you have any ideia why it doen’t work? Looks like i can’t replace the BonitaApplication.html for the default one.