Skill Level: Intermediate


SetupFor further information on IBM Bluemix geospatial service, see https://www.ng.bluemix.net/docs/#services/geospatial/index.html Add the Geospatial service to your Node.js application.To set up the Geospatial service follow the steps at https://www.ng.bluemix.net/docs/#services/geospatial/index.htmlGo to the IBM Watson IoT Platform DashboardGo to your organizationClick on “API Keys"Click on “New API Key" to generate a new API key and token.Save these values.Geospatial […]


Software Requirements:

  • Bluemix Subscription
  • IBM Watson IoT Platform Service in Bluemix
  • IoT enabled device that sends GPS data and connected with the Watson IoT.
  • Node.js Runtime application deployed on Bluemix and bound with the Watson IoT service.

Service fee may apply. Estimated Cost: Monthly Costs vary


More than 256MB RAM might be required to deploy this application. If the memory usage exceeds 256MB in the free trial accounts (Bluemix Trial Account and Standard Account), the application might not work as expected.

We suggest that you upgrade to Pay-as-You-Go or Subscription account to enjoy the full-range Bluemix services.


Deploy the Setup:

Click on the Create Toolchain button provided below, to quickly deploy the Watson IoT Platform (WIoTP) service and Node.js application, provided as part of the WIoTP -  Node-RED Bluemix starter app. Post successful deployment, you shall have both the above mentioned services, up & running on Bluemix along with a Cloudant NoSQL DB at your disposal.


Note: If you are a User using the United Kingdom Region in your Bluemix environment, then please make use of the steps mentioned in the IoT Recipe Deploy Internet of Things Platform Starter service on Bluemix to deploy the setup. Alternatively, you can also try using the Deploy to Bluemix button, to deploy the setup under your United Kingdom Region, provided your Jazzhub account is validated. Users of US South Region can ignore this step.


Open the Bluemix Dashboard and click on the Application service that you have currently deployed. You should see the Watson IoT Platform listed under the Connections. Click on the WIoTP service and choose the click on Launch button to launch the WIoTP Dashboard.


  1. Setup

    For further information on IBM Bluemix geospatial service, see https://www.ng.bluemix.net/docs/#services/geospatial/index.html

    1. Add the Geospatial service to your Node.js application.
    2. To set up the Geospatial service follow the steps at https://www.ng.bluemix.net/docs/#services/geospatial/index.html
    3. Refer to Generate API Keys Section to generate API Key and to use in this recipe.

    For detailed step by step setup procedure, refer to article link – Build a connected-car IoT app with Geospatial Analytics

  2. Geospatial service

    The Geospatial service requires an MQTT broker to provide the device messages. In this tutorial we will use the IBM Watson IoT Platform service as the MQTT broker.

    So lets assume for this tutorial, we want to monitor all devices of deviceType “vehicle“. So each device of this type are sending the GPS data(lat and long).

    We need to use the following while setting up the service

    var your_org = "whegdt"; // replace it with your orgIdjsonObject = JSON.stringify({"mqtt_client_id_input" : "a:"+ your_org + ":geoInput","mqtt_client_id_notify" : "a:"+ your_org + ":geoNotify","mqtt_uid" : api_key,"mqtt_pw" : " auth_token ","mqtt_uri" :your_org + ".messaging.internetofthings.ibmcloud.com:1883","mqtt_input_topics" : " iot-2/type/vehicle/id/+/evt/gps/fmt/json","mqtt_notify_topic" : " iot-2/type/alerts/id/geospatialAlerts/cmd/geoAlert/fmt/json ","device_id_attr_name" : "deviceId","latitude_attr_name" : "lat","longitude_attr_name" : "long"});


    api_key & auth_token API-Key that you generated for your organization

    uri Your organization MQTT hostname

    mqtt_input_topics Topic used by the geospatial service to get the current location of the vehicles.

    mqtt_notify_topic Topic used by geospatial service to send back alerts to the device.

    device_id_attr_name The device ID attribute in the JSON message

    latitude_attr_name The latitude attribute in the JSON message.

    longitude_attr_name The longitude attribute in the JSON message.

  3. Define the geo-fence

    Use the geospatial ReST APIs – https://www.ng.bluemix.net/docs/?cm_mmc=developerWorks-_-dWdevcenter-_-iot-_-lp#services/geospatial/index.html#restapi


    Define a geo-fence region using the “addRegion” ReST operation.

    jsonObject=JSON.stringify({    "regions": [        {            "region_type": "regular",            "name": "Coffee Shop",            "notifyOnExit": "false",            "center_latitude": "12.9521265",            "center_longitude": "77.6439412",            "number_of_sides": "10",            "distance_to_vertices": "150"        }    ]}) 


    You can also add multiple regions for geo-fencing.

  4. Alerts

    Now when the vehicle enters the geo-fence area, a notification will be sent to the device “geospatialAlerts” which is listening to command “geoAlert”.


    Here is a sample snippet of the device code.

    var deviceId = "geospatialAlert";var clientId = "d:" + your_org + ":" + "alerts" + ":" + deviceId ;var subscribeTopic = "iot-2/cmd/geoAlert/fmt/json";deviceClient.subscribe(subscribeTopic);

     This device will receive all notifications from the geospatial service.

  5. Conclusion and some of other analytics related IoT Recipes

    In this recipe, we have shown how to use Bluemix Geospatial Analytics service to track when devices enter or leave defined regions, or geofences. We have good number of analytic recipes integrated with using IoT devices in our recipe pool.

    An article on building an IoT Application using Geospatial Service for Connected Car describes in deep how to Build a connected-car IoT app with Geospatial Analytics.

    Here, we are giving links to some of related IoT Recipes on Analytics  for the reference:

Expected Outcome

You will be notified when your device enters and exits the defined geo-fence.

Join The Discussion