Jump to content

Passing Object out of a function


jh_dempsey

Recommended Posts

Hey All

 

Im pretty new to Javascript, but quite competant in PHP. So when things dont work like they do in PHP, i get a bit stuck!!!

 

Im creating a page which uses the Google Maps API.

 

The idea is that when i click a link, it runs the toggle_overlay() function and shows an overlay on my map.

 

The create_overlay() function creates the polygon object. The idea then is that it returns this object, and then i can add it to the map using the map.addOverlay function. The problem is, the polygon[id] variable im passing the object to appears to not be recieving the object. If i run map.addOverlay(polygon[id]), nothing happens.

 

I know that the polygon is being created ok because if i put the map.addOverlay function inside the create_overlay() function straight after the polygon object was created (ie adding map.addOverlay(poly_function) just after poly_function is created), then the overlay shows up on the map.

 

Why isnt my object being passed to the polygon[id] variable??

 

The page can be found at www.coastervideosonline.co.uk/floodmodelling/index.html

 

The following code all appears between the <head></head> tags of the page

 

<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAA35aUO1VCBYfikrJho5rctRRlQbv0yLQwDzXq89Ttj6TRJVoXfRRQIjL7jISrTKy0OXgED5SCQK5uMg"> </script>

<script type="text/javascript">

// Load the Google Maps API
google.load("maps", "2.x");

// Set the location of the XML file. Include a random parameter so the page doesnt cache
var url2 = "xml_test.xml?r=" + new Date().getTime();

// Set the map variable
var map = null;

// Set the array that will hold all the overlays
var polygon = new Array();


// Call this function when the page has been loaded
function initialize() 
{
// Create a new Google Map Object
map = new google.maps.Map2(document.getElementById("map"));

// Center the map on a certain point
map.setCenter(new google.maps.LatLng(53.869281,-1.691551), 13);

// Add the controls to the top left corner
map.addControl(new google.maps.LargeMapControl());

// Enable Zoom In/Out via Mouse Scrollwheel
map.enableScrollWheelZoom();

google.setOnLoadCallback(initialize);
}



// Read the data from xml_test.xml
function create_overlay(xmldata,i)
{
// Create an AJAX HTTP request
var request = google.maps.XmlHttp.create();

// What to do when the server replys...
request.onreadystatechange = function() 
{
    	if (request.readyState == 4) 
	{
		var xmlDoc = google.maps.Xml.parse(request.responseText);
		var coordinates = xmlDoc.getElementsByTagName("coordinates");

		// Create an array to hold the Latitude and Longitude values
		var LatLong = new Array();

 		for (var i = 0; i < coordinates.length; i++) 
		{
			// Grab the X and Y coordinates
    			var coord_x = coordinates[i].getAttribute("x");
			var coord_y = coordinates[i].getAttribute("y");
			// Add these coordinates into the LatLong array, converting to LatLng first
			LatLong[i] = new google.maps.LatLng(coord_x,coord_y);		
		}

		// Join the polygon abck to start by making the last point equal to the first point
		j = coordinates.length + 1;
		LatLong[j] = LatLong[0];

		// Create the Polygon
		var poly_function = new google.maps.Polygon(LatLong,"#f33f00", 5, 1, "#ff0000", 0.2);

		return poly_function;			
     	}
}

request.open("GET",xmldata,true);
request.send(null);

}





function toggle_overlay(id)
{

polygon[id] = new create_overlay(url2,id);
debugger;
outputEl = document.getElementById("output");
outputEl.childNodes[0].nodeValue = var_dump(polygon[id]);

map.addOverlay(polygon[id]);


}


function var_dump(obj) 
{
   if(typeof obj == "object") 
   {
      return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "")+"\nValue: " + obj;
   } 
   else 
   {
      return "Type: "+typeof(obj)+"\nValue: "+obj;
   }
}


</script>

Link to comment
https://forums.phpfreaks.com/topic/79075-passing-object-out-of-a-function/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.