Javascript infowindow谷歌地图错误:can';t访问词汇声明';信息窗口&x27;
我想画一张50个州的叶绿素图。 每个州都有一个潜在的信息窗口 问题是,当用户单击某个状态时,会打开一个信息窗口。挑战是在单击另一个状态时关闭它。问题出在下面的代码中,我甚至无法打开第一个信息窗口 我得到的错误是:Javascript infowindow谷歌地图错误:can';t访问词汇声明';信息窗口&x27;,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我想画一张50个州的叶绿素图。 每个州都有一个潜在的信息窗口 问题是,当用户单击某个状态时,会打开一个信息窗口。挑战是在单击另一个状态时关闭它。问题出在下面的代码中,我甚至无法打开第一个信息窗口 我得到的错误是:uncaughtreferenceerror:无法在初始化之前访问词法声明“infowindow” "use strict"; let map; function initMap() { map = new google.maps.Map(document.
uncaughtreferenceerror:无法在初始化之前访问词法声明“infowindow”
"use strict";
let map;
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
zoom: 6,
center: {
lat: 28,
lng: -85
}
}); // Load GeoJSON.
map.data.loadGeoJson(
//"https://storage.googleapis.com/mapsdevsite/json/google.json"
"maps/US_ALL_STATES.geojson"
); // Color each letter gray. Change the color when the isColorful property
// is set to true.
map.data.setStyle(feature => {
let color = "blue";
if (feature.getProperty("isColorful")) {
color = feature.getProperty("color");
}
return (
/** @type {!google.maps.Data.StyleOptions} */
({
fillColor: color,
strokeColor: color,
strokeWeight: 2
})
);
}); // When the user clicks, set 'isColorful', changing the color of the letters.
map.data.addListener("click", event => {
if (infowindow) infowindow.close();
var content = "State: " + event.feature.getProperty("NAME") + "\r"
content = content + "Click <a href='county.html?state="+event.feature.getProperty("STATEFP")+"'>here</a> to Zoom in"
const infowindow = new google.maps.infowindow({
content: content
});
event.feature.setProperty("fillColor", "pink");
infowindow.setPosition(event.latLng);
infowindow.open(map);
console.log(event.feature);
}); // When the user hovers, tempt them to click by outlining the letters.
// Call revertStyle() to remove all overrides. This will use the style rules
// defined in the function passed to setStyle()
map.data.addListener("mouseover", event => {
map.data.revertStyle();
map.data.overrideStyle(event.feature, {
strokeWeight: 8,
fillColor: "green",
strokeColor: "pink"
});
});
map.data.addListener("mouseout", event => {
map.data.revertStyle();
});
}
“严格使用”;
让地图;
函数initMap(){
map=new google.maps.map(document.getElementById(“map”){
缩放:6,
中心:{
拉丁美洲:28,
液化天然气:-85
}
});//加载GeoJSON。
map.data.loadGeoJson(
//"https://storage.googleapis.com/mapsdevsite/json/google.json"
“maps/US\u ALL\u STATES.geojson”
);//将每个字母涂成灰色。当isColorful属性
//设置为true。
map.data.setStyle(功能=>{
让color=“蓝色”;
if(feature.getProperty(“isColorful”)){
颜色=feature.getProperty(“颜色”);
}
返回(
/**@type{!google.maps.Data.StyleOptions}*/
({
fillColor:color,
strokeColor:颜色,
冲程重量:2
})
);
});//当用户单击时,设置“isColorful”,更改字母的颜色。
map.data.addListener(“单击”,事件=>{
if(infowindow)infowindow.close();
var content=“State:”+event.feature.getProperty(“名称”)+“\r”
内容=内容+“单击以放大”
const infowindow=new google.maps.infowindow({
内容:内容
});
event.feature.setProperty(“fillColor”、“pink”);
infowindow.setPosition(event.latLng);
打开(地图);
console.log(event.feature);
});//当用户悬停时,通过列出字母的轮廓来引诱他们单击。
//调用revertStyle()以删除所有重写。这将使用样式规则
//在传递给setStyle()的函数中定义
map.data.addListener(“鼠标悬停”,事件=>{
map.data.revertStyle();
map.data.overrideStyle(event.feature{
冲程重量:8,
填充颜色:“绿色”,
strokeColor:“粉色”
});
});
map.data.addListener(“mouseout”,事件=>{
map.data.revertStyle();
});
}
如何更新它使其成为“一次一个信息窗口”场景?谢谢。改为在持久外部作用域中创建变量:
let infowindow;
map.data.addListener("click", event => {
if (infowindow) infowindow.close();
var content = "State: " + event.feature.getProperty("NAME") + "\r"
content = content + "Click <a href='county.html?state="+event.feature.getProperty("STATEFP")+"'>here</a> to Zoom in"
infowindow = new google.maps.infowindow({
content: content
});
// etc
let-infowindow;
map.data.addListener(“单击”,事件=>{
if(infowindow)infowindow.close();
var content=“State:”+event.feature.getProperty(“名称”)+“\r”
内容=内容+“单击以放大”
infowindow=新建google.maps.infowindow({
内容:内容
});
//等
现在的错误:未捕获引用错误:在初始化之前无法访问词法声明“infowindow”您确定要从代码中删除常量infowindow
吗?准确复制我的代码-不应该有这样的错误,因为infowindow
现在在外部范围中声明,而不是在内部范围中。确定。错误检查nged:google.maps.infowindow不是一个构造器,或者在试图使用google maps的infowindow
时复制了它。如果是这样,您需要使用正确的大写字母。.infowindow()
。否则,请查看文档,了解您试图使用的infowindow
的定义。