×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - How to create a Context Menu

From Nokia Developer Wiki
Jump to: navigation, search
hamishwillee (Talk | contribs)
m (Hamishwillee - Add dependencies field to ArticleMetaData)
jasfox (Talk | contribs)
m (Jasfox - update to 2.2.4)
(18 intermediate revisions by 3 users not shown)
Line 1: Line 1:
[[Category:Web]][[Category:Browser]][[Category:Nokia Maps]][[Category:Code Snippet]][[Category:How To]]
+
[[Category:Nokia Maps]][[Category:Code Snippet]][[Category:JavaScript]]
{{Abstract|This article explains how to enable right click on Nokia maps and add components to it - making the map tiles more interactive.}}
+
{{Abstract|This article explains how to add context menus to Nokia Maps and how to customise them.}}
 +
 
  
 
{{ArticleMetaData
 
{{ArticleMetaData
Line 9: Line 10:
 
|platform= Web browser
 
|platform= Web browser
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
 
|devicecompatability= <!-- Compatible devices e.g.: All* (must have internal GPS) -->
|dependencies=Nokia Maps 2.1.0
+
|dependencies=Nokia Maps 2.2.4
 +
|keywords= Nokia Maps, component, Context Menu, Right Click, JavaScript
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|signing=<!-- Signing requirements - empty or one of: Self-Signed, DevCert, Manufacturer -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
 
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|id= <!-- Article Id (Knowledge base articles only) -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
Line 25: Line 26:
 
|author=[[User:Maveric]]
 
|author=[[User:Maveric]]
 
}}
 
}}
 +
 +
{{SeeAlso|
 +
* [http://developer.here.net/javascript_api Nokia Maps API]
 +
* [http://developer.here.net/apiexplorer/examples/api-for-js/context-menu/context-menu.html  Context menu Example]
 +
}}
 +
[[File:NokiaMapsRightClick.png|700px]]
 +
<br />
 +
Context menus are used to display relevant information and options to the user on right-click:
  
 
==Introduction==
 
==Introduction==
 +
Context menus are useful because they can be customised to display items that are not present by default, and provide the user with added functionality.
 +
They are instantiated as follows:
  
When the user clicks the mouse right button, a RightClick event takes place. The Nokia Maps API there is certain functionality that can be attached to it and the opening menu.
+
<code java>var contextMenu = new nokia.maps.map.component.ContextMenu();</code>
 
+
In this example we will add Routing Component and the Search component.
+
 
+
map.components.add(myRightClick);
+
map.components.add(myRoutingComponent);
+
map.components.add(mySearchComponent);
+
 
+
These can be then bound to further parts of your program to provide additional functionality.
+
  
 
==Prerequisites==
 
==Prerequisites==
Line 42: Line 45:
 
Nokia Maps API supported web browser (basically any modern web browser)
 
Nokia Maps API supported web browser (basically any modern web browser)
  
==Important about Maps credentials==
+
==Important note about maps credentials==
  
With Nokia Maps API you can start without having any credentials given, but you might face a performance gap. In order to get the full potential out of the
+
Nokia provides several services options within the Maps API offering. The service is free to use, but  you must obtain and use authentication and authorization credentials to use the services. Please read the
offering, you must get the credentials that authenticate your application against the Services. Please read through the Location API.
+
[http://developer.here.net/terms_conditions Terms and Conditions] and check the [http://developer.here.net/web/guest/plans Pricing Plans page]  to decide which business model best fits your needs. Authentication requires unique Maps API credentials, namely an AppId and a token. You can get these credentials free for free following the instructions  [http://developer.here.net/docs/maps_js/topics/credentials.html#acquiring-credentials here]
  
For more information on how to obtain the credentials, please start with the Nokiai Maps API Developers Guide section "Acquiring API credentials"
+
==Code Commentary==
 +
Context menu divisions are added to the context menu with the following syntax
 +
<code java> var menuItems1 = function(contextMenuEvent, group) {
 +
group.addEntry("Menu Item 1",
 +
function(activationEvent) {alert("Menu Item 1 clicked");});
 +
group.addEntry("Menu Item 2",
 +
function(activationEvent) {alert("Menu Item 2 clicked");});
 +
group.addEntry("Menu Item 3",
 +
function(activationEvent) {alert("Menu Item 3 clicked");});
 +
contextMenu.addHandler(menuItems1);
 +
</code>
 +
The {{Icode|addHandler()}} function takes as a parameter a function which by default creates a new context menu division, in this case adding three new items to the new division.
  
 +
<code java> contextMenu.removeHandler(handler); </code>
 +
The {{Icode|removeHandler()}} method removes a whole context menu division and all items within it.
 
==Implementation==
 
==Implementation==
  
In the example we will add a RightClick() component to the map, then populate it with Routing and Search.
 
  
 +
The following statement is important:
 +
<code java>map.components.add(contextMenu);</code>
 +
Without this the context box is not rendered on the map.
  
 
==Example code==
 
==Example code==
 +
The example displays a context menu when the map is right clicked, Further {{Icode|alerts()}} are displayed when the menu items are clicked.
 +
Remember to add in your own [[How to Obtain Your Own Nokia appID and Token| AppId and Token]].
 +
<code javascript>
 +
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 +
<html xmlns="http://www.w3.org/1999/xhtml">
 +
<head>
 +
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
 +
<meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9" />
 +
<title>Context Menu</title>
 +
<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.4/jsl.js"></script>
 +
</head>
 +
<body>
 +
<form>
 +
Click  to add/remove  a group of menu items from the context menu. <br />
 +
<input type = "button" id ="addRemove" value = "Add Menu Items" onclick = "addRemoveGroup(extraMenuItems)"/>
  
<code java>
+
</form>
<html>
+
<div id="mapcanvas" style="width:1200px; height:800px;" >&nbsp;</div>
<head>
+
 
    <script type="text/javascript"
+
    src="http://api.maps.nokia.com/2.0.0/jsl.js" charset="utf-8">
+
</script>
+
  <script type="text/javascript">
      <link rel="stylesheet" href="style.css" />
+
/*<![CDATA[*/
</head>
+
 
<body>
+
/////////////////////////////////////////////////////////////////////////////////////
<div id="map" style="width:100%; height:100%;"></div>
+
// Don't forget to set your API credentials
</body>
+
//
<script type="text/javascript">
+
// Replace with your appId and token which you can obtain when you
+
// register on http://api.developer.nokia.com/
myRightClick = new nokia.maps.map.component.RightClick () ;
+
//
myRoutingComponent = new nokia.maps.routing.component.RightClick();
+
nokia.Settings.set( "appId", "YOUR APP ID GOES HERE");  
mySearchComponent = new nokia.maps.search.component.RightClick();
+
nokia.Settings.set( "authenticationToken", "YOUR AUTHENTICATION TOKEN GOES HERE");
+
//
var map = new nokia.maps.map.Display(document.getElementById("map"),   // new instance of Nokia Maps
+
/////////////////////////////////////////////////////////////////////////////////////
{
+
 
+
// Get the DOM node to which we will append the map
    components: [  
+
var mapContainer = document.getElementById("mapcanvas");
        new nokia.maps.map.component.Behavior(),
+
                new nokia.maps.map.component.ZoomBar(),
+
                new nokia.maps.map.component.Overview(),                           
+
// Create a map inside the map container DOM node
                new nokia.maps.map.component.TypeSelector(),    
+
var map = new nokia.maps.map.Display(mapContainer, {
                new nokia.maps.map.component.ScaleBar()
+
components: [
                ],
+
new nokia.maps.map.component.panning.Drag(),
     'zoomLevel': 8,  
+
new nokia.maps.map.component.panning.Kinetic()
    'center':[19.119, 72.8957]
+
],
});
+
'zoomLevel': 13,
+
  'center': [18.975, 72.825833]
map.set("baseMapType", map.NORMAL);
+
});
+
map.components.add(myRightClick);
+
var contextMenu = new nokia.maps.map.component.ContextMenu();
map.components.add(myRoutingComponent);
+
map.components.add(mySearchComponent);
+
/* Add Custom Context Menu Items */
+
</script>
+
//This adds three items with a menu separator
</body>
+
var menuItems1 = function(contextMenuEvent, group) {
 +
group.addEntry("Menu Item 1",
 +
function(activationEvent) {alert("Menu Item 1 clicked");});
 +
group.addEntry("Menu Item 2",
 +
function(activationEvent) {alert("Menu Item 2 clicked");});
 +
group.addEntry("Menu Item 3",
 +
function(activationEvent) {alert("Menu Item 3 clicked");});
 +
     }
 +
//This adds one item with a menu separator
 +
var menuItems2 = function(contextMenuEvent, group) {
 +
group.addEntry("Menu Item 4",
 +
function(activationEvent) {alert("Menu Item 4 clicked");});
 +
    }
 +
//Two extra items added with a menu separator when Add/Remove button clicked
 +
var extraMenuItems = function(contextMenuEvent,group){
 +
group.addEntry("Menu Item 5",
 +
function(activationEvent) {alert("Menu Item 5 clicked");});
 +
  group.addEntry("Menu Item 6",
 +
function(activationEvent) {alert("Menu Item 6 clicked");});
 +
}
 +
 
 +
contextMenu.addHandler(menuItems1);
 +
contextMenu.addHandler(menuItems2);
 +
map.components.add(contextMenu);
 +
 +
var isAdded = false;   
 +
 +
 +
// Simple function to add/remove a group of menu items...
 +
function addRemoveGroup(handler){
 +
if (isAdded == false){
 +
contextMenu.addHandler(handler);
 +
document.getElementById("addRemove").value ="Remove Menu Items";
 +
isAdded = true;
 +
} else {
 +
contextMenu.removeHandler(handler);
 +
document.getElementById("addRemove").value ="Add Menu Items";
 +
isAdded = false;
 +
}
 +
}
 +
/*]]>*/
 +
</script>
 +
    </body>
 
</html>
 
</html>
 
</code>
 
</code>
  
== Screenshot ==
+
==For more on the Nokia Maps API==
[[File:NokiaMapsRightClick.png|700px]]
+
 
+
==For more on Nokia Maps API==
+
  
 
Please check out the Nokia Maps API full documentation and API reference here:
 
Please check out the Nokia Maps API full documentation and API reference here:
* http://api.maps.nokia.com/
+
* [http://developer.here.net/javascript_api Nokia Maps API]
  
You may also access the interactive Nokia Maps API playground,
+
You may also access the interactive API explorer
* http://api.maps.nokia.com/2.1.0/playground/index.html
+
* [http://developer.here.net/javascript_api_explorer API explorer]
  
 
==Tested with==
 
==Tested with==
 
* Google Chrome 11.0x
 
* Google Chrome 11.0x
 
* Mozilla Firefox 5.0b
 
* Mozilla Firefox 5.0b

Revision as of 14:28, 14 March 2013

This article explains how to add context menus to Nokia Maps and how to customise them.


Article Metadata
Compatibility
Platform(s): Web browser
Dependencies: Nokia Maps 2.2.4
Article
Keywords: Nokia Maps, component, Context Menu, Right Click, JavaScript
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (31 Dec 2011)
Last edited: jasfox (14 Mar 2013)

NokiaMapsRightClick.png
Context menus are used to display relevant information and options to the user on right-click:

Contents

Introduction

Context menus are useful because they can be customised to display items that are not present by default, and provide the user with added functionality. They are instantiated as follows:

var contextMenu = new nokia.maps.map.component.ContextMenu();

Prerequisites

Nokia Maps API supported web browser (basically any modern web browser)

Important note about maps credentials

Nokia provides several services options within the Maps API offering. The service is free to use, but you must obtain and use authentication and authorization credentials to use the services. Please read the Terms and Conditions and check the Pricing Plans page to decide which business model best fits your needs. Authentication requires unique Maps API credentials, namely an AppId and a token. You can get these credentials free for free following the instructions here

Code Commentary

Context menu divisions are added to the context menu with the following syntax

 var menuItems1 = function(contextMenuEvent, group) {
group.addEntry("Menu Item 1",
function(activationEvent) {alert("Menu Item 1 clicked");});
group.addEntry("Menu Item 2",
function(activationEvent) {alert("Menu Item 2 clicked");});
group.addEntry("Menu Item 3",
function(activationEvent) {alert("Menu Item 3 clicked");});
contextMenu.addHandler(menuItems1);

The addHandler() function takes as a parameter a function which by default creates a new context menu division, in this case adding three new items to the new division.

 contextMenu.removeHandler(handler);

The removeHandler() method removes a whole context menu division and all items within it.

Implementation

The following statement is important:

map.components.add(contextMenu);

Without this the context box is not rendered on the map.

Example code

The example displays a context menu when the map is right clicked, Further alerts() are displayed when the menu items are clicked. Remember to add in your own AppId and Token.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9" />
<title>Context Menu</title>
<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.4/jsl.js"></script>
</head>
<body>
<form>
Click to add/remove a group of menu items from the context menu. <br />
<input type = "button" id ="addRemove" value = "Add Menu Items" onclick = "addRemoveGroup(extraMenuItems)"/>
 
</form>
<div id="mapcanvas" style="width:1200px; height:800px;" >&nbsp;</div>
 
 
 
<script type="text/javascript">
/*<![CDATA[*/
 
/////////////////////////////////////////////////////////////////////////////////////
// Don't forget to set your API credentials
//
// Replace with your appId and token which you can obtain when you
// register on http://api.developer.nokia.com/
//
nokia.Settings.set( "appId", "YOUR APP ID GOES HERE");
nokia.Settings.set( "authenticationToken", "YOUR AUTHENTICATION TOKEN GOES HERE");
//
/////////////////////////////////////////////////////////////////////////////////////
 
// Get the DOM node to which we will append the map
var mapContainer = document.getElementById("mapcanvas");
 
 
// Create a map inside the map container DOM node
var map = new nokia.maps.map.Display(mapContainer, {
components: [
new nokia.maps.map.component.panning.Drag(),
new nokia.maps.map.component.panning.Kinetic()
],
'zoomLevel': 13,
'center': [18.975, 72.825833]
});
 
var contextMenu = new nokia.maps.map.component.ContextMenu();
 
/* Add Custom Context Menu Items */
 
//This adds three items with a menu separator
var menuItems1 = function(contextMenuEvent, group) {
group.addEntry("Menu Item 1",
function(activationEvent) {alert("Menu Item 1 clicked");});
group.addEntry("Menu Item 2",
function(activationEvent) {alert("Menu Item 2 clicked");});
group.addEntry("Menu Item 3",
function(activationEvent) {alert("Menu Item 3 clicked");});
}
//This adds one item with a menu separator
var menuItems2 = function(contextMenuEvent, group) {
group.addEntry("Menu Item 4",
function(activationEvent) {alert("Menu Item 4 clicked");});
}
//Two extra items added with a menu separator when Add/Remove button clicked
var extraMenuItems = function(contextMenuEvent,group){
group.addEntry("Menu Item 5",
function(activationEvent) {alert("Menu Item 5 clicked");});
group.addEntry("Menu Item 6",
function(activationEvent) {alert("Menu Item 6 clicked");});
}
 
contextMenu.addHandler(menuItems1);
contextMenu.addHandler(menuItems2);
map.components.add(contextMenu);
 
var isAdded = false;
 
 
// Simple function to add/remove a group of menu items...
function addRemoveGroup(handler){
if (isAdded == false){
contextMenu.addHandler(handler);
document.getElementById("addRemove").value ="Remove Menu Items";
isAdded = true;
} else {
contextMenu.removeHandler(handler);
document.getElementById("addRemove").value ="Add Menu Items";
isAdded = false;
}
}
/*]]>*/
</script>
</body>
</html>

For more on the Nokia Maps API

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

You may also access the interactive API explorer

Tested with

  • Google Chrome 11.0x
  • Mozilla Firefox 5.0b
212 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×