×
Namespaces

Variants
Actions
(Difference between revisions)

HERE Maps API - How to add an HTML InfoBubble on the map

From Nokia Developer Wiki
Jump to: navigation, search
jasfox (Talk | contribs)
m (Jasfox - update to 2.2.4)
jasfox (Talk | contribs)
m (Jasfox - links)
 
(19 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Nokia Maps]][[Category:Code Snippet]][[Category:JavaScript]]
+
[[Category:HERE Maps]][[Category:JavaScript]][[Category:Code Examples]]
 
{{Abstract|This article explains how to add an info bubble on the map. The info bubble can contain any HTML content.}}
 
{{Abstract|This article explains how to add an info bubble on the map. The info bubble can contain any HTML content.}}
  
{{ArticleMetaData
+
{{ArticleMetaData <!-- v1.2 -->
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
+
|sourcecode= [https://github.com/heremaps/examples/blob/master/maps_api_for_javascript/simple-examples/infobubble.html infobubble.html] [https://github.com/heremaps/examples/blob/master/maps_api_for_javascript/simple-examples/infobubble-on-marker-click.html infobubble-on-marker-click.html] [https://github.com/heremaps/examples/blob/master/maps_api_for_javascript/simple-examples/opening-infobubble-with-id.html opening-infobubble-with-id.html]
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= Internet Explorer, Google Chrome, Firefox, Opera
 
|devices= Internet Explorer, Google Chrome, Firefox, Opera
Line 9: Line 9:
 
|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.2.4
+
|dependencies= HERE Maps 2.5.3 or higher
|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 -->
 
|keywords= <!-- APIs, classes and methods (e.g. QSystemScreenSaver, QList, CBase -->
|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 -->
 
|translated-by= <!-- [[User:XXXX]] -->
 
|translated-by= <!-- [[User:XXXX]] -->
|translated-from-title= <!-- Title only -->  
+
|translated-from-title= <!-- Title only -->
 
|translated-from-id= <!-- Id of translated revision -->
 
|translated-from-id= <!-- Id of translated revision -->
|review-by=<!-- After re-review: [[User:username]] -->
+
|review-by= <!-- After re-review: [[User:username]] -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 
|review-timestamp= <!-- After re-review: YYYYMMDD -->
|update-by=[[User:avnee.nathani]]
+
|update-by= [[User:avnee.nathani]]
|update-timestamp=20111231
+
|update-timestamp= 20111231
|creationdate=20110621
+
|creationdate= 20110621
|author=[[User:Maveric]]
+
|author= [[User:Maveric]]
 
}}
 
}}
 
{{SeeAlso|
 
{{SeeAlso|
* [http://developer.here.net/javascript_api Nokia Maps API]
+
* [http://developer.here.com/javascript_api HERE Maps API]
* [http://developer.here.net/apiexplorer/examples/api-for-js/information-bubbles/infomation-bubble.html Adding an Infobubble to the Map]
+
* [http://heremaps.github.io/examples/index.html HERE Maps on Github]
* [http://developer.here.net/apiexplorer/examples/api-for-js/information-bubbles/marker-with-information-bubble.html Adding an Infobubble to a Marker]
+
* [http://developer.here.com/apiexplorer/examples/api-for-js/information-bubbles/infomation-bubble.html Adding an Infobubble to the Map]
 +
* [http://developer.here.com/apiexplorer/examples/api-for-js/information-bubbles/marker-with-information-bubble.html Adding an Infobubble to a Marker]
 
  }}
 
  }}
  
Line 45: Line 45:
 
==Prerequisites==
 
==Prerequisites==
  
Nokia Maps API supported web browser (basically any modern web browser)
+
HERE Maps API supported web browser (basically any modern web browser)
  
 
==Important note about maps credentials==
 
==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
+
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
 
[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]
 
[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]
  
Line 57: Line 57:
  
 
<code javascript>
 
<code javascript>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
function addInfoBubble(map){
<html xmlns="http://www.w3.org/1999/xhtml">
+
map.set("baseMapType", map.SATELLITE);
    <head>
+
map.set("center", new nokia.maps.geo.Coordinate(19.119, 72.8957));
      <title>HTML Info Bubble</title>
+
map.setZoomLevel(10);
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
+
      <script type="text/javascript"
+
var infoBubbles = new nokia.maps.map.component.InfoBubbles();
            src="http://api.maps.nokia.com/2.2.4/jsl.js" charset="utf-8">
+
map.components.add(infoBubbles);
        </script>
+
    </head>
+
var myHTMLcontent = "<div class=\"myHtmlContent\">"+"<iframe width='425' height='349' " +
    <body>
+
" src='http://www.youtube.com/embed/Nr41eRzUnrg' frameborder='0' allowfullscreen>" +
 +
"</iframe></div>";
  
        <div id="mapContainer" style="z-index: -1; left:0px; top:0px; width: 100%; height: 80%; position: absolute;"></div>
+
infoBubbles.openBubble(myHTMLcontent,  
        <script type="text/javascript">
+
new nokia.maps.geo.Coordinate(19.119, 72.8957)) ;
/////////////////////////////////////////////////////////////////////////////////////
+
}
// Don't forget to set your API credentials
+
</code>
//
+
// 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");
+
 
+
//
+
/////////////////////////////////////////////////////////////////////////////////////
+
  
var myInfoBubbles = new nokia.maps.map.component.InfoBubbles();
 
var map = new nokia.maps.map.Display(document.getElementById("mapContainer"),  // new instance of Nokia Maps
 
{
 
 
    components: [ new nokia.maps.map.component.Behavior(),
 
                new nokia.maps.map.component.ZoomBar(),
 
                new nokia.maps.map.component.Overview(),                           
 
                new nokia.maps.map.component.TypeSelector(),   
 
                new nokia.maps.map.component.ScaleBar(),
 
                myInfoBubbles],
 
               
 
    'zoomLevel': 10,
 
    'center':[19.119, 72.8957]
 
});
 
 
var myHTMLcontent = "<div class=\"myHtmlContent\">"+"<iframe width='425' height='349' src='http://www.youtube.com/embed/Nr41eRzUnrg' frameborder='0' allowfullscreen></iframe>"+"</div>";
 
map.set("baseMapType", map.SATELLITE);
 
myInfoBubbles.addBubble(myHTMLcontent, new nokia.maps.geo.Coordinate(19.119, 72.8957)) ;
 
 
</script>
 
</body>
 
</html></code>
 
  
===Screenshot===
 
 
[[File:NokiaMapsInfoPopup.png|600px]]
 
[[File:NokiaMapsInfoPopup.png|600px]]
  
== Adding an InfoBubble to a map marker ==
+
A fully working example can be found at:
Assuming we already have a map, the first thing to do is to create an info bubble and add it as a map component.
+
 
 +
http://heremaps.github.io/examples/examples.html#infobubble
 +
 
 +
== Adding an InfoBubble to a map marker ==
 +
Assuming we already have a map, the first thing to do is to create an info bubble and add it as a map component, and a container with an {{Icode|onclick}} event
 
<code javascript>
 
<code javascript>
 
var infoBubbles = new nokia.maps.map.component.InfoBubbles();
 
var infoBubbles = new nokia.maps.map.component.InfoBubbles();
map.addComponent( infoBubbles);
+
map.components.add(infoBubbles);
 +
container = new nokia.maps.map.Container();
 +
container.addListener("click" ,  function(evt) {
 +
infoBubbles.openBubble(evt.target.html, evt.target.coordinate); 
 +
}, false);
 +
map.objects.add(container);
 
</code>
 
</code>
  
The function described below, will add an {{Icode|onclick}} event to a marker and display the associated HTML content.
+
The function described below, will add a marker and HTML content to the container
 
<code javascript>
 
<code javascript>
function addInfoBubble(infoBubbles, marker, html) {
+
function addMarkerToContainer(container, coordinate , html) {
      marker.html = html;
+
var marker =  new nokia.maps.map.StandardMarker(
    marker.addListener("click" , function(evt) {
+
coordinate, {html: html});
                                    infoBubbles.addBubble(evt.target.html, evt.target.coordinate);  
+
container.objects.add(marker);
                                        }, false);
+
 
}
 
}
 
</code>
 
</code>
Line 127: Line 104:
 
Finally the markers themselves must be added. Clicking on the marker brings up the embedded HTML.
 
Finally the markers themselves must be added. Clicking on the marker brings up the embedded HTML.
 
<code javascript>
 
<code javascript>
var marker;
+
addMarkerToContainer(container, [53.439,-2.221],
var markers = new Array();
+
"<div><a href='http://www.mcfc.co.uk' >Manchester City</a>" +
marker =  new nokia.maps.map.StandardMarker(new nokia.maps.geo.Coordinate(53.4393381986981,-2.2211828827858));
+
"</div><div >City of Manchester Stadium<br>Capacity: 48,000</div>");
addInfoBubble( infoBubbles, marker, "<div><a href='http://www.mcfc.co.uk' >Manchester City</a></div><div >City of Manchester Stadium<br>Capacity: 48,000</div>");
+
markers.push(marker);
+
addMarkerToContainer(container,  [53.430,-2.961],
marker = new nokia.maps.map.StandardMarker(new nokia.maps.geo.Coordinate(53.4308166986509,-2.96082615852356));
+
"<div ><a href='http://www.liverpoolfc.tv' >Liverpool</a>" +
addInfoBubble( infoBubbles, marker, "<div ><a href='http://www.liverpoolfc.tv' >Liverpool</a></div><div >Anfield<br>Capacity: 45,362</div>");
+
"</div><div >Anfield<br>Capacity: 45,362</div>");
markers.push(marker);
+
map.objects.addAll(markers);
+
 
</code>
 
</code>
  
Line 141: Line 116:
 
  [[File:MarkersWithInfoBubble.png]]
 
  [[File:MarkersWithInfoBubble.png]]
  
==For more on the Nokia Maps API==
+
Working Example:
 +
 
 +
http://heremaps.github.io/examples/examples.html#infobubble-on-marker-click
 +
 
 +
== Pre-opening an  InfoBubble ==
 +
It is possible to use the {{Icode|dispatch()}} method, to fire an  {{Icode|onclick}} event, which causes the {{Icode|infobubble}} to be opened when the page is loaded. Image a URL of the form {{Icode|../open-infobubble-with-id.html?id&#61;2}} - the query string of this url can be read to determine which infobubble should be opened.
 +
 
 +
First create a helper function to obtain the query string parameters:
 +
<code javascript>
 +
function getParameterByName(name) {
 +
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
 +
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
 +
results = regex.exec(location.search);
 +
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
 +
}
 +
</code>
 +
 
 +
The call the function below to open the relevant id
 +
<code javascript>
 +
function openInfoBubble(){
 +
var param = getParameterByName('id');
 +
if (param == ""){
 +
return; // Don't open a bubble.
 +
}
 +
// Iterate through the markers to find the correct id.
 +
for (var i = 0; i < container.objects.getLength(); i++){
 +
if (param  === container.objects.get(i).id){
 +
// fire an event to open the bubble.
 +
container.dispatch(
 +
new nokia.maps.dom.Event(
 +
{type: "click",
 +
target: container.objects.get(i)}));
 +
map.zoomTo(container.objects.get(i).getBoundingBox())
 +
break;
 +
}
 +
}
 +
}
 +
</code>
 +
 
 +
The markers  must be assigned a unique  {{Icode|id}} when instantiated as shown:
 +
<code javascript>
 +
var marker = new nokia.maps.map.StandardMarker(
 +
new nokia.maps.geo.Coordinate(52.516237, 13.377686),
 +
{html: "Brandenburger Tor",
 +
id : "1" }
 +
);
 +
var fernsehturmMarker = new nokia.maps.map.StandardMarker(
 +
new nokia.maps.geo.Coordinate(52.520816, 13.409417),
 +
{html: Fernsehturm,
 +
id : "2" }
 +
);
 +
</code>
 +
 
 +
Working Example - click on the links:
 +
 
 +
* [http://rawgithub.com/heremaps/examples/master/maps_api_for_javascript/simple-examples/opening-infobubble-with-id.html Map of Berlin]
 +
* [http://rawgithub.com/heremaps/examples/master/maps_api_for_javascript/simple-examples/opening-infobubble-with-id.html?id=1 Brandenburg Gate]
 +
* [http://rawgithub.com/heremaps/examples/master/maps_api_for_javascript/simple-examples/opening-infobubble-with-id.html?id=2 Fernsehturm]
 +
 
 +
==For more on the HERE Maps API==
  
Please check out the Nokia Maps API full documentation and API reference here:
+
Please check out the HERE Maps API full documentation and API reference here:
* [http://developer.here.net/javascript_api Nokia Maps API]
+
* [http://developer.here.com/javascript_api HERE Maps API]
  
 
You may also access the interactive API explorer
 
You may also access the interactive API explorer
* [http://developer.here.net/javascript_api_explorer API explorer]
+
* [http://developer.here.com/javascript_api_explorer API explorer]

Latest revision as of 15:43, 20 December 2013

This article explains how to add an info bubble on the map. The info bubble can contain any HTML content.

Article Metadata
Code ExampleTested with
Devices(s): Internet Explorer, Google Chrome, Firefox, Opera
Compatibility
Platform(s): Web browser
Dependencies: HERE Maps 2.5.3 or higher
Article
Created: Maveric (21 Jun 2011)
Updated: avnee.nathani (31 Dec 2011)
Last edited: jasfox (20 Dec 2013)

Contents

[edit] Introduction

An infoBubble must have a nokia.maps.geo.Coordinate and content which can be either provided as a string or as a nokia.maps.search.Location object.

API defines:

Extends nokia.maps.map.component.Component.
new nokia.maps.map.component.InfoBubbles ()
Options Interface defining the info bubbles' options (keys)


[edit] Prerequisites

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

[edit] 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

[edit] Adding an Infobubble directly onto the Map

In this example we will create one infoBubble with video content from YouTube. Location is mumbai, where a info bubble would be shown with out youtube video. Remember to add in your own AppId and Token.

function addInfoBubble(map){
map.set("baseMapType", map.SATELLITE);
map.set("center", new nokia.maps.geo.Coordinate(19.119, 72.8957));
map.setZoomLevel(10);
 
var infoBubbles = new nokia.maps.map.component.InfoBubbles();
map.components.add(infoBubbles);
 
var myHTMLcontent = "<div class=\"myHtmlContent\">"+"<iframe width='425' height='349' " +
" src='http://www.youtube.com/embed/Nr41eRzUnrg' frameborder='0' allowfullscreen>" +
"</iframe></div>";
 
infoBubbles.openBubble(myHTMLcontent,
new nokia.maps.geo.Coordinate(19.119, 72.8957)) ;
}


NokiaMapsInfoPopup.png

A fully working example can be found at:

http://heremaps.github.io/examples/examples.html#infobubble

[edit] Adding an InfoBubble to a map marker

Assuming we already have a map, the first thing to do is to create an info bubble and add it as a map component, and a container with an onclick event

var infoBubbles = new nokia.maps.map.component.InfoBubbles();
map.components.add(infoBubbles);
container = new nokia.maps.map.Container();
container.addListener("click" , function(evt) {
infoBubbles.openBubble(evt.target.html, evt.target.coordinate);
}, false);
map.objects.add(container);

The function described below, will add a marker and HTML content to the container

function addMarkerToContainer(container, coordinate , html) {
var marker = new nokia.maps.map.StandardMarker(
coordinate, {html: html});
container.objects.add(marker);
}

Finally the markers themselves must be added. Clicking on the marker brings up the embedded HTML.

addMarkerToContainer(container, [53.439,-2.221], 
"<div><a href='http://www.mcfc.co.uk' >Manchester City</a>" +
"</div><div >City of Manchester Stadium<br>Capacity: 48,000</div>");
 
addMarkerToContainer(container, [53.430,-2.961],
"<div ><a href='http://www.liverpoolfc.tv' >Liverpool</a>" +
"</div><div >Anfield<br>Capacity: 45,362</div>");

[edit] Screenshot

MarkersWithInfoBubble.png

Working Example:

http://heremaps.github.io/examples/examples.html#infobubble-on-marker-click

[edit] Pre-opening an InfoBubble

It is possible to use the dispatch() method, to fire an onclick event, which causes the infobubble to be opened when the page is loaded. Image a URL of the form ../open-infobubble-with-id.html?id=2 - the query string of this url can be read to determine which infobubble should be opened.

First create a helper function to obtain the query string parameters:

function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}

The call the function below to open the relevant id

function openInfoBubble(){
var param = getParameterByName('id');
if (param == ""){
return; // Don't open a bubble.
}
// Iterate through the markers to find the correct id.
for (var i = 0; i < container.objects.getLength(); i++){
if (param === container.objects.get(i).id){
// fire an event to open the bubble.
container.dispatch(
new nokia.maps.dom.Event(
{type: "click",
target: container.objects.get(i)}));
map.zoomTo(container.objects.get(i).getBoundingBox())
break;
}
}
}

The markers must be assigned a unique id when instantiated as shown:

var marker = new nokia.maps.map.StandardMarker(
new nokia.maps.geo.Coordinate(52.516237, 13.377686),
{html: "Brandenburger Tor",
id : "1" }
);
var fernsehturmMarker = new nokia.maps.map.StandardMarker(
new nokia.maps.geo.Coordinate(52.520816, 13.409417),
{html: Fernsehturm,
id : "2" }
);

Working Example - click on the links:

[edit] 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:43.
2523 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.

×