Google Maps API -- Setting Icon for Marker Results in Error: "constructor Error cannot be invoked without 'new'"
Google Maps API -- Setting Icon for Marker Results in Error: "constructor Error cannot be invoked without 'new'"
我正在尝试使用 Google 地图 API 创建一些要添加的标记,但是如果我尝试指定图标,我会收到错误消息:"constructor Error cannot be invoked without 'new'"。
我是 运行 通过 locahost。
我尝试过的东西:
- 创建标记,然后设置图标。
- 不同版本API。
- 将我的 API 密钥插入 JSFiddle。 (没有错误。)
- Removing/commenting出图标线。 (使用默认图标运行良好。)
代码几乎是从 Google 提供的 JSFiddle 中逐字记录的,它在那里工作,但不在我正在处理的页面上。
const latlng = new google.maps.LatLng(this.location[0], this.location[1]);
this.marker = new google.maps.Marker({
position: latlng,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 10
},
draggable: false
});
我应该在指定位置有一个圆圈图标,但我却得到以下内容:
js?v=weekly&key=(APIKEY):53 Uncaught TypeError: Class constructor Error cannot be invoked without 'new'
at new Yc (:53)
at Object._.$c (:53)
at :56
at :56
at :56
at _.nf.setIcon (:66)
at .nf..T.setValues (:165)
at _.nf.mf [as constructor] (:79)
at new _.nf (:79)
at User.createMarker (classes.js:329)
在我看来 API 的代码中存在问题,导致难以调试触发错误的实际问题。
如有任何建议,我们将不胜感激。
你把这个地方放在 class 中了吗?我将官方 JSFiddle 更改为使用 class 并且它有效。
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {
lat: -25.363882,
lng: 131.044922
}
});
var Location = class Location {
constructor(lat, lng) {
this.location = [lat, lng]
const latlng = new google.maps.LatLng(this.location[0], this.location[1]);
this.marker = new google.maps.Marker({
position: latlng,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 10
},
draggable: false
});
this.marker.setMap(map);
}
}
var location = new Location(-25.363882, 131.044922);
}
发生的事情是 API 正在调用 setIcon 函数链中的全局错误对象(尽管显然完全没有错误),由于我重写而导致错误它带有自定义错误 class 我曾天真地尝试添加到项目中。
错误信息:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
我正在尝试使用 Google 地图 API 创建一些要添加的标记,但是如果我尝试指定图标,我会收到错误消息:"constructor Error cannot be invoked without 'new'"。
我是 运行 通过 locahost。
我尝试过的东西:
- 创建标记,然后设置图标。
- 不同版本API。
- 将我的 API 密钥插入 JSFiddle。 (没有错误。)
- Removing/commenting出图标线。 (使用默认图标运行良好。)
代码几乎是从 Google 提供的 JSFiddle 中逐字记录的,它在那里工作,但不在我正在处理的页面上。
const latlng = new google.maps.LatLng(this.location[0], this.location[1]);
this.marker = new google.maps.Marker({
position: latlng,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 10
},
draggable: false
});
我应该在指定位置有一个圆圈图标,但我却得到以下内容:
js?v=weekly&key=(APIKEY):53 Uncaught TypeError: Class constructor Error cannot be invoked without 'new'
at new Yc (:53)
at Object._.$c (:53)
at :56
at :56
at :56
at _.nf.setIcon (:66)
at .nf..T.setValues (:165)
at _.nf.mf [as constructor] (:79)
at new _.nf (:79)
at User.createMarker (classes.js:329)
在我看来 API 的代码中存在问题,导致难以调试触发错误的实际问题。
如有任何建议,我们将不胜感激。
你把这个地方放在 class 中了吗?我将官方 JSFiddle 更改为使用 class 并且它有效。
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: {
lat: -25.363882,
lng: 131.044922
}
});
var Location = class Location {
constructor(lat, lng) {
this.location = [lat, lng]
const latlng = new google.maps.LatLng(this.location[0], this.location[1]);
this.marker = new google.maps.Marker({
position: latlng,
icon: {
path: google.maps.SymbolPath.CIRCLE,
scale: 10
},
draggable: false
});
this.marker.setMap(map);
}
}
var location = new Location(-25.363882, 131.044922);
}
发生的事情是 API 正在调用 setIcon 函数链中的全局错误对象(尽管显然完全没有错误),由于我重写而导致错误它带有自定义错误 class 我曾天真地尝试添加到项目中。
错误信息:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error