我有一個PolyLine,我在地圖上點擊繪製。這工作正常,但由於某種原因,我無法將其從地圖上隱藏起來。刪除和其他一切正常。我嘗試了幾乎所有從setVisible到setMap(null)的東西,就像你可以在下面的代碼中一樣。無法從地圖隱藏PolyLine
var drawOnMap = function(){
var poly = null;
var path;
var encodedString;
var decodedString;
function drawPolyLine(latLng){
if(!poly){
path = new google.maps.MVCArray();
}
console.log(latLng);
path.push(latLng);
poly = new google.maps.Polyline({
path: path,
strokeColor: '#000000',
strokeOpacity: 1.0,
strokeWeight: 3,
editable: true,
map: map
});
}
function getPolyLineData(){
//console.log(path.getPath());
var firstPoint = path.getAt(0);
var lastPoint = path.getAt(path.length - 1);
console.log(firstPoint);
console.log(lastPoint);
if ($j("#useWaypoints").is(":checked")) {
var twaypoints = path.slice(1, path.length - 1);
var waypoints = [];
for (var i = 0; i < twaypoints.length; i++) {
waypoints.push(
{
location: new google.maps.LatLng(twaypoints[i].lat(), twaypoints[i].lng())
}
);
}
} else {
waypoints = null;
}
return data = {
origin: {lat: firstPoint.lat(), lng: firstPoint.lng()},
destination: {lat: lastPoint.lat(), lng: lastPoint.lng()},
waypoints: waypoints
};
}
function removePolyLine(){
for(var i = path.length; i > 0 ; i--){
path.pop();
}
}
function removeLastPoint(){
if(path.length > 0){
path.pop();
poly.setPath(path);
}
}
function hidePolyLine(){
console.log("HIDE");
console.log(poly.getVisible());
poly.getVisible() ? poly.setVisible(false) : poly.setVisible(true);
poly.setMap(null);
}
function showPolyLine(){
console.log("SHOW");
poly.setVisible(true);
poly.setMap(map);
}
return {
drawPolyLine: drawPolyLine,
getPolyLineData: getPolyLineData,
removeLastPoint: removeLastPoint,
removePolyLine: removePolyLine,
showPolyLine: showPolyLine,
hidePolyLine: hidePolyLine
}
}();
有人可以告訴我,爲什麼這不起作用。根據Google Maps API,我是對的。我也發現了幾個關於這個話題的SO問題,但沒有爲我工作。
要更具體些。這部分爲什麼不起作用?我不看它的任何理由...
poly.getVisible() ? poly.setVisible(false) : poly.setVisible(true);
編輯:I'm呼籲通過單選按鈕隱藏/顯示功能
$j("input[name='editDraw']").change(function() {
console.log("editDraw change");
if ($j("#editDraw_on").is(":checked")) {
drawOnMap.showPolyLine();
};
if ($j("#editDraw_off").is(":checked")) {
//console.log("OFF");
drawOnMap.hidePolyLine();
};
});
該代碼是圍繞使用揭示模塊模式編輯對象。該對象正在運行,因爲它僅用於測試地圖實例和單擊事件來調用drawPolyLine(latLng)。
如果你需要某事來幫助我,讓我知道。或者,如果問題太難回答,請讓我知道。我不明白 – BayLife
你從哪裏調用'hidePolyLine'? – duncan
@duncan我已經更新了我的Q.這是通過單選按鈕完成的。這些功能絕對稱爲! – BayLife