gpt4 book ai didi

javascript - Uncaught ReferenceError : google is not defined

转载 作者:行者123 更新时间:2023-12-03 06:01:45 26 4
gpt4 key购买 nike

我尝试应用示例 Extjs 6 Google map ,但出现错误 google is not Defined on GMapPanel.js 文件。任何人都可以帮助我为什么会显示此错误,我提到我花了时间在这里和其他论坛上阅读为什么会出现此错误,但没有给我答案?提前致谢

viewMap.js

Ext.onReady(function () {
Ext.create('Ext.panel.Panel', {
renderTo: Ext.getBody(),
title: 'Google Map',
layout: 'fit',
width: 300,
height: 300,
items: [{
xtype: 'button',
id: 'show-btn',
text: 'Click here'
}]
});
Ext.require([
'Ext.window.*',
'Ext.ux.GMapPanel'
]);
var mapwin;
Ext.get('show-btn').on('click', function () {
// create the window on the first click and reuse on subsequent clicks
if (mapwin) {
mapwin.show();
} else {
mapwin = Ext.create('Ext.window.Window', {
autoShow: true,
layout: 'fit',
title: 'GMap Window',
closeAction: 'hide',
width: 450,
height: 450,
border: false,
x: 40,
y: 60,
mapTypeId: 'google.maps.MapTypeId.ROADMAP',
mapConfOpts: ['enableScrollWheelZoom', 'enableDoubleClickZoom', 'enableDragging'],
mapControls: ['GSmallMapControl', 'GMapTypeControl', 'NonExistantControl'],
items: {
xtype: 'gmappanel',
center: {
geoCodeAddr: '4 Yawkey Way, Boston, MA, 02215-3409, USA',
marker: {title: 'Fenway Park'}
},
markers: [{
lat: 42.339641,
lng: -71.094224,
title: 'Boston Museum of Fine Arts',
listeners: {
click: function (e) {
Ext.Msg.alert('It\'s fine', 'and it\'s art.');
}
}
}, {
lat: 42.339419,
lng: -71.09077,
title: 'Northeastern University'
}]
}
});

}
});
});

GMapPanel.js

    Ext.define('Ext.ux.GMapPanel', {
extend: 'Ext.panel.Panel',

alias: 'widget.gmappanel',

requires: ['Ext.window.MessageBox'],

initComponent : function(){
Ext.applyIf(this,{
plain: true,
gmapType: 'map',
border: false
});

this.callParent();
},

onBoxReady : function(){
var center = this.center;
this.callParent(arguments);

if (center) {
if (center.geoCodeAddr) {
this.lookupCode(center.geoCodeAddr, center.marker);
} else {
this.createMap(center);
}
} else {
Ext.raise('center is required');
}

},

createMap: function(center, marker) {
var options = Ext.apply({}, this.mapOptions);

options = Ext.applyIf(options, {
zoom: 14,
center: center,
mapTypeId: 'google.maps.MapTypeId.HYBRID'
});
this.gmap = new google.maps.Map(this.body.dom, options);
if (marker) {
this.addMarker(Ext.applyIf(marker, {
position: center
}));
}

Ext.each(this.markers, this.addMarker, this);
this.fireEvent('mapready', this, this.gmap);
},

addMarker: function(marker) {
marker = Ext.apply({
map: this.gmap
}, marker);

if (!marker.position) {
marker.position = new google.maps.LatLng(marker.lat, marker.lng);
}
var o = new google.maps.Marker(marker);
Ext.Object.each(marker.listeners, function(name, fn){
google.maps.event.addListener(o, name, fn);
});
return o;
},

lookupCode : function(addr, marker) {
this.geocoder = new google.maps.Geocoder();
this.geocoder.geocode({
address: addr
}, Ext.Function.bind(this.onLookupComplete, this, [marker], true));
},

onLookupComplete: function(data, response, marker){
if (response != 'OK') {
Ext.MessageBox.alert('Error', 'An error occured: "' + response + '"');
return;
}
this.createMap(data[0].geometry.location, marker);
},

afterComponentLayout : function(w, h){
this.callParent(arguments);
this.redraw();
},

redraw: function(){
var map = this.gmap;
if (map) {
google.maps.event.trigger(map, 'resize');
}
}

});

最佳答案

在此网站上获取 key https://developers.google.com/maps/documentation/javascript/

之后,您必须包含 map url,就像 url key=您在 index.html 中的键一样的参数
然后就可以工作了。

网址如下所示
https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap

您必须将其放入 <script></script> 内标签。

关于javascript - Uncaught ReferenceError : google is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39724486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com