Here's my conundrum: I have a page that uses Google Maps V3 and jQuery. It all worked well locally in FF5, Chrome and Safari.
Once I uploaded to a web site, I get a "google is not defined" error on the first line that I try to use a google object
var defaultLocation = new google.maps.LatLng(lat, lng);
It only occurs in FF and only occurs remotely (i.e., if I load the file into FF locally, it works well). Chrome and Safari seem to be working great regardless, as is my Android and iPod browsers.
Here's what I tried so far:
- Moved
<script开发者_如何学运维 type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
to top of the<head>
section. - Moved all content of
$(function() {...});
to a function calledinitialize()
and added<body onload="initialize()">
- Played with scripts and css files order
- Pasted the URL http://maps.google.com/maps/api/js?sensor=false into FF address box and verified I'm getting the legit script
But since this is only happening in FF on a remote machine and works well otherwise, I don't think it has anything to do with my code. Maybe the load order in FF5 is screwed. Maybe it prioritizes network resources differently than other browsers. I really do not know what to make of it at this point.
Any help is appreciated.
GuyUpdate:
Just wanted to add the following fact: After trying the previous on a Mac, I tried FF5 in Windows, and have replicated the exact same behavior. For good measure, I tried Pale Moon as well - same results. Chrome 14, Opera 11.50 and even frickin' IE9 (which wasn't included in the test plan) work. It just FF5, now on both Mac and Windows 7, that fails on that page.I faced 'google is not defined' several time. Probably Google Script has some problem not to be loaded well with FF-addon BTW. FF has restart option ( like window reboot ) Help > restart with Add-ons Disabled
I had the same error "google is not defined" while using Gmap3. The problem was that I was including 'gmap3' before including 'google', so I reversed the order:
<script src="https://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script src="/assets/gmap3.js?body=1" type="text/javascript"></script>
Another suggestion that helped me:
Here is what happent to me => My script was working once in 3 time I was loading the page and the error was the «google is not defined».
My function using the google map was in my jQuery document's ready function
$(function(){
//Here was my logic
})
I simply added this code to make sure it works:
$(function(){
$(window).load(function(){
//Here is my logic now
});
});
It works like a charm. If you want more details on difference between document ready and window load, here is a great post about it: window.onload vs $(document).ready()
The ready event occurs after the HTML document has been loaded, while the onload event occurs later, when all content (e.g. images) also has been loaded.
The onload event is a standard event in the DOM, while the ready event is specific to jQuery. The purpose of the ready event is that it should occur as early as possible after the document has loaded, so that code that adds functionality to the elements in the page doesn't have to wait for all content to load.
Try using this:
<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
try this:
<script src="https://maps.googleapis.com/maps/api/js"></script>
it works for me... the point is, change HTTP to HTTPS
Add the type for the script
<script src="https://maps.googleapis.com/maps/api/js" type="text/javascript"></script>
So the important part is the type text/javascript.
instead of this
var defaultLocation = new google.maps.LatLng(lat, lng);
use this
var defaultLocation = new window.google.maps.LatLng(lat, lng);
and this worked for me.
From Firefox 23, there is Mixed Content Blocking Enabled set by default (locally disabled). It blocks some APIs from Google also if you use secure connection and some unsecure APIs.
To disable it you'll have to click shield which appears in location bar when there are some unsecure contents, set 'Disable protection' and then you'll have to look at yellow exclamation mark in location bar :(
https://blog.mozilla.org/.../mixed-content-blocking-enabled-in-firefox-23/
You can always try also replace http protocol with https in the API url. If API is provided also in secure connection - you will not see any warnings.
It works for me.
You can try the following:
First, add async defer
. This specifies that the script will be executed asynchronously as soon as it is available and when the page has finished parsing.
Second, add the initMap()
function as a callback in a script tag inside your html. In this way the map will be initialized before the document.ready and window.onload:
<script async defer src="{{ 'https://maps.googleapis.com/maps/api/js?key=$key&language='.$language.'®ion='.$country.'&callback=initMap' }}"></script>
<script>
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 4,
disableDefaultUI: false,
scrollwheel: false,
styles: [{ ... }]
});
}
</script>
Finally, you can use the map object inside your js files.
Changed the
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API">
function(){
myMap()
}
</script>
and made it
<script type="text/javascript">
function(){
myMap()
}
</script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=API"></script>
It worked :)
I think the easiest trick is:
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR API KEY&callback=initMap">google.maps.event.addDomListener(window,'load', initMap);</script>
It will init the map when your app is ready.
Check this.
Please check the order you are calling, your libraries, the following order
<script type = "text/javascript" src = "../resources/googleMaps/jquery-ui.js"></script>
<script type = "text/javascript" src = "../resources/googleMaps/jquery-ui.min.js"></script>
<script type = "text/javascript" src="http://maps.googleapis.com/maps/api/
METOD
<script type = "text/javascript" src = "googleMaps/mapa.js"></script>
I was with this problem, I just adjusted my order.
In my case I was loading the google script via http while my server had SSL and its being loaded over https. So I had to load script via https
I don't know for sure but here are my best suggestions.
You're using jQuery. So instead of setting the event you should really be using $(function() {... }); to do your initialization. The reason to use this is that it ensures that all the scripts on the page have loaded and you're not limited to just one init function like you are with the onload body tag.
Also, be sure your Javascript code is after the Google include. Otherwise your code might execute before the Google objects are created.
I would also suggest taking a look at this page about event order.
http://dean.edwards.name/weblog/2005/09/busted/
If you have mentioned a call back function in API call, you should wrap your google map code inside a JavaScript function with same name. Else you will get this error.
精彩评论