简体   繁体   中英

Having trouble getting Google Maps to display on my site.

I'm getting my lat & long via:

$geocode = file_get_contents('http://maps.google.com/maps/api/geocode/json?address=' . $g_location . '&sensor=true');
$output= json_decode($geocode);

$lat = $output->results[0]->geometry->location->lat;
$long = $output->results[0]->geometry->location->lng;

I return the correct lat & long. So now I try to build my map:

<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCTQqh2ksk9ZTqSfx-_ki_lHj-q32kYIY8&sensor=false">    </script>
<script language="JavaScript">
 var map;
      function initialize() {
        var mapOptions = {
          zoom: 8,
          center: new google.maps.LatLng(<?=$lat?>, <?=$long?>),
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById('map_canvas'),
            mapOptions);
      }

      google.maps.event.addDomListener(window, 'load', initialize);      
</script>

HTML:

div id="map_canvas"></div>

Nothing is showing up. Am I missing something?

Thanks.

您的< div id="map_canvas">需要一个大小(宽度和高度),因此,

<div id="map_canvas" style="width:400px;height:300px"></div>

Add the following. You need to use initialize() in the onload function in the body as well

<body onload="initialize()">
    <div id="map_canvas" style="width: 500px; height: 300px"></div>
</body>

Please refer to the google map playground examples

http://code.google.com/apis/ajax/playground/?exp=maps#map_simple

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM