Wednesday 15 June 2011

javascript - function not being called first time script runs -


I'm new to JavaScript and I have an initial catch on the concepts. I have a function that is one km from the redness It appears on this screen (some clustering part of this function was supplied by a person at S / O), it works perfectly for all the blankets to be loaded except for the first time.

I'm sure this problem is something with variables and its scope, but for my life, I do not know where or how I am getting the error, I will improve the code in a great way But just useful for the improvement of my understanding (or lack of there).

Many thanks in the editor

Here is the code

Edit 1 ) I have changed the function getlabel several times and the changes have only been shown on the Kembs The first AJAX call shown was loaded out of the call, I understand why this is not happening for the life of this happening. This can be a relevant issue, though it is beyond my understanding of the subject.

   var tripid = 1; Var myStyles; Var ceramidad; Var navigate = true; Var edit = false; Vector vectors; Var polycontroll; Var bound = false; Var McCloster; Var label = ""; $ (Document) .ready (function () {$ .ajax ({type: "POST", url: "temp.php", datatype: "json", error: function (e) {warning ('error:' + E);}, success: function (data) {if (data [0] === "no") {window.location = "http://www.g4ema.com/index.html";} maxlat = data [0]; Makslon = data [1]; Minlat = data [2]; Minlon = data [3]; Tripid = Parsaint (data [4]); var Boksks = new Open Leyrs. bounce (); Bibiaks.andend ( 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 (); mycluste R = New OpenLayers.Strategy.Cluster ({limit: 2, // single groups are shown as mustCluster features: function (cluster, feature) {if (feature.geometry.CLASS_NAME === "OpenLayers .Geometry.Point "& amp; & amp; cluster.cluster [0] .geometry.CLASS_NAME ===" OpenLayers.Geometry.Point ") {return OpenLayers.Strategy.Cluster.prototype.shouldCluster.apply (this, arguments);} Else { Return false;}}}) var layer = new open layer Layer.Google ("Go 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: "# 003,399", fillOpacity: 1, labelYOffset: 15, pointRadius: 4, label: "$ {getLabel}", fontColor: "# ff0000"}, {reference: {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;} and {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 ({parameters : {tripid: tripid}, url: "kml2.php", readWithPOST: true, // {user id: user ID, tripid: tripid}, format: new OpenLayers.Format.KML ({extractStyles: true, extractAttributes : truth}) })}); Map.addLayer (kmlLayer); Var clat = (paraslot (minalat) + purphhot (max list)) / 2; Var clone = (paraslot (minnl) + parsflot (maxlon)) / 2; Var Lonat = New OpenLayer Lonatat (Clone, Claat). Transformform (new open layer projection ("EPSG: 4326"), new open layer projection ("EPSG: 900913")); Map.setCenter (lonlat); Map.zoomTo (15);  

First of all, I do not get the benefit of declaring label variables shared by you Global Realm in the context of the code made since you are returning a label from the getLabel function, so I think you only have to declare the var label , at the top of the getLabel function and with that function that local variables Return the value of

Second, the only way I can find that "undefined" will be returned with GetLabel if f.attributes.label is undefined. I'll try a code block such as:

 } else {// is not cluster if (f.attributes.label! = Null & amp; amp; amp; amp ; Amp; amp; amp; amp; and; "undefined") {// if (f.attributes.label) {// Optional if simple; statement label = "" + f.attributes.label;} else {label = ""; }}    

No comments:

Post a Comment