google map show after screen resize? - javascript

I'm having problems with the google map I'm embedding, I don't understand why on load the map not appears and only appear when I resize the window, I went through different solutions in stackoverflow but they don't work for me.
Here is my js code:
<script>
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 31.554606, lng: 74.357158},
zoom: 14
});
google.maps.event.addListenerOnce(map, 'idle', function() {
google.maps.event.trigger(map, 'resize');
});
}
</script>
my div
<div id="map"></div>
when i load application it appears like
and after re size browser window it appears perfectly like this
kindly tell me how to fix this problem ?

call google.maps.event.trigger(map, 'resize') when your "div id=map" will be visible.
Please see How do I resize a Google Map with JavaScript after it has loaded?

Nothing worked for me. I was using Model box in Bootstrap to load the map. I tried everything for hours. Every time I would load the map I would see a grey box, but as soon as i would resize the browser, it would display the map. Seemed it required resizing of window. So I put all the map functionality into a function called initmap2() and displayed the fresh map inside it. and upon clicking i used
setTimeout( initMap2, 200 );
So as soon as the Modal box would open, it would display the map, and I tried working will 100 milliseconds but I guess it works with 200+ milliseconds so I kept 500ms just to be safe.
I hope this helps

I found this working only after combine two things: setTimeout and google resize.
So, at the end of initMap, I set:
google.maps.event.addListener(map, "idle", function(){
google.maps.event.trigger(map, 'resize');
});
And create a setTimeout for the initMap like this:
$(document).ready(function(){
setTimeout(initMap, 1000);
});
--
Edit
To answer #LucaC.: my initMap is a separate js, that I link into the pages. The solution above is defined in this js (the setTimeout only).
But, after a while, I noticed this problem still happening with mobile devices.
Working around: keep the setTimeout for the initMap and on the page I load the map, I've set up
$(window).load(function(){
setTimeout(function(){
google.maps.event.trigger(map, "resize");
}, 250);
});
So: i) delay anything that break the map; ii) at the end of all, resize the map after page load :)

Nothing worked for me. I was using modal box in bootstrap to load the map. i did following code may it will help you
First i load map in body with display:none like below
<div style="display:none; overflow:hidden; width:100%; height:400px;" id="map"></div>
by initMap method, map will load properly but we used display=none so that map will not display.
For display that map in model i append the div which have id="map" to id="map-modal" which is in the model. like below
$('#modal-id').on('shown.bs.modal', function () {
document.getElementById('map').style.display = "";
var mapNode = map.getDiv();
$('#map-modal').append(mapNode);
});
May it will work.

Resize your map without javascript
step 1: Go to google maps and after searching your location, click on share and
Get the Google Maps Embed
Code
step 2: code will look something like this
<iframe src="/" width="600" height="450" frameborder="0" style="border:0"
allowfullscreen></iframe>
step 3: Add a div tag around the embed code. Use the CSS class map-responsive
<div class="map-responsive">
<iframe src="/" width="600" height="450" frameborder="0" style="border:0"
allowfullscreen></iframe>
</div>
step 4: Add your Google Maps CSS
.map-responsive{
overflow:hidden;
padding-bottom:56.25%;
position:relative;
height:0;
}
.map-responsive iframe{
left:0;
top:0;
height:100%;
width:100%;
position:absolute;
}
this will make your map responsive to any screen size.!!

Related

How can I use a function to display Google Maps in an HTML code?

When my mouse goes over an image, I want to display google maps for where the image was taken. Where in MapFunction am I going wrong? Are the arguments in div being used incorrectly?
I am a beginner in HTML. I wanted to create a website where an image is displayed but when my mouse is over the image, the location of where that image was taken is displayed (i have the lat and long already in the code). To do this, I use onmouseover and onmouseout. I'm not sure where I went wrong in the functions. I understand that I can use a screenshot of the map for display purposes, but I need to know how to display google maps for future knowledge.
On a side note, why do I need to put in my API key if I wont be using my location? The instructions shown in https://developers.google.com/maps/documentation/javascript/adding-a-google-map say that I need to do this but I'm not sure why the API itself is necessary.
Thanks for your help.
<head>
<style>
#MapAndImg {
width: 600px;
height: 400px;
background-color: lightblue;
background-image: linear-gradient(lightblue, white);
}
</style>
</head>
<body>
<em> May 30 </em><br>
<div onmouseover = "MapFunction()" onmouseout = "ImgFunction()" border = 5 id = "MapAndImg">
<script>
function MapFunction() {
var loc = {lat: 51.12722222, lon: 114.20194444};
var map = new google.maps.Map(document.getElementById('MapAndImg'), {zoom: 4, center: loc});
var marker = new google.maps.Marker({position: loc, map: map});
}
function ImgFunction() {
<img src = "https://www.istockphoto.com/resources/images/HomePage/FourPack/2018_05/Illustrations_499178960.jpg" alt = "editeted.jpg">
document.getElementById('MapAndImg');
}
</script>
<script async defer src = "https://maps.googleapis.com/maps/api/js?key=(INSERT API KEY HERE)&callback=initMap"></script>
</div>
</body>
The map itself isn't showing up when I open it on Firefox and Internet Explorer, even though a similar code found in https://developers.google.com/maps/documentation/javascript/adding-a-google-map does have the map show up.

Google Maps API showing grey map on ajax page load

I'm using ajax to load (into a div) the page that includes the Google map, however when it's finished loading it shows only a grey background for the map, with a Google logo in the bottom left, and a "Terms of use" link in the bottom right.
If I resize the window, then the map instantly appears. Also if I reload the entire page by pressing F5 the map loads normally. It's only when I initially load this map page using jQuery ajax that I get the grey map.
On the container page I load the maps api.
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
On the map page I do everything else.
$(document).ready(function() {
initializeMap();
});
function initializeMap() {
var companyname = document.getElementById("companyname").value;
infowindow = new google.maps.InfoWindow();
latlng = new google.maps.LatLng(-34.397, 150.644);
var mapOptions = {
zoom: 13,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
geo = new google.maps.Geocoder();
map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
bounds = new google.maps.LatLngBounds();
resultsPanel = new google.maps.DirectionsRenderer({suppressMarkers:true});
resultsPanel.setMap(map);
$("#progressbar").progressbar(0);
var companyaddress = getCompanyAddress();
setCompanyMarker(companyaddress,companyname)
}
On resize I recalculate the height so that it fills the full page. If I don't do this then the map opens with 1px height, though I don't know why. Could that be the same issue?
function calculateHeight(){
var height = $('#maincontent').height();
$('#map-canvas').css("height",height);
}
$(window).resize(function () {
calculateHeight();
});
Since the map appears when the user resizes the window manually I thought programatically calling the resize event would have the same effect, so I added this:
$(window).trigger('resize');
But that did nothing.
I tried moving the api script to the map page but that caused a "You've loaded the same script multiple times" error if I load the page more than once, which then triggered other errors. However, with this setup I was able to load the map successfully using ajax after three page loads. In other words, after loading the container page once, I have to click on the menu item (that loads the map page) three times to get it to load properly. the first two times it still fails to load properly, but after that it loads properly every time, though with a load of JavaScript errors.
I also tried loading the api script asynchronously as per this suggestion.
I also tried adding "async defer" to the api script as per this suggestion.
Neither of these suggestions worked, and now I'm here.
What is causing the map to load with a grey background when the page is loaded via ajax, and what can I do about it?
1.Make sure map-canvas have width set in CSS.
2.When exactly did you try calling google.maps.event.trigger(map,'resize'); as geocodezip suggested? You need to call it after any map-canvas div's dimension change (calculateHeight) or any change to visibility of maps div. If the map-canvas div, or it's parent, or it's parent's parent (any predecessor) has display:none set at some point, the map view won't initialise properly and you will only see gray map. Also add the map resize code into some timeout to be sure, and then lower it if it works, e.g:
function calculateHeight(){
var height = $('#maincontent').height();
$('#map-canvas').css("height",height);
setTimeout(function(){ //also do this after any other visibility/dimension change
google.maps.event.trigger(map,'resize');
}, 500);
}
Try this, which worked for me
google.maps.event.addListener(map, 'idle', function()
{
google.maps.event.trigger(map, 'resize');
});
map.setZoom( map.getZoom() - 1 );
map.setZoom( map.getZoom() + 1 );

Why do I have to reload the page to display the map correctly?

I asked this question already here, unfortunately the problem could not be solved, so I ask here :)
I've got a JQM page in which I show a map. Stupidly, to show the map correctly, I have to reload the page.
This is how the map gets displayed on the first load of the page:
And this is how it gets displayed after refreshing the page:
Here is my code:
var map;
require([
"esri/map",
"dojo/dom",
"esri/layers/ArcGISTiledMapServiceLayer",
"dojo/domReady!"
],
function (Map, dom, Tiled) {
map = new Map("map", {
logo: false,
minZoom: 1,
maxZoom: 11
});
var luftbild = new Tiled(URL);
map.addLayer(luftbild);
});
And this is how I style the map:
<style>
html, body, #map {
padding: 0;
margin: 0;
height: 100%;
}
</style>
And how I display it:
<div data-role="page" style="background-color:red" ...>
<!--Header-->
....
<!--/Header-->
<!--Content-->
<div id="map">
</div>
<!--/Content-->
</div>
EDIT:
After putting this code of line:
$(document).on("pageshow","#page",function(){ // initialize map and show
});
the Map looks like this now:
Is it possible to set the width and height to 100%?
Map needs a canvas with defined width and height. In Jquery mobile handles pages differently than normal html pages. i.e. one page (i.e. div with data-role=page) is visible at a time, others are not. So initializing map on any page should be done with jquery mobile specific page event called pageshow
$(document).on("pageshow","#page",function(){ // initialize map and show
});

Google Maps API v2 - Huge gap at bottom of infoWindow

I've simplified my JavaScript code to the example below; this code is giving me a huge border below the text in my infoWindow. Looking at examples on the web with single line infoWindows it seems like this is a standard feature. Is there any way to eliminate this?
Thanks
var marker = new GMarker(point);
map.addOverlay(marker);
GEvent.addListener(marker, "mouseover", function () {
marker.openInfoWindow('a<br />b<br />c<br />d<br />a<br />b<br />c<br />d<br />a<br />b<br />c<br />d<br />');
}
);
Update: Found the answer, this CSS is causing the problem .gmnoprint {
font-size:12px;
} The smaller font-size seems to be making the infoWindow display as though it was displaying the default font-size, hence the extra space. Does anyone have a good solution? Thanks
Answer was the simplest one: Add a new CSS class with the amended font size (e.g. myGoogleMapInfoWindow, and add a <div class="myGoogleMapInfoWindow"> around the content of your openInfoWindow argument. This makes the Google Maps API behave correctly. Of course it is better to do this using DOM objects rather than a HTML string. The answer seems obvious now, but it was someone else's code I had to fix this on.

Virtual Earth maps don't show when starting as display:none

I have this code to show a map using the Virtual Earth API:
<script type="text/javascript">
function GetMap() {
var map = map = new VEMap('myMap');
map.LoadMap(new VELatLong(47.6, -122.33), 10, 'h', false);
}
$(document).ready(function () {
GetMap();
});
</script>
Show Map
<div id="myMap" style="position:relative; width:400px; height:400px; display:none;"></div>
This doesn't work and displays a black box where the map should go. If I remove the display: none; style then it works just fine. But I don't want the map to be visible when the page loads, I want the user to toggle it. Can anyone see anything wrong with my approach?
Maybe the map needs to be on display when it initializes. It happens especially if the map has to measure the dimensions of the container in order to render properly.
Either go as Diodes suggested moving the map off visual area (you could also set visibility to false) or initialize the map when you actually have to show it.
Alternately, you could move it off-screen, with left:-2000px.

Categories