我是JavaScript新手,我有初學者抓住概念我有一個函數,從kml獲取標籤,並將其顯示在屏幕上(該函數的一些聚類部分是由S/O上的某人提供)。它適用於除第一個以外加載的所有kmls。功能沒有被稱爲第一次腳本運行
我相信問題與變量及其範圍有關,但對於我的生活,我無法看到我在哪裏或如何得到一個錯誤,我對代碼的更正將是一個很大的幫助,但糾正我的理解(或缺乏)同樣有幫助。
在adavance
這裏許多感謝是代碼
EDIT 1)I具有轉換功能getlabel的次數和變化只看到的加載外部的第一AJAX調用的KML所示下面。我不爲他們的生活理解爲什麼會發生這種情況。它可能是一個方面的問題,但是這超出了我的話題
var tripid=1;
var myStyles;
var cfarmerid;
var navigate=true;
var edit=false;
var vectors;
var polyControl;
var bound=false;
var mycluster;
var label=" ";
$(document).ready(function(){
$.ajax({
type: "POST",url: "temp.php",dataType: "json",
error: function(e){
alert('Error: '+e);
},
success: function (data) {
if(data[0]==="not"){
window.location = "http://www.g4ema.com/index.html";
}
maxlat=data[0];
maxlon=data[1];
minlat=data[2];
minlon=data[3];
tripid=parseInt(data[4]);
var bbox=new OpenLayers.Bounds();
bbox.extend(new OpenLayers.LonLat(minlon,minlat));
bbox.extend(new OpenLayers.LonLat(maxlat,maxlon));
bbox.toBBOX();
map = new OpenLayers.Map("map");
//var layer= new OpenLayers.Layer.OSM();
mycluster = new OpenLayers.Strategy.Cluster(
{
threshold: 2, // single clusters are shown as features
shouldCluster: function(cluster, feature)
{
if (feature.geometry.CLASS_NAME === "OpenLayers.Geometry.Point" &&
cluster.cluster[0].geometry.CLASS_NAME === "OpenLayers.Geometry.Point") {
return OpenLayers.Strategy.Cluster.prototype.shouldCluster.apply(this, arguments);
} else {
return false;
}
}
});
var layer = new OpenLayers.Layer.Google(
"Google Hybrid",
{type: google.maps.MapTypeId.HYBRID, numZoomLevels: 20});
layer.wrapDateLine=false;
map.addLayer(layer);
myStyles = new OpenLayers.StyleMap({
"default": new OpenLayers.Style({
strokeColor: "#00ffff",
strokeWidth:5,
strokeOpacity:1,
fillColor:"#003399",
fillOpacity: 1,
labelYOffset: 15,
pointRadius: 4,
label:"${getLabel}",
fontColor:"#ff0000"
}, {
context: {
getLabel: function (f) {
label=" ";
if (f.cluster) { // is a cluster
if (f.cluster[0].attributes.label!==" ") {
label= " " + f.attributes.count + " " +
f.cluster[0].attributes.label;
} else {
label= " " ;//+ f.attributes.count + "init";
}
} else { // is not cluster
if (f.attributes.label!==" ") {
label= " " + f.attributes.label;
}else{
label=" ";
}
}
if(!label){label=" ";}
return label;
}
}
})
});
kmlLayer = new OpenLayers.Layer.Vector("Trip", {
styleMap: myStyles,
projection: map.displayProjection,
strategies: [new OpenLayers.Strategy.Fixed(),mycluster],
protocol: new OpenLayers.Protocol.HTTP({
params:{ tripid:tripid},
url: "kml2.php",
readWithPOST:true,
//{userid:userid,tripid:tripid},
format: new OpenLayers.Format.KML({
extractStyles: true,
extractAttributes: true
})
})
});
map.addLayer(kmlLayer);
var clat=(parseFloat(minlat)+parseFloat(maxlat))/2;
var clon=(parseFloat(minlon)+parseFloat(maxlon))/2;
var lonlat = new OpenLayers.LonLat(clon,clat).transform(new OpenLayers.Projection("EPSG:4326"),new OpenLayers.Projection("EPSG:900913"));
map.setCenter(lonlat);
map.zoomTo(15);
非常感謝你但是,對於你對此有何評論還沒有解決這個問題,整個事情有我真正打敗! –
最近兩天我一直在尋找這個問題,問題不在於函數,問題在於它沒有被調用 –