2015-11-07 136 views
0

我正在试验谷歌地图API。我没有API密钥,但据我所知,你不需要v3的密钥。 但是地图不在页面上呈现,我不确定问题出在哪。 这里是我的代码:谷歌地图API未加载

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Place searches</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="utf-8"> 
    <style> 
     html, body { 
     height: 100%; 
     margin: 0; 
     padding: 0; 
     } 
     #map { 
     height: 100%; 
     } 
    </style> 
    <script> 
var map; 
var infowindow; 

function initMap() { 
    var pyrmont = {lat: -33.867, lng: 151.195}; 

    map = new google.maps.Map(document.getElementById('map'), { 
    center: pyrmont, 
    zoom: 15 
    }); 

    infowindow = new google.maps.InfoWindow(); 

    var service = new google.maps.places.PlacesService(map); 
    service.nearbySearch({ 
    location: pyrmont, 
    radius: 500, 
    types: ['store'] 
    }, callback); 
} 

function callback(results, status) { 
    if (status === google.maps.places.PlacesServiceStatus.OK) { 
    for (var i = 0; i < results.length; i++) { 
     createMarker(results[i]); 
    } 
    } 
} 

function createMarker(place) { 
    var placeLoc = place.geometry.location; 
    var marker = new google.maps.Marker({ 
    map: map, 
    position: place.geometry.location 
    }); 

    google.maps.event.addListener(marker, 'click', function() { 
    infowindow.setContent(place.name); 
    infowindow.open(map, this); 
    }); 
} 

    </script> 
    </head> 
    <body> 
    <div id="map"></div> 
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    </body> 
</html> 

任何帮助将不胜感激。

+1

您是否在开发者工具控制台上发现任何错误? –

+0

你似乎根本不会调用'initMap' - google地图的文档显示'&callback = initMap'作为脚本标记的一部分src –

回答

1

代替

http://maps.google.com/maps/api/js?sensor=false 

您可以使用此;

https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places&callback=initMap