×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - Coroutines

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Ovi Maps category)
hamishwillee (Talk | contribs)
m (Hamishwillee - Automated change of category from Ovi Maps to Nokia Maps)
Line 1: Line 1:
[[Category:Web]][[Category:Browser]][[Category:Ovi Maps]]
+
[[Category:Web]][[Category:Browser]][[Category:Nokia Maps]]
 
{{Abstract|This article will demonstrate the use of Ovi Maps API feature called Coroutine }}  
 
{{Abstract|This article will demonstrate the use of Ovi Maps API feature called Coroutine }}  
  

Revision as of 02:00, 6 December 2011

This article will demonstrate the use of Ovi Maps API feature called Coroutine

Contents

Introduction and Prerequisites

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

http://api.maps.ovi.com/apireference/symbols/ovi.mapsapi.util.Coroutine.html?s=Coroutine

The example below is using the same concept as in the "Ovi Maps API - Pan the map" example.

As the Coroutine built in serves the similar functionality as 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 you should read the releated 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.

<script type="text/javascript">
 
//Globals
 
var myInterval = 0;
 
var myTimer;
 
 
var map_start_x = 100;
var map_start_y = 100;
var map_end_x = 120;
var map_end_y = 100;
 
var myMap;
 
if (document.addEventListener)
document.addEventListener("DOMContentLoaded", all_is_ready, false);
 
 
function all_is_ready(){
 
alert("Click OK to start the panning using Coroutine");
 
myMap = new ovi.mapsapi.map.Display(document.getElementById("map"),
{
components: [ new ovi.mapsapi.map.component.Behavior(),
new ovi.mapsapi.map.component.ZoomBar(),
new ovi.mapsapi.map.component.Overview(),
new ovi.mapsapi.map.component.TypeSelector(),
new ovi.mapsapi.map.component.ScaleBar() ],
zoomLevel: 3,
center: [52.51, 13.4]
 
});
 
 
var i = 0,
Coroutine = ovi.mapsapi.util.Coroutine;
 
// Define the 1st coroutine - increase the value of i by one every 10 milliseconds, until we reach a 100.
// Watch the console output for the numbers!
 
var iteration = Coroutine.create("iterator", function( scope, context ) {
i++;
 
if ( i <= 500 ) {
document.getElementById("showCoords").innerHTML = "Wait until 500 to see the map move! : "+i+"/500";
return Coroutine.sleep(10);
}
 
map_start_x = map_start_x + 200;
map_start_y = map_start_y + 200;
map_end_x = map_end_x - 400;
map_end_y = map_end_y + 400;
 
// pan the map to the new position after the loop is over
myMap.pan(map_start_x, map_start_y, map_end_x, map_end_y);
 
});
 
// 2nd coroutine display iteration in console.log every 100 miliseconds
var printing = Coroutine.create("printing", function( scope, context ) {
console.log(i);
 
return Coroutine.sleep(100);
});
 
// 1st coroutine, init to the "coroutine" variable
var coroutine = iteration();
 
// 2nd second coroutine, init to the "coroutine2" variable
var coroutine2 = printing();
 
// Add an observer to the 1st coroutine for it's status
coroutine.addObserver("status", function( context, key, value, oldValue ) {
if ( value == Coroutine.TERMINATED ) { // When gets terminated
Coroutine.kill( coroutine2 ); // kill the 2nd coroutine
document.getElementById("showCoords").innerHTML = "Coroutine finished, and got terminated.";
}
});
 
}
 
 
</script>
<html>
<head>
<script src="http://api.maps.ovi.com/jsl.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<center>
<div id="map" style="width:80%; height:80%;"></div>
<div id="showCoords" style="height: 2.0em; font-size: 2em; color: blue;"></div>
</center>
</body>
</html>

Tested with

Windows XP Pro

Firefox 5.0

Safari 5.0.5


For more on Ovi Maps API go to

http://api.maps.ovi.com

91 page views in the last 30 days.
×