对象内的表达式-Javascript
从我读到的内容来看,我认为这是不可能的,但是在下面的代码中是否有检查我的映射标记是否为null的方法?我读了一些关于原型的东西,但我不确定这是否是我需要使用的。代码如下:对象内的表达式-Javascript,javascript,google-maps,object,this,expression,Javascript,Google Maps,Object,This,Expression,从我读到的内容来看,我认为这是不可能的,但是在下面的代码中是否有检查我的映射标记是否为null的方法?我读了一些关于原型的东西,但我不确定这是否是我需要使用的。代码如下: this.currentLocation = function(latitude, longitude) { if (loc_marker != null) { loc_marker.setPosition(new google.maps.LatLng(latitude, longitude);
this.currentLocation = function(latitude, longitude) {
if (loc_marker != null) {
loc_marker.setPosition(new google.maps.LatLng(latitude, longitude);
}
else {
var loc_marker = new google.maps.Marker({
position: new google.maps.LatLng(latitude, longitude),
title: "Here"
});
loc_marker.setMap(map);
}
};
每次位置改变时,我都试图在谷歌地图上移动一个标记。我想检查是否设置了标记,如果设置了标记,我将使用setPosition()更改位置
如果你有任何建议,我将非常感谢 而不是
if (loc_marker != null) {
试一试
而不是
if (loc_marker != null) {
试一试
您必须将
loc_marker
设置为“全局”变量,即每次调用函数时,该变量都指向同一对象。在上面的代码中,您只需要在函数的作用域中创建另一个局部变量。尝试以下方法:
var loc_marker = null;
this.currentLocation = function(latitude, longitude) {
if (loc_marker != null) {
loc_marker.setPosition(new google.maps.LatLng(latitude, longitude);
}
else {
loc_marker = new google.maps.Marker({
position: new google.maps.LatLng(latitude, longitude),
title: "Here"
});
loc_marker.setMap(map);
}
};
您必须将
loc_marker
设置为“全局”变量,即每次调用函数时,该变量都指向同一对象。在上面的代码中,您只需要在函数的作用域中创建另一个局部变量。尝试以下方法:
var loc_marker = null;
this.currentLocation = function(latitude, longitude) {
if (loc_marker != null) {
loc_marker.setPosition(new google.maps.LatLng(latitude, longitude);
}
else {
loc_marker = new google.maps.Marker({
position: new google.maps.LatLng(latitude, longitude),
title: "Here"
});
loc_marker.setMap(map);
}
};
以下所有内容都将满足您的要求
if (loc_marker != null) { //Tests for null and for undefined
if (loc_marker === null) { //Tests for null
if (typeof loc_marker === 'object' && loc_marker === null) { //Another null test, albeit redundant
if (typeof loc_marker === 'undefined') { //Tests for undefined
if (loc_marker) { //Test for a defined non-null value
也就是说,loc\u标记实际定义在哪里?它看起来“像”您正试图在else块()中定义它
除了要声明的loc\u marker
,您拥有的代码应该可以正常工作
希望这有帮助
皮特以下所有内容都可以满足您的需求
if (loc_marker != null) { //Tests for null and for undefined
if (loc_marker === null) { //Tests for null
if (typeof loc_marker === 'object' && loc_marker === null) { //Another null test, albeit redundant
if (typeof loc_marker === 'undefined') { //Tests for undefined
if (loc_marker) { //Test for a defined non-null value
也就是说,loc\u标记实际定义在哪里?它看起来“像”您正试图在else块()中定义它
除了要声明的loc\u marker
,您拥有的代码应该可以正常工作
希望这有帮助
皮特在哪里定义标记似乎是我最大的问题。我不知道在哪里申报。如果我在表达式之外执行此操作,则每次都会创建一个新标记。我只需要做一个标记,然后每次位置改变时都改变它的位置。我想你已经接近解决这个问题了。关于如何/在何处声明它有什么建议吗?如果您只需要一个标记,我会将其作为this
的一部分,因此在与this.currentLocation
相同的范围内,添加this.locu marker={}代码>。这样,您就可以在函数中引用这个.loc_标记
,而不仅仅是loc_标记
。标记的定义位置似乎是我最大的问题。我不知道在哪里申报。如果我在表达式之外执行此操作,则每次都会创建一个新标记。我只需要做一个标记,然后每次位置改变时都改变它的位置。我想你已经接近解决这个问题了。关于如何/在何处声明它有什么建议吗?如果您只需要一个标记,我会将其作为this
的一部分,因此在与this.currentLocation
相同的范围内,添加this.locu marker={}代码>。这样,您就可以在函数中引用this.loc\u marker
,而不仅仅是loc\u marker
。