|
| 1 | +--- |
| 2 | +title: Always Show Marker Tooltip |
| 3 | +description: How to have labels on the map markers by utilizing their tooltips |
| 4 | +type: how-to |
| 5 | +page_title: Always show marker tooltips like labels | Kendo UI Map |
| 6 | +slug: map-marker-labels-from-always-shown-tooltips |
| 7 | +position: |
| 8 | +tags: map,show,always,marker,tooltip,label |
| 9 | +ticketid: 1412666 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | +<table> |
| 15 | + <tbody> |
| 16 | + <tr> |
| 17 | + <td>Product</td> |
| 18 | + <td>Map for Progress® Kendo UI®</td> |
| 19 | + </tr> |
| 20 | + </tbody> |
| 21 | +</table> |
| 22 | + |
| 23 | + |
| 24 | +## Description |
| 25 | +The map markers have tooltips the user can show by hovering with the mouse. In some cases, you may want them to show up automatically when the map loads. |
| 26 | + |
| 27 | +To do this, you need to loop the markers, and call the .show() method of their tooltips. You can do this after the map initializes and in its [events](https://docs.telerik.com/kendo-ui/api/javascript/dataviz/ui/map#events). |
| 28 | + |
| 29 | +## Solution |
| 30 | +```dojo |
| 31 | +<div id="map"></div> |
| 32 | +
|
| 33 | +<script> |
| 34 | + function createMap() { |
| 35 | + var map = $("#map").kendoMap({ |
| 36 | + center: [30.268107, -97.744821], |
| 37 | + zoom: 3, |
| 38 | + layers: [{ |
| 39 | + type: "tile", |
| 40 | + urlTemplate: "http://#= subdomain #.tile.openstreetmap.org/#= zoom #/#= x #/#= y #.png", |
| 41 | + subdomains: ["a", "b", "c"], |
| 42 | + attribution: "© <a href='http://osm.org/copyright'>OpenStreetMap contributors</a>" |
| 43 | + }], |
| 44 | + markerDefaults: { //make tooltips "sticky" by default |
| 45 | + tooltip: { |
| 46 | + autoHide: false |
| 47 | + } |
| 48 | + }, |
| 49 | + markers: [{ |
| 50 | + location: [30.268107, -97.744821], |
| 51 | + shape: "pinTarget", |
| 52 | + tooltip: { |
| 53 | + content: "Austin, TX" |
| 54 | + } |
| 55 | + }, { |
| 56 | + location: [40.7128, -74.0060], |
| 57 | + shape: "pinTarget", |
| 58 | + tooltip: { |
| 59 | + content: "New York, NY" |
| 60 | + } |
| 61 | + }], |
| 62 | + zoomEnd: function (e) { //when the user zooms or pans, re-show the tooltips |
| 63 | + showTooltips(e.sender); |
| 64 | + }, |
| 65 | + panEnd: function (e) { |
| 66 | + showTooltips(e.sender); |
| 67 | + } |
| 68 | + }).data("kendoMap"); |
| 69 | + return map; |
| 70 | + } |
| 71 | +
|
| 72 | + $(document).ready(function () { |
| 73 | + var map = createMap(); |
| 74 | + showTooltips(map); //show the tooltips initially |
| 75 | + }); |
| 76 | +
|
| 77 | + function showTooltips(map) { |
| 78 | + setTimeout(function () { |
| 79 | + var extent = map.extent(); //we use this to only show tooltips for markers that are visible |
| 80 | + for (var i = 0; i < map.markers.items.length; i++) { |
| 81 | + if (extent.contains(map.markers.items[i].options.location)) { |
| 82 | + map.markers.items[i].tooltip.show();//show the tooltips |
| 83 | + } |
| 84 | + } |
| 85 | + }, 500); //kinetic scrolling and loading new content can cause concurrency issues if no timeout is present. You can test smaller values if you like, though |
| 86 | + } |
| 87 | +</script> |
| 88 | +
|
| 89 | +<style> |
| 90 | + /* remove the manual close button from the tooltips so the user can't dismiss them and the are more like labels */ |
| 91 | +
|
| 92 | + div.k-tooltip-button { |
| 93 | + display: none; |
| 94 | + } |
| 95 | +
|
| 96 | + div.k-tooltip div.k-tooltip-content { |
| 97 | + padding-right: 0; |
| 98 | + } |
| 99 | +</style> |
| 100 | +``` |
0 commit comments