Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

HERE Maps API - Coroutines

From Wiki
Jump to: navigation, search

This article demonstrates the use of HERE Maps API Coroutine feature.

Article Metadata
Code Example
Source file: coroutine.html
Tested with
Devices(s): Internet Explorer, Google Chrome, Firefox, Opera
Platform(s): Web browser
Dependencies: HERE Maps 2.5.3
Keywords: HERE Maps, JavaScript, coroutines
Created: Maveric (28 Jun 2011)
Updated: avnee.nathani (31 Dec 2011)
Last edited: jasfox (20 Dec 2013)

Introduction and Prerequisites

Firstly, please read the complete section of Coroutines at the HERE Maps API reference. It is explained there in detail, and here is the direct link:

The example below is using the same concept as in the How to pan the map example.

As the Coroutine built in serves a similar function to the JavaScript setTimeout() and setInterval(), you can also utilize them in same type of use cases. The main idea behind the Coroutine implementation is to serve as a point of saving some CPU time cycles, while performing multiple tasks during the same mapping program - threading.

Because there is a lot to this subject, it is highly recommended that you read the related API chapter.

Example code

This example will loop from 1 to 500 with 10ms delay. The run is displayed on screen, and after the set time period of Coroutine operations, the map will pan to a new position and the Coroutines get terminated. Remember to add in your own AppId and Token.

function initialiseCoroutines(map){
// Define the 1st coroutine - increase the value of i by one every 10 milliseconds,
// until we reach a 500. Watch the screen output for the numbers.
this.iterationCoroutine = nokia.maps.util.Coroutine.create(
"iterator", function( scope, context ) {
if ( i <= 500 ) {
$("#iterator").text("Wait until 500 to see the map move! : "+i+"/500");
return nokia.maps.util.Coroutine.sleep(10);
// pan the map to the new position after the loop is over, 0, 400, 400);
// Define the 2nd coroutine display iteration in console.log every 100 miliseconds
this.loggingCoroutine = nokia.maps.util.Coroutine.create(
"printing", function( scope, context ) {
$("#printerlog").text( 'logging count = ' + i);
return nokia.maps.util.Coroutine.sleep(100);
function runCoroutines(){
// 1st coroutine, init the iteration.
var coroutine1 = iterationCoroutine();
// 2nd second coroutine, init the logging.
var coroutine2 = loggingCoroutine();
// Add an observer to the 1st coroutine for it's status
coroutine1.addObserver("status", function( context, key, value, oldValue ) {
if ( value == nokia.maps.util.Coroutine.TERMINATED ) { // When gets terminated
nokia.maps.util.Coroutine.kill( coroutine2 ); // kill the 2nd coroutine
$("#showCoords").text( "coroutine1 has finished, and coroutine2 was terminated.");

A working example can be found at:

For more on the HERE Maps API

Please check out the HERE Maps API full documentation and API reference here:

You may also access the interactive API explorer

This page was last modified on 20 December 2013, at 15:35.
71 page views in the last 30 days.