How add image and text to popup of leaflet map - dictionary

I want to add image and text to popup of leaflet map such as this link:
https://www.bookdepository.com/
I can add image But How I can add text near the image?
my code is here:
<div id="map" style="width:270px;height:310px;"></div>
<?php
$matrix = [
[34.05777800, 51.48416700],
[38.50972200, 46.65444400],
[29.26666700, 51.21666700],
[34.05777800, 51.48416700],]
?>
<script type="text/javascript">
var matrix = JSON.parse('<?php echo json_encode($matrix);?>');
var map = L.map('map').setView(matrix[0],7);
var i = 1;
function myLoop() {
setTimeout(function () {
var div = L.DomUtil.create('div', 'my-div');
var img = L.DomUtil.create('img', 'my-img',div);
img.style.width = '60px';
img.style.height = '80px';
div.style.textAlign = 'right';
img.src = 'http://127.0.0.1:8000/productImages/64517.jpg';
L.marker(matrix[i]).addTo(map)
.bindPopup(div)
.openPopup();
map.flyTo(matrix[i]);
i++;
if (i < 10) {
myLoop();
}
}, 6000)
}
L.tileLayer('https://stamen-tiles
{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}', {
ext: 'jpg'
}).addTo(map);
myLoop();
</script>
My output is this pic:

You can use bindpopup function of leaflet to attach a pop up with your custom html.
marker.bindPopup("<div>Your custom html</div>");
You can find more information here.

You can indeed use the .bindPopup method like this (in your case) :
matrix
.bindPopup("<div> <p>Text</p> <img src="path/or/url"/> </div>");
Nevertheless it seems that using the css background-image in a <span> tag as popup image doesn't work correctly, I recommend using an <img> tag.
Hope it's helpful, you can find more information on methods there.

Related

OpenLayers 3 - Add scale line out of map

How to add a scaleline out of map area?
My HTML:
<div id="map"></div>
<div id="scaleline"></div>
and javascript code for adding scaleline:
var scale = new ol.control.ScaleLine({
target: document.getElementById("scaleline")
});
map.addControl(scale);
It sounds a CSS problem, but how do i configure css for div "scaleline" ?
Should work. See in action in this fiddle
var scaleLineControl = new ol.control.ScaleLine({
target: document.getElementById('scaleline')
});
var map = new ol.Map({
controls: ol.control.defaults().extend([scaleLineControl])
})

What element is jQuery UI draggable being dragged over in an iframe

Here is my code, where I'm trying to detect the element, which a jQuery UI draggable is hovering over. I need to get the element's object and attributes, such as class names (in this case .sortable-grid,.sortable-table,.sortable-row,.sortable-cell).
The answers found here only show how to get the draggable item itself (ui.helper or event.target), but not the element it is hovering above.
The best way to answer would be using the prepared JSFiddle, since my code uses an iframe, which would not work if the full code is posted here:
JSFiddle
HTML:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0-beta.1/jquery-ui.js"></script>
<div style="background-color:grey;display:inline;cursor:move" id="draggable">DRAG ME</div>
<iframe src="https://fiddle.jshell.net/piglin/UAcC7/1869/show/" id="frame" style="width:100%;overflow:visible" seamless="seamless" scrolling="no"></iframe>
JS:
$("#draggable").draggable({
drag: function(event, ui) {
//Some code here
}
}
It was possible by modifying the function from another answer to fit this purpose. After adapting it to use the contentWindow of the iframe and adding offset calculation it works now.
Solution
function allElementsFromPointIframe(x, y, offsetX, offsetY) {
var element, elements = [];
var old_visibility = [];
while (true) {
element = document.getElementById('frame').contentWindow.document.elementFromPoint(x - offsetX, y - offsetY);
if (!element || element === document.getElementById('frame').contentWindow.document.documentElement) {
break;
}
elements.push(element);
old_visibility.push(element.style.visibility);
element.style.visibility = 'hidden'; // Temporarily hide the element (without changing the layout)
}
for (var k = 0; k < elements.length; k++) {
elements[k].style.visibility = old_visibility[k];
}
elements.reverse();
return elements;
}
var selected = $('');
var tmpColor = 'transparent';
$("#draggable").draggable({
drag: function(event, ui) {
var el = $(allElementsFromPointIframe(event.pageX, event.pageY, $(frame).offset().left, $(frame).offset().top));
var div = $(el).filter('ul, li').not($(this));
selected.css({'backgroundColor': tmpColor});
selected = div.last()
tmpColor = selected.css('backgroundColor');
selected.css({'backgroundColor': 'red'});
console.dir(div);
},
iframeFix: true,
iframeOffset: $('#iframe').offset()
});

Creating a button for Leaflet JS "panTo spot on map" outside of the map in Meteor

I have created a map in Meteor using Leaflet JS. The problem is, I could only get map.panTo to work inside the Template.dynamicmap.rendered area. However, this makes it so anywhere you click on the map pans to the location.
This is the complete rendered area with id and access token removed.
Template.dynamicmap.rendered = function() {
var southWest = L.latLng(35.02035919622158, -121.21049926757814);
var northEast = L.latLng(42.4426214924114, -110.79740478515624);
var mybounds = L.latLngBounds(southWest, northEast);
var map = L.map('map_container',{zoomControl: false, maxBounds: [[37.00035919622158, -119.23049926757814],[40.4626214924114, -112.77740478515624]],}).setView([38.685509760012, -115.86181640625001], 10);
var w = window.innerWidth;
var h = window.innerHeight;
$('#map_container').css({width: w+'px', height: h+'px'});
L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
bounds: mybounds,
maxZoom: 10,
minZoom: 9,
}).addTo(map);
L.Icon.Default.imagePath = 'packages/leaflet/images';
var marker = L.marker([38.685509760012, -115.86181640625001]).addTo(map);
map.fitBounds(bounds);
};
So I tried putting it in an Template event shown below, this button is not within the map area but still in the dynamicmap template. It does not work though.
Template.dynamicmap.events({
'click input.goto3':(function() {
map.panTo(L.latLng(38.685509760012, -115.86181640625001));
})
});
I receive the error:
"Uncaught ReferenceError: map is not defined"
in the console. Which I have been trying to wrap my head around but no luck. I was hoping someone here could point me in the right direction.
Here is my HTML Template.
<template name="dynamicmap">
<div class="containerbox">
<div class="map_container" id="map_container">
</div>
</div>
<input type="button" class="goto3" value="Go"/>
</template>
You need to make map a global variable:
var map = null;
Template.dynamicmap.rendered = function() {
var southWest = L.latLng(35.02035919622158, -121.21049926757814);
var northEast = L.latLng(42.4426214924114, -110.79740478515624);
var mybounds = L.latLngBounds(southWest, northEast);
map = L.map('map_container',{zoomControl: false, ...
...

How to make an info window clickable?

I have the below code, based on one of the API samples. A click on the map creates a marker. A click on the marker opens up an info window. Now I want a click on the info window to do something. E.g. a click anywhere might close it, as opposed to the little cross in the corner. Or a click on it might open a new URL. Etc.
Unfortunately it seems there is no "click" event for info windows.
The closest I've got is shown as a commented out line below: I wrap my info window content in a div, and give that an onClick. This works, but there is a big border around it. I really want to be able to click anywhere in the info window box.
Is there a way?
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Click Test</title>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
google.maps.visualRefresh = true; //New look visuals.
function initialize() {
var useragent = navigator.userAgent;
var mapdiv = document.getElementById("map-canvas");
if (useragent.indexOf('iPhone') != -1 || useragent.indexOf('Android') != -1 ) {
mapdiv.style.width = '100%';
mapdiv.style.height = '100%';
} else {
mapdiv.style.width = '400px';
mapdiv.style.height = '600px';
}
var mapOptions = {
zoom:3,
center: new google.maps.LatLng(37.09024, -95.712891),
mapTypeId: google.maps.MapTypeId.TERRAIN
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
google.maps.event.addListener(map, 'click', function(event) {
placeMarker(event.latLng);
});
function placeMarker(location) {
var marker = new google.maps.Marker({
position: location,
map: map
});
var infowindow = new google.maps.InfoWindow({
//content: "<div onClick='test1()'>(lat,lng):<br/>"+location.lat()+","+location.lng()+"</div>"
content: "(lat,lng):<br/>"+location.lat()+","+location.lng()
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(marker.get('map'), marker);
infowindow.addListener('click',test1); //Does nothing
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
function test1(){alert("test1");}
</script>
</head>
<body>
<div id="map-canvas"></div>
</body>
</html>
UPDATE:
This image shows the problem when I use a clickable div inside the content (background set to red to show the extent of the region I can make clickable, and also that I can style; if I set a negative margin I just get scrollbars, not a bigger region!). It is the whole white area I want to be clickable, not just that red rectangle.
I decided to use InfoBox found in the Google Maps Utility Library. So in the header add a link to the library. Then replace the new google.maps.InfoWindow() line with this one:
var infowindow = new InfoBox({
closeBoxURL:"",
content: '<div onClick="test1();return false;" style="background:white;opacity:0.8;padding:8px">(lat,lng):<br/>'+
location.lat()+","+location.lng()+"</div>"
});
By setting closeBoxUrl to a blank string I get no close option. I added a large padding just so you can see that clicking right to the edge does indeed work.
You can also do it this way. I also use the boxClass option so the formatting is done in CSS:
var infoContent=document.createElement('div');
infoContent.innerHTML="(lat,lng):<br/>"+location.lat()+","+location.lng();
infoContent.onclick=test1;
var infowindow = new InfoBox({
closeBoxURL:"",
boxClass:"marker_popup",
content: infoContent,
});
(Aside, if doing it this way, on just some browsers it creates a marker below the InfoBox! Simplest fix is to change test1 so it looks like: function test1(event){alert("test1");event.preventDefault();return false;} )
P.S. I chose InfoBox over InfoBubble, as the latter has no documentation, and it had no obvious advantages to compensate for that major flaw! InfoBox has documentation and a reference. (links are for version 1.1.9)

How to change text color of autocomplete input box (GMaps API v3)

I used the code of a beginners turtorial (by Mano Marks / Google) about the autocomplete method of GMaps. Now everything works fine, however I don't know how to change the color of the suggested strings in the autocomplete box that is adjusted to my input-field
I want to do that because the default color seems to be white!!??
Does somebody know how to change that color! Thanks a lot.
Here is the code:
HTML:
Suche:<input type="text" id="autocomplete" size="40" />
......
JS:
//Autocomplete
var acOptions = {
types:['establishment'],
};
var autocomplete =
new google.maps.places.Autocomplete(document.getElementById('autocomplete'),acOptions);
autocomplete.bindTo('bounds',map);
var ac_iw = new google.maps.InfoWindow();
var ac_marker = new google.maps.Marker({
map: map
});
google.maps.event.addListener(autocomplete,'place_changed',function(){
ac_iw.close();
var place = autocomplete.getPlace();
if(place.geometry.viewport){
map.fitBounds(place.geometry.viewport);
}
else{
map.setCenter(place.geometry.location);
map.setZoom(17);
}
ac_marker.setPosition(place.geometry.location);
ac_iw.setContent('<strong>' + place.name + '</strong><br/>');
ac_iw.open(map,ac_marker);
google.maps.event.addListener(ac_marker,'click',function(){
ac_iw.open(map,ac_marker);
});
});
AFAIK there is no particular text-color set for the suggestions. But however, you may use the CSS-selector
.pac-container *
... to apply a custom style.

Resources