The layout of the placemark balloon
Object layouts can be created using the templateLayoutFactory factory and text templates.
This example creates a custom balloon layout for a geo object. The layout is set for the geo object via options.
index.html
placemark_balloon_layout.js
<!DOCTYPE html>
<html>
<head>
<title>Examples. The layout of a geo object's balloon.</title>
<meta
http-equiv="Content-Type"
content="text/html; charset=UTF-8"
/>
<!--
Set your own API-key. Testing key is not valid for other web-sites and services.
Get your API-key on the Developer Dashboard: https://developer.tech.yandex.ru/keys/
-->
<script
src="https://api-maps.yandex.ru/2.1/?lang=en_RU&apikey=<your API-key>"
type="text/javascript"
></script>
<script
src="https://yandex.st/jquery/2.2.3/jquery.min.js"
type="text/javascript"
></script>
<script
src="placemark_balloon_layout.js"
type="text/javascript"
></script>
<style>
html,
body,
#map {
width: 100%;
height: 100%;
padding: 0;
margin: 0;
}
</style>
</head>
<body>
<div id="map"></div>
</body>
</html>
ymaps.ready(init);
function init() {
var map = new ymaps.Map(
"map",
{
center: [55.650625, 37.62708],
zoom: 10,
},
{
searchControlProvider: "yandex#search",
}
),
counter = 0,
/**
* Creating a balloon content layout.
* The layout is created using the layout factory and a text template.
*/
BalloonContentLayout = ymaps.templateLayoutFactory.createClass(
'<div style="margin: 10px;">' +
"<b>{{properties.name}}</b><br />" +
'<i id="count"></i> ' +
'<button id="counter-button"> +1 </button>' +
"</div>",
{
/**
* Redefining the "build" function in order to start listening to the "click" event
* on a counter button when creating the layout.
*/
build: function () {
// First, we call the "build" method of the parent class.
BalloonContentLayout.superclass.build.call(this);
// Then we perform additional steps.
$("#counter-button").bind("click", this.onCounterClick);
$("#count").html(counter);
},
/**
* In the same way, we redefine the "clear" function in order to
* remove listening for clicks when the layout is deleted from the map.
*/
clear: function () {
/**
* We perform the steps in reverse order - first remove the listener,
* and then call the "clear" method of the parent class.
*/
$("#counter-button").unbind(
"click",
this.onCounterClick
);
BalloonContentLayout.superclass.clear.call(this);
},
onCounterClick: function () {
$("#count").html(++counter);
if (counter == 5) {
alert("Good work.");
counter = 0;
$("#count").html(counter);
}
},
}
);
var placemark = new ymaps.Placemark(
[55.650625, 37.62708],
{
name: "Calculating",
},
{
balloonContentLayout: BalloonContentLayout,
/**
* Disabling replacing the normal balloon with the balloon panel.
* If you do not specify this option, the balloon panel opens on small maps.
*/
balloonPanelMaxMapArea: 0,
}
);
map.geoObjects.add(placemark);
}