Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make leaflet map height variable

Tags:

css

leaflet

In my Application I was making div of map as

<div id="map" style="height: 610px; width:100%"></div> 

but to make my map responsive I want to make height also 100%, if I make height: 100% then it is not working.

How can I make height also variable like width so that map can be seen properly on any device.

Demo : http://jsfiddle.net/CcYp6/

If you change height & width of map then you will not get map.

like image 810
vaibhav shah Avatar asked May 14 '13 12:05

vaibhav shah


People also ask

How do I make a Leaflet map?

We recommend Sublime Text . If you want to follow along on your own computer, your maps will need be on a local web server. Easy ways of doing this include running Python's SimpleHTTPServer in your map directory, installing MAMP (for Mac), or installing WampServer (for Windows). This is a simple Leaflet map. A few lines of code. Let's make a map!

What data types can I map in leaflet?

GeoJSON is the de facto standard data type for web maps, and Leaflet has built-in methods to make it easy to map GeoJSON data. For more about GeoJSON, start with its Wikipedia article.

What's the best way to extend leaflet?

There's a whole slew of plugins people have written to extend the Leaflet core, most of them very easy to drop right into your project. We'll use a popular one, Leaflet.markercluster. A map cluttered with rat symbols isn't great, and this plugin will help make it more readable and usable. Poke around on the map below.

What happens if you change the height&width of map?

If you change height & width of map then you will not get map. Show activity on this post. Explanation: Why do you need to do that? So when you specify an element's height in % then the 1st question that arises is: 100% of what?


2 Answers

You need to set the parent elements to height: 100%; first

html, body {    height: 100%; } 

Demo

Demo (This won't work as no parent height is defined)

Explanation: Why do you need to do that? So when you specify an element's height in % then the 1st question that arises is: 100% of what? By default, a div has height of 0px, so 100% for a div simply won't work, but setting the parent elements height to 100%; will work.

like image 175
Mr. Alien Avatar answered Oct 02 '22 19:10

Mr. Alien


You have to set the div size with JavaScript.

$("#map").height($(window).height()).width($(window).width()); map.invalidateSize(); 

You can find a complete example here.

like image 43
jgillich Avatar answered Oct 02 '22 20:10

jgillich