2012-03-22 75 views
10

我有一個Web應用程序,它將爲每個用戶繪製一條折線(跟蹤移動),並且我想包含一些功能,允許Web應用程序用戶通過更改某個用戶的「焦點」折線的顏色。它必須首先將所有多段線更改爲紅色,然後將選定的折線更改爲藍色。我認爲最好避免專注於一條線,然後試着專注於另一條線,並讓它們都是藍色。我真的不知道如何實現這一點,但我有功能,當名稱被按下時返回用戶ID。我只需要遍歷每個對象(每個用戶的多段線)先將其更改爲紅色,然後將特定的一個更改爲藍色。以下是一些代碼。如果你能指出我正確的方向,我會很感激。謝謝。這是我的代碼的精簡版,所以我希望它提供了足夠的信息。動態更改折線顏色

function User(id) { 
this.id = id; 

this.locations = []; 

this.mark = 0; 

this.getId = function() { 
    return this.id; 
}; 

this.addLocation = function(latitude, longitude) { 
    this.locations[this.locations.length] = new google.maps.LatLng(latitude, longitude);   
}; 
var polyline; 
this.drawPolyline = function(loc) { 
     polyline = new google.maps.Polyline({ 
     map: map, 
     path: loc, 
     strokeColor: "#FF0000", 
     strokeOpacity: 1.0, 
     strokeWeight: 2 
     }); 
    polyline.setMap(map); 
}; 

this.removePolyline = function() { 
    if (polyline != undefined) { 
     polyline.setMap(null); 
     } 
    } 
this.get_user_info = function(user_id) { 

var datastr = 'id=' + user_id; 
$.ajax({  
    type: "POST", 
    url: 'user_api.php', 
    data: datastr,  
    dataType: 'json',     
    success: function(data){ 
     var phone_id = data[0]; 
     var leftDiv = document.createElement("div"); //Create left div 
     leftDiv.id = "left"; //Assign div id 
     leftDiv.setAttribute("style", "float:left; width:66.5%; line-height: 26px; text-align:left; font-size:12pt; padding-left:8px; height:26px;"); //Set div attributes 
     leftDiv.style.background = divColor; 
     //user_name = document.createTextNode(fullName + ' '); //Set user name   
     a = document.createElement('a'); 
     a.href ="javascript:setFocus('" + phone_id + "');"; 
     a.innerHTML = fullName + ' '; 
     leftDiv.appendChild(a); 
     } 
    }); 
    } 
} 

function setFocus(phone_id) { 
    alert(phone_id); 
} 
function Users() { 
this.users = {}; 

this.createUser = function(id) { 
    this.users[id] = new User(id); 
    return this.users[id]; 
}; 

this.getUser = function(id) { 
    return this.users[id];  
}; 

this.removeUser = function(id) { 
    var user = this.getUser(id); 
    delete this.users[id]; 
    return user; 
}; 
} 

var users = new Users(); 

回答

17

目前你不存儲用戶對象內部的折線,你首先應該做的是讓行後訪問:

this.drawPolyline = function(loc) { 
     this.polyline = new google.maps.Polyline({//<--note the this 
     map: map, 
     path: loc, 
     strokeColor: "#FF0000", 
     strokeOpacity: 1.0, 
     strokeWeight: 2 
     }); 
    this.polyline.setMap(map); 
}; 

現在你就能higlight一行:

Users.prototype.highlightLine=function(id) 
{ 
    for(var k in this.users) 
    { 
    this.users[k].polyline.setOptions({strokeColor:(id===k)?'blue':'red'}); 
    } 
} 

//use it 
users.highlightLine(5)//will highlight the line for user with id 5 
+0

我在哪裏放'Users.prototype ...'功能?在用戶()或用戶(ID)?我還沒有使用過原型,所以我不確定它們是如何工作的。我是否必須將第一個函數(this.polyline ...)移動到Users()?我將'this.'添加到了你所做的相同的地方,並且它應該可以正常工作。我只是不確定我將如何去移動所有這些。 – mkyong 2012-03-25 15:14:21

+0

我明白了!在我要求澄清之前,我應該嘗試過。感謝使它變得如此簡單! – mkyong 2012-03-25 15:19:37