Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why won't my second group of data show in this javascript loop when i document.write()

Status
Not open for further replies.

primorudy

Programmer
Jul 30, 2012
16
US
JavaScript:
var method = "39.017653,-77.38967506;39.0296,-77.400312;39.025008,-77.392108;39.026076,-77.391247;39.039701,-77.385149;39.042511,-77.384622;39.044917,-77.376168;39.044431,-77.363503;39.042494,-77.36261;39.040363,-77.363011;39.029778,-77.366968;39.027949,-77.368974;39.026714,-77.369321;39.020609,-77.368007;39.020209,-77.365907;39.020209,-77.365907;39.015409,-77.370807;39.015409,-77.370807;39.012095,-77.373905;38.991771,-77.3936;38.991072,-77.394355;38.982709,-77.402509;38.982709,-77.402509;38.988954,-77.408699;38.991142,-77.412171;38.993777,-77.418058;38.992509,-77.420009;38.991814,-77.419249;38.991726,-77.41711;38.989543,-77.415239;38.988617,-77.419776;38.989666,-77.428636;39.004009,-77.43391;39.005716,-77.428903;39.007709,-77.418509;39.022422,-77.407153;39.024145,-77.405407;39.026734,-77.40095;39.0296,-77.400312;";

var method2 = "38.8967799,-77.3283754;38.908811,-77.360907;38.910312,-77.359908;38.911312,-77.356208;38.909411,-77.352507;38.911811,-77.347907;38.913011,-77.343807;38.913411,-77.338307;38.916611,-77.328606;38.920312,-77.322807;38.922112,-77.322507;38.922112,-77.318232;38.922112,-77.318232;38.922112,-77.317507;38.920712,-77.316407;38.917011,-77.316406;38.915511,-77.318706;38.911111,-77.318206;38.910411,-77.317006;38.910311,-77.311006;38.910711,-77.310206;38.909711,-77.303305;38.902711,-77.303406;38.897711,-77.304406;38.892011,-77.304306;38.889911,-77.304106;38.885811,-77.301106;38.881011,-77.301006;38.880411,-77.294705;38.875211,-77.287705;38.875211,-77.287705;38.872322,-77.291809;38.872322,-77.291809;38.869885,-77.303265;38.869012,-77.309106;38.873223,-77.308738;38.875387,-77.307438;38.877211,-77.305606;38.874711,-77.313606;38.873012,-77.316406;38.866812,-77.322106;38.865239,-77.330357;38.864312,-77.336507;38.864312,-77.336507;38.871459,-77.336417;38.873512,-77.339207;38.871812,-77.340807;38.871212,-77.343707;38.871886,-77.34613;38.873312,-77.348207;38.875512,-77.353707;38.876812,-77.358907;38.879812,-77.367308;38.881312,-77.368908;38.885111,-77.363008;38.890111,-77.352207;38.893711,-77.350207;38.904111,-77.357507;38.904111,-77.357507;38.908811,-77.360907;";

var places = [method, method2];
var splitPlaces;
var cityOverLays = new Array();

function create() {

    for (var i = 0; i <= places.length; i++) {
            var splitPlaces = places[i].split(";");

        for (var a = 0; a <= splitPlaces.length; a++) {
            var xyCoords = splitPlaces[a].split(",");
            document.write(xyCoords + "<br />");
        }
    }


}

create();​
 
Hi

Maybe is related to the logic error or looping one time more that the count of array elements ?

Replace the [tt]<=[/tt] with [tt]<[/tt] in the [tt]for[/tt] conditions.

Feherke.
[link feherke.github.com/][/url]
 
I agree.
At the end of the first run through the loop, it tries to read a nonexistent index from splitPlaces so the JS execution stops there with an error:


Error: TypeError: splitPlaces[a] is undefined
Source File: Line: 17

Why because the positions start at zero, so x.length is always one more than the last index in the array.

A length of 3 means the array contains index 0, 1 ad 2, but not 3.

So if splitPlaces is 40 in length, it stops at index 39.

I suggest you use the Error Console in your browser to pick out these errors.


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
even if i change it to jsut "<" it only writes the first group of data. Should the first loop pick back up again because the length of places is 2?
 
i got it to work by removing the last ";" in each variable - but it still says an error of "Uncaught TypeError: Cannot call method 'split' of undefined "

why would it work if it says this error? Do I have something to worry about or can i ignore this?
 
basically what i need to know is why is this only drawing one polygon on my google maps when it should be drawing two?

JavaScript:
<html>
<head>
<script type="text/javascript"
      src="[URL unfurl="true"]http://maps.googleapis.com/maps/api/js?sensor=true">[/URL]
    </script>
	
<script type="text/javascript">

var centerofmap = new google.maps.LatLng(38.7233, -77.5367);
var map;
var myOptions; 
function initialize() {
	var myOptions = {
		center: centerofmap,
		zoom: 10,
		mapTypeId: google.maps.MapTypeId.ROADMAP
		};
	var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
	
	create();
	
	
	}
		



var method = "38.74884248,-77.55642311;38.782815,-77.564612;38.7813,-77.5593;38.775313,-77.55119;38.77296,-77.542959;38.766829,-77.539732;38.766034,-77.538661;38.765936,-77.537141;38.760696,-77.533914;38.756904,-77.532714;38.751815,-77.535312;38.746955,-77.535663;38.745416,-77.531711;38.742316,-77.531911;38.738216,-77.530411;38.738216,-77.530411;38.737616,-77.533111;38.726516,-77.531311;38.72926,-77.52668;38.727768,-77.52567;38.724576,-77.525414;38.724517,-77.524893;38.723875,-77.524897;38.722617,-77.521412;38.717916,-77.524211;38.717716,-77.521111;38.715516,-77.515711;38.716225,-77.513317;38.707956,-77.505511;38.707956,-77.505511;38.707518,-77.505911;38.700718,-77.503911;38.69602,-77.501929;38.69602,-77.501929;38.697085,-77.504918;38.695979,-77.50733;38.693517,-77.51141;38.686717,-77.51751;38.696817,-77.534611;38.703017,-77.533511;38.705217,-77.539711;38.706073,-77.544401;38.704463,-77.546958;38.703112,-77.550935;38.707291,-77.556552;38.712117,-77.561711;38.716017,-77.561211;38.717017,-77.560511;38.731516,-77.547711;38.733116,-77.559712;38.732663,-77.576221;38.736099,-77.574855;38.740319,-77.571354;38.741218,-77.569395;38.740418,-77.5655;38.739576,-77.564239;38.739921,-77.563728;38.742716,-77.562712;38.747116,-77.575312;38.751416,-77.580312;38.754316,-77.590312;38.754916,-77.595312;38.757696,-77.596962;38.758178,-77.596839;38.759736,-77.600633;38.760294,-77.608139;38.759845,-77.608774;38.758335,-77.609267;38.754986,-77.609513;38.754861,-77.611481;38.755761,-77.61507;38.758902,-77.618823;38.761311,-77.619541;38.763216,-77.622313;38.763216,-77.622313;38.775715,-77.610513;38.783615,-77.606213;38.786916,-77.603814;38.797314,-77.602213;38.782584,-77.575285;38.786644,-77.571032;38.782815,-77.564612";

var method2 = "38.74884248,-77.55642311;38.782815,-77.564612;38.7813,-77.5593;38.775313,-77.55119;38.77296,-77.542959;38.766829,-77.539732;38.766034,-77.538661;38.765936,-77.537141;38.760696,-77.533914;38.756904,-77.532714;38.751815,-77.535312;38.746955,-77.535663;38.745416,-77.531711;38.742316,-77.531911;38.738216,-77.530411;38.738216,-77.530411;38.737616,-77.533111;38.726516,-77.531311;38.72926,-77.52668;38.727768,-77.52567;38.724576,-77.525414;38.724517,-77.524893;38.723875,-77.524897;38.722617,-77.521412;38.717916,-77.524211;38.717716,-77.521111;38.715516,-77.515711;38.716225,-77.513317;38.707956,-77.505511;38.707956,-77.505511;38.707518,-77.505911;38.700718,-77.503911;38.69602,-77.501929;38.69602,-77.501929;38.697085,-77.504918;38.695979,-77.50733;38.693517,-77.51141;38.686717,-77.51751;38.696817,-77.534611;38.703017,-77.533511;38.705217,-77.539711;38.706073,-77.544401;38.704463,-77.546958;38.703112,-77.550935;38.707291,-77.556552;38.712117,-77.561711;38.716017,-77.561211;38.717017,-77.560511;38.731516,-77.547711;38.733116,-77.559712;38.732663,-77.576221;38.736099,-77.574855;38.740319,-77.571354;38.741218,-77.569395;38.740418,-77.5655;38.739576,-77.564239;38.739921,-77.563728;38.742716,-77.562712;38.747116,-77.575312;38.751416,-77.580312;38.754316,-77.590312;38.754916,-77.595312;38.757696,-77.596962;38.758178,-77.596839;38.759736,-77.600633;38.760294,-77.608139;38.759845,-77.608774;38.758335,-77.609267;38.754986,-77.609513;38.754861,-77.611481;38.755761,-77.61507;38.758902,-77.618823;38.761311,-77.619541;38.763216,-77.622313;38.763216,-77.622313;38.775715,-77.610513;38.783615,-77.606213;38.786916,-77.603814;38.797314,-77.602213;38.782584,-77.575285;38.786644,-77.571032;38.782815,-77.564612";

var places = [method, method2];
var splitPlaces;
var cityOverLays = new Array();

function create() {
	var myOptions = {
		center: centerofmap,
		zoom: 10,
		mapTypeId: google.maps.MapTypeId.ROADMAP
		};
	var map = new google.maps.Map(document.getElementById('map_canvas'), myOptions);
	var places = [method, method2];
    
		for (var i = 0; i <= places.length; i++) {
			var cityOverlays = new Array();
            var splitPlaces = places[i].split(";");

				for (var a = 0; a < splitPlaces.length; a++) {
					var xyCoords = splitPlaces[a].split(",");
					
					cityOverlays.push(new google.maps.LatLng(xyCoords[0], xyCoords[1]));
					
					
				}
		
			var polygons = new google.maps.Polygon ({
			paths: cityOverlays,
			strokeColor: "#FFFFFF",
			strokeWeight: 2,
			strokeOpacity: .8,
			fillColor: "#FF0000",
			fillOpacity: .35
			});
			
			polygons.setMap(map);
		}


}


</script>
</head>

<body onload="create();">
<div id="map_canvas" style="height: 250px; width:250px;">



</div>

</body>
</html>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top