Google Map is not working in my responsive tab - html

My Google map is not working
I used this tab view
http://www.petelove.com/responsiveTabs/
My google map init() is:
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: <?php echo $lat; ?>, lng: <?php echo $long; ?>},
zoom: 3
});
When i run my google map in first tab it's working fine.
But when i load in second tab. I just get view only. NO MAP DISPLAYING.....

This question has been asked about a thousand times on this website. You should first search before asking again.
The key is that your map is hidden when initialized and therefore, you need to trigger a resize event once the tab has been shown.
google.maps.event.trigger(map, "resize");
Regarding the "tabs" library: choose a library that exposes events. This doesn't seem to be the case (or it is not documented) with the library you choose.
A quick example: Bootstrap tabs
This library exposes events:
Events
When showing a new tab, the events fire in the following order:
hide.bs.tab (on the current active tab)
show.bs.tab (on the to-be-shown tab)
hidden.bs.tab (on the previous active tab, the same one as for the hide.bs.tab event)
shown.bs.tab (on the newly-active just-shown tab, the same one as for the show.bs.tab event)
This way, you have full control over what happens with your tabs. You know when it's shown, hidden, etc.
So you could use something like:
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
// Trigger map resize when tab is shown
google.maps.event.trigger(map, "resize");
});
Edit:
If you need to reset the center, do it here:
$('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
// Trigger map resize when tab is shown
google.maps.event.trigger(map, "resize");
map.setCenter(new google.maps.LatLng(<?php echo $lat; ?>, <?php echo $long; ?>));
});

I got this working with just CSS no JS needed:
.tab-content > .tab-pane {
display: block;
height:0;
overflow:hidden;
}
.tab-content > .active {
display: block;
height:auto;
}

Keval, you need to trigger a resize when you display the map.
According to the documentation, you need to trigger a resize event like by
google.maps.event.trigger(map, "resize");
I created a fiddle by adding a tab to the demo page. I used the attrchange jquery plugin to listen to all the changes made to the mapTab, the tab that contains the Google Maps, just to make things simple.
Look at this demo on jsfiddle.
if you take out the listener, the maps would not show unless you click the Resize Map button. Hope this make things clear.

Related

How to trigger Google Maps fullscreen mode with a separate button?

Basically I'd like to be able to add a standalone button to trigger the Google Map I've embedded onto my site to enter fullscreen mode. I haven't had any luck locating an answer as of yet, so any help would be greatly appreciated.
Currently the only way to enter fullscreen mode is by clicking the fullscreen button inside the map (at the top right). I imagine I just need to assign the same command that this button has, although I'm not sure if that would work on an element outside of the map.
Thanks in advance!
I don't know if you solved this, but i've recently faced same problem and I want to share the way I did it.
Basically what I've done is show the default fullscreen control then find it in the dom using its title, then I manually fired click event.
When I verified that worked, I written a css to hide the button.
This is a little bit hacky but it works!
function initMap() {
var container = $('#container').get(0);
var map = new google.maps.Map(container, {
center: {lat: -39.000, lng: -60.000},
zoom: 3,
fullscreenControl: true
});
$('#fullscreen').click(function() {
$('#container div.gm-style button[title="Toggle fullscreen view"]').trigger('click');
});
}
#container div.gm-style > button[title="Toggle fullscreen view"] {
display: none;
}
See this fiddle
Hope this help.
You could simply call requestFullscreen method from the map's container. Like this:
$('[data-id="map_go_fullscreen"]').click(function(e){
var map = document.querySelector(".gm-style");
map.requestFullscreen();
});
Docs: https://developer.mozilla.org/en-US/docs/Web/API/Element/requestFullScreen
map = new google.maps.Map(document.getElementById('map') , {
zoom: 6,
center: latLng,
gestureHandling: 'none'
});
initFullscreenControl(map);
function initFullscreenControl (map) {
var elementToSendFullscreen =
map.getDiv().firstChild;
var fullScreenControl =
document.querySelector('.fullscreen-control');
fullScreenControl.onclick = function() {
elementToSendFullscreen.requestFullscreen();
};
};
<div class="controls fullscreen-control">
<button title="Toggle Fullscreen"> Show full screen </button>
</div>
<div id = "map"></div>

Cordova Admob causing google map rendering "grey" screen in Ionic tabs

Running ionic tabs. My maps works fine until I click to another tab and then click back to the map. When returning to the map tab, most of the map is greyed out with a little bit of the map still appearing in the upper left corner. If I grab the visible section of the map and drag into the center view I see the visible maps is about 2/3rd of the screen - but the moment I let go the visible part shoots back up to upper left corner - and now all the previously greyed out section is just a blank white.
In addition, if I simply rotate my device from portrait to landscape - the map completely redraws itself correctly. And then from landscape back to portrait mode and the full maps is showing again.
For the life of me though, I can't get the 'grey' out from happening.
In my apps.js:
.run(function($ionicPlatform) {
$ionicPlatform.ready(function() {
globalGPS() ;
});
})
.config(function($stateProvider,$urlRouterProvider) {
'$compileProvider',
function( $compileProvider )
{
$compileProvider.aHrefSanitizationWhitelist(/^\s*(https?||tel):/);
// Angular before v1.2 uses $compileProvider.urlSanitizationWhitelist(...)
}
$stateProvider
// setup an abstract state for the tabs directive
.state('tab', {
url: "/tab",
abstract: true,
templateUrl: "templates/tabs.html",
controller: 'TabsCtrl'
})
// Each tab has its own nav history stack:
.state('tab.map', {
url: '/map',
views: {
'tab-map': {
templateUrl: 'templates/tab-map.html',
controller: 'MapCtrl'
}
}
})
The gps functions take place outside of the state/controllers loaded from a standard javascript file, and when all the gps that same external function sets the map as a global var:
setMap = new google.maps.Map(document.getElementById("mapBody"), myOptions);
In my controller is defined:
.controller('MapCtrl', function($scope,$rootScope,constants) {
// runs this code on EVERY return to map tab
$scope.$on('$ionicView.beforeEnter', function(){
if (setMap) {
google.maps.event.addListener(setMap, "idle", function(){
google.maps.event.trigger(setMap, "resize");
}) ;
// $scope.refreshMap() ; // see note below
}
});
$scope.refreshMap = function() {
setTimeout(function () {
$scope.refreshMap_();
}, 1);
};
$scope.refreshMap_ = function() {
var div = document.getElementById("mapBody");
reattachMap(setMap,div);
};
reattachMap() is an external function:
function reattachMap(map,div) {
if (!isDom(div)) {
return map;
} else {
map.set("div", div);
while(div.parentNode) {
div.style.backgroundColor = 'rgba(0,0,0,0)';
div = div.parentNode;
}
return map;
}
}
In place of the google.maps.event.trigger(setMap, "resize"), I tried using reattaching the map div thinking it had been removed from the DOM. Neither method works or even indicates I am onto the correct fix. In my div's that hold the maps I even hard set width/heigh css values as I had read that fixed some ppl's issues (whereas width/height percentages was causing the problem):
<div id="mapWrapper" style="position:absolute;width:100%;height:100%">
<div id="mapBody" data-tap-disabled="false"></div>
</div>
</div>
and
#mapBody {
border:2px solid #4e8cf9;
text-align:center;
height:700px;
width:400px;*/
flex: 1;
}
Well, I solved the issue. Turns out when moving away from the map tab to another tab, those other tabs are loading ads by AdMob. AdMob ads are not a part of the main DOM, they are a sub-view and thus they are persistent. If you navigate to another tab, the ad stays in the same place on the new tab. When navigating back to the map tab, the ad follows and somehow interferes with google maps ability to properly display itself.
In my app, the first default view is the map tab which doesn't show ads, so no map issues until user returns to the map tab (...and the persistent AdMob ad followed)
Sooo...i now used the above function to remove the ad from the map view completely.
.controller('MapCtrl', function($scope,$rootScope,constants) {
$scope.$on('$ionicView.beforeEnter', function(){
// this function will run EVERY time user goes back to this tab
if (setMap) { // only attempt to remove ad if 'map' is defined
removeAd() ; // global external function
});

Can I hide Google Maps' default "Exit street view button"?

The Google Maps documentation offers some customization of the StreetViewPanorama. However, what I'm using is the regular Google Maps Map, but with the option to go to Street view, as the StreetViewControl option is on.
Once the user goes into Street View on my Google Maps, the Street View shows default controls: A fullscreen button on the top right, and a back button with the address on the top left. But this is something my users don't quite intuitively understand (and frankly I don't blame them). I need a way to hide those controls, so I can substitute my own buttons.
I am familiar with detecting when a user has entered Street View, and I know how to show a button to have it exit Street View. What I don't know is, can I hide Google's default "back" button? I've tried using the properties of the StreetViewControl object straight into my Map object initialization, but it doesn't work; and understandably so, since some of the option names clash.
Per request, here is the code for showing the map, and showing/hiding my button that exits the Street view:
// Create a map object and specify the DOM element for display.
map = new google.maps.Map(document.getElementById('map'), {
center: this.mapCenter,
scrollwheel: true,
scaleControl: false,
overviewMapControl: false,
zoom: this.zoom
});
// Show the button for exiting Street View when Street view is entered
google.maps.event.addListener(map.getStreetView(), 'visible_changed', function(){
if(this.getVisible() == true) {
document.getElementById("exitStreetViewButton").style.display = "block";
} else {
document.getElementById("exitStreetViewButton").style.display = "none";
}
});
And this is the code that the Exit button executes to exit the Street View:
map.getStreetView().setVisible(false);
You can use this option:
panorama.setOptions(
{
enableCloseButton:false
}
);
I just ran into the same problem. Disabling the UI doesn't work, the exit button is the only element (besides the legal stuff at the bottom) still there.
As far as the documentation goes there is no way to remove it. I resorted to hiding it using css:
.gm-iv-container {
display: none;
}
Have a look at the StreetViewPanoramaOptions.
Try the disableDefaultUI option. You can set it to true to disable the default UI then enable some controls individually if you need.
var panoramaOptions = {
disableDefaultUI: true
};
JSFiddle demo
Edit:
If you need you can also do it that way:
var panorama = map.getStreetView();
panorama.setOptions(panoramaOptions);

How can I bind click event to custom overlay with Google maps v3 both in IE and Firefox

I've already subclass my overlay object under the instruction of google document, and my onAdd() function is listed below:
MyOverlay.onAdd() {
var div_parent = document.createElement("DIV");
var div_child = document.createElement("DIV");
div_child.innerHTML = "Click Me";
div_parent.appendChild( div_child );
this.getPanes().overlayLayer.appendChild(div_parent);
var this = that;
google.maps.event.addDomListener( div_parent, 'click', function(){
google.maps.event.trigger(that, 'click'); // from [http://stackoverflow.com/questions/3361823/make-custom-overlay-clickable-google-maps-api-v3]
alert("Clicked");
} );
}
My code can work well ONLY in IE, but in Firefox and Chrome, the click event will not be triggered anymore.
So how to solve the problem?
Instead of using overlayLayer mapPanes, you should use overlayMouseTarget.
Reference: http://code.google.com/apis/maps/documentation/javascript/overlays.html#CustomOverlays
I know this is an old post, but if you were wondering, here is the solution:
In your code above, you need to change the overlay function from:
this.getPanes().overlayLayer.appendChild(div_parent);
to:
this.getPanes().overlayMouseTarget.appendChild(div_parent);
Also note although your click events will be captured on desktop even if you use overlay pane, for touch events to work, mouse target pane is necessary.

Keep a Google Maps v3 Map hidden, show when needed

Is there a way of preventing a Google Maps (JS, v3) map being displayed from the get-go? I'm doing some pre-processing and would like to show my 'Loading' spinner until everything is good to go (more eloquently put, hide the map -- e.g. the container div – until all pre-processing is complete – at which point, show the map).
Hooking up the map's idle event doesn't help that much, since the map is already displayed when this event hits.
I know that the container div gets inline-styled by GMaps after loading, my first idea was to clear out the style attribute (whilst listening to the idle event), but it would be interesting to see if there is a way of creating the map and not displaying it until all pre-processing is done.
Maybe by using an argument to the new google.maps.Map constructor, or a MapOption ?
Any thoughts on this?
Thank you in advance!
Also remember to call:
google.maps.event.trigger(map, 'resize');
if you have changed the size of the <div>. A display:none <div> has no size.
Or you could just hide it like with css visablility or css opacity.
$("#GoogleMap").css({ opacity: 0, zoom: 0 });
initialize();
google.maps.event.addListener(map,"idle", function(){
$('#Loader').hide();
$("#GoogleMap").css({ opacity: 1, zoom: 1 });
});
This works for me. I'm using the JQuery library.
$(document).ready(function(){
$('#Checkbox').click(function(){
$('#googleMapDiv').toggle();
initialize(); // initialize the map
});
});
another way to show the hidden map when map is first time rendering the <div> is to set style: visibility.
When firstly hidden, use visibility = hidden; to show use visibility = visible
the reason is: visibility:hidden means that the contents of the element will be invisible, but the element stays in its original position and size.
this works fine for me, I use jquery tabs
setTimeout(function() {
google.maps.event.trigger(map, "resize");
map.setCenter(new google.maps.LatLng(default_lat, default_lng));
map.setZoom(default_map_zoom);
}, 2000);
om this link https://code.google.com/p/gmaps-api-issues/issues/detail?id=1448
This will work
google.maps.event.addListener(map, "idle", function ()
{
google.maps.event.trigger(map, 'resize');
});
better way:
gmap.redraw = function() {
gmOnLoad = true;
if(gmOnLoad) {
google.maps.event.trigger(gmap, "resize");
gmap.setCenter(gmlatlng);
gmOnLoad = false;
}
}
and in show click event:
$("#goo").click(function() {
if ($("#map_canvas").css("display") == "none") {
$("#YMapsID").toggle();
$("#map_canvas").toggle();
if (gmap != undefined) {
gmap.redraw();
}
}
});
depending on what you are doing another posibility could be to have multiple bools you set to true when each process is done.
For example:
if you have a geocode service running which you want to wait for, you could have a var called
GeoState
and in the result part of the geocoder set GeoState to true,
then have a timed function check if all the services have returned true, when they have, make the map visible.