Page 1 of 1

Javascript variable define problem -- maybe scope issue?

Posted: Thu Jul 20, 2006 10:00 am
by hydroxide
Weirdan | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I'm confused as to how to get an argument from one function to another... or how to solve my issue here. When I run this, it throws a js error "marker is undefined". Anyone have any thoughts as to how I could get this loop to work?I'm trying to pass the info into the text "bubbles" that appear when you click on the marker.

[syntax="javascript"]
var map = null;
    var geocoder = null;
	<?php
		$key = 0;
		foreach($results as $key => $contact) {
			$results[$key][javavarname] = "address$key";
			$varString = "var address$key = \"$contact[address1], $contact[city], $contact[state], $contact[zip]\";\n";
			echo $varString;
			$key++;
		}
	?>

	function markAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
            // alert(address + " not found");
            } else {
              map.setCenter(point, 7);
              var marker = new GMarker(point);
              map.addOverlay(marker);
            }
          }
        );
      }
    }
    
    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        geocoder = new GClientGeocoder();
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        
<?
		foreach($results as $contact) {
			echo "markAddress(" . $contact[javavarname] . ");\n";
			echo "GEvent.addListener(marker, \"click\", function() {\n";
			echo "marker.openInfoWindowHtml(" . $contact[javavarname] . ");\n";
			echo "});\n";
		}
?>
      }
    }

Weirdan | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Thu Jul 20, 2006 11:21 am
by RobertGonzalez
It could be in the way that PHP is being used. There are some things you could clean up a bit.

Code: Select all

$varString = "var address$key = \"$contact[address1], $contact[city], $contact[state], $contact[zip]\";\n";
Maybe do this...

Code: Select all

$varString = 'var address' . $key . ' = "' . $contact[address1].$contact[city].$contact[state].$contact[zip] . '";'; 
$varString .= "\n";
And when using arrays, make sure to enclose the index in quotes...

Code: Select all

echo "markAddress(" . $contact[javavarname] . ");\n";
echo "GEvent.addListener(marker, \"click\", function() {\n";
echo "marker.openInfoWindowHtml(" . $contact[javavarname] . ");\n";
might be better off as..

Code: Select all

echo "markAddress(" . $contact['javavarname'] . ");\n";
echo "GEvent.addListener(marker, \"click\", function() {\n";
echo "marker.openInfoWindowHtml(" . $contact['javavarname'] . ");\n";
These are just possibilities, no guarantees.

Posted: Thu Jul 20, 2006 11:43 am
by hydroxide
I'm not sure if the way the PHP is set up is the problem. I'm getting the proper output from my php file. You can see for yourself here http://myels.com/testing/jimmy/workdash.php. I'm out of ideas :(

Posted: Thu Jul 20, 2006 12:11 pm
by hydroxide
Well, now I changed it to use global variables, but it throws an error "'__e_' is null or not an object. This is really weird. You can see for yourself http://myels.com/testing/jimmy/workdash.php

Code: Select all

var map = null;
    var geocoder = null;
    var marker = null;
    var point = null;
	<?php
		$key = 0;
		foreach($results as $key => $contact) {
			$results[$key][javavarname] = "address$key";
			$varString = "var address$key = \"$contact[address1], $contact[city], $contact[state], $contact[zip]\";\n";
			echo $varString;
			$key++;
		}
	?>

    
    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        geocoder = new GClientGeocoder();
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
<?
		foreach($results as $contact) {
			echo "markAddress(" . $contact[javavarname] . ");\n";
			echo "GEvent.addListener(marker, \"click\", function() {\n";
			echo "marker.openInfoWindowHtml(" . $contact[javavarname] . ");\n";
			echo "});\n";
		}
?>
      }
    }
	function markAddress(address) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
            // alert(address + " not found");
            } else {
              map.setCenter(point, 7);
			  marker = new GMarker(point);
              map.addOverlay(marker);
            }
          }
        );
      }
    }

Posted: Thu Jul 20, 2006 11:27 pm
by feyd
Javascript :arrow: Client Side.

Posted: Fri Jul 21, 2006 6:56 am
by hydroxide
Sorry about that

Posted: Fri Jul 21, 2006 1:42 pm
by hydroxide
*sigh* fixed this finally: JS 0 | hydroxide 1

Code: Select all

var map = null;
    var geocoder = null;
	<?php
		$key = 0;
		foreach($results as $key => $contact) {
			$results[$key][javavarname] = "address$key";
			$results[$key][javavarname2] = "address$key" . "_phone";
			
			$varString = "var address$key = \"$contact[address1], $contact[city], $contact[state], $contact[zip]\";\n";
			echo $varString;

			$varString2 = "var address$key" . "_phone = \"<b>$contact[cliname]</b><br>$contact[address1]<br>$contact[zip] $contact[city], $contact[state]<br>$contact[phone]\";\n";
			echo $varString2;
			$key++;
		}
	?>

    function load() {
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        geocoder = new GClientGeocoder();
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
<?
		foreach($results as $contact) {
			echo "markAddress(" . $contact[javavarname] . ", " . $contact[javavarname2] . ");\n";
		}
?>
      }
    }

    function markAddress(address, info) {
      if (geocoder) {
        geocoder.getLatLng(
          address,
          function(point) {
            if (!point) {
            // alert(address + " not found");
            } else {
              map.setCenter(point, 7);
              var marker = new GMarker(point);
              map.addOverlay(marker);
<?
			echo "GEvent.addListener(marker, \"click\", function() {\n";
			echo "marker.openInfoWindowHtml(info);\n";
			echo "});\n";			

?>			  
            }
          }
        );
      }
    }