google.maps.places未定义 [英] google.maps.places is undefined
问题描述
我有一个网站,通过谷歌地图,街道和地方加载3个独立的视图。
请参阅下面的代码:
我终于获得了Google地图和街景视图的正常工作,但我正在为此付出一点努力。
我有一个标签
< script type =text / javascript
src = http://maps.googleapis.com/maps/api/js?v=3&key=....&sensor=false&callback=initializeMap >< /脚本>
< script type =text / javascript>
var myLattitude =<?php echo $ data [lattitude]; ?取代;
var myLongitude =<?php echo $ data [longitude]; ?取代;
var poiMap;
var infowindow;
函数initializePoi(){
var poiCentre = new google.maps.LatLng(myLattitude,myLongitude);
poiMap = new google.maps.Map(document.getElementById('poi-canvas'),{
center:poiCentre,
zoom:15
}) ;
var request = {
location:poiCentre,
radius:500,
types:['store']
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(poiMap);
service.nearbySearch(request,callback);
}
函数回调(结果,状态){
if(status == google.maps.places.PlacesServiceStatus.OK){
for(var i = 0; i< results.length; i ++){
createMarker(results [i]);
函数createMarker(place){
var placeLoc = place.geometry.location;
var marker = new google.maps.Marker({
map:poiMap,
position:place.geometry.location
});
google.maps.event.addListener(marker,'click',function(){
infowindow.setContent(place.name);
infowindow.open(poiMap,this );
});
现在这初始化正常,但控制台会引发以下错误:
TypeError: google.maps.places未定义
我只想知道为什么我会得到这个错误,我喜欢干净无错的代码。
这些地方确实显示正确和一切。
您应该在Google API网址中添加 libraries = places
选项
在您的情况下,您应该替换以下内容:
< script type =text / javascriptsrc = http://maps.googleapis.com/maps/api/js?v=3&key=....&sensor=false&callback=initializeMap\"></script>
有了这个:
< script type =text / javascriptsrc =http://maps.googleapis.com/maps/api/js?v=3&key=....&sensor=false& ;回调= initializeMap&安培;库=地方>< /脚本>
查看的结尾src =
I have a website that loads 3 seperate "views" of a location via Google Maps, Street and Places.
Please see my code below:
I have finally gotten Maps and Street view to work properly but am struggling a bit with this one.
I have a tab that displays the same as map but with places added.
<script type="text/javascript"
src="http://maps.googleapis.com/maps/api/js?v=3&key=....&sensor=false&callback=initializeMap"></script>
<script type="text/javascript">
var myLattitude = <?php echo $data["lattitude"]; ?>;
var myLongitude = <?php echo $data["longitude"]; ?>;
var poiMap;
var infowindow;
function initializePoi() {
var poiCentre = new google.maps.LatLng(myLattitude, myLongitude);
poiMap = new google.maps.Map(document.getElementById('poi-canvas'), {
center: poiCentre,
zoom: 15
});
var request = {
location: poiCentre,
radius: 500,
types: ['store']
};
infowindow = new google.maps.InfoWindow();
var service = new google.maps.places.PlacesService(poiMap);
service.nearbySearch(request, 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: poiMap,
position: place.geometry.location
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(place.name);
infowindow.open(poiMap, this);
});
}
Now This initializes properly but the console throws the following error: TypeError: google.maps.places is undefined
I just want to know why I get this error, I like having clean errorless code.
The places do actually show up properly and everything.
You should add the option libraries=places
in the Google API URL
In your case you should replace the following:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&key=....&sensor=false&callback=initializeMap"></script>
With this:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?v=3&key=....&sensor=false&callback=initializeMap&libraries=places"></script>
Look at the end of the src=""
这篇关于google.maps.places未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!