问题
I need to implement on my php page map. I have container <div id="map"></div> which is inside other div, and I put this code inside <head> tag but it doesn't show at all. Can anybody help me?
<script type="text/javascript" src="javascript/jquery-1.4.3.js"></script>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
(function() {
window.onload = function() {
var mapDiv = document.getElementById('map');
var latlng = new google.maps.LatLng(37.09, -95.71);
var options = {
center: latlng,
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(mapDiv, options);
}
})();
</script>
回答1:
Did you specify size for your div? eg:
<div id="map" style="width: 300px; height: 300px;"></div>
If this doesn't work you might have to post a snippet of your HTML and CSS..
回答2:
I had some thing like that happen and all I had to do was use jquery compliance code. jQuery(document).ready(function($) { $(function() { // Your map code here. }); });
回答3:
Your code does work for me even though the last line, when you close that anonymous function, gave me an error like you were using some illegal character, so I had to retype it. Once I did, it worked perfectly showing a road map of all of the United States. Other than that possible illegal character you could also try:
- giving your #map element a height and width or else it won't be visible.
- putting all your javascript inside $(document).ready() so it's only execute when the document is ready.
- you say that you include your scripts in the head tag of your document, but maybe you should try putting them at the end of your body to speed up loading.
Hope that helps.
回答4:
Check the validity of your HTML and the doctype declaration. It has been an issue on several Google Maps I've setup.
See this
回答5:
If any one using google map code from javascript or jquery or any language, He/She should use for local host https://localhost/test/ or in server https://example.com.
Means use secure url like https
来源:https://stackoverflow.com/questions/3960056/google-map-not-showing-up