[Chameleon] looking for help with zoom to features bugs

Jennifer Shanks jennifer at greeninfo.org
Wed Apr 5 13:45:11 EDT 2006


Hello list,

I'm work for a non-profit organization and we're developing an 
application that helps users to find parks, campgrounds and trails 
(http://216.93.173.158/BAOSC/). The application includes a search 
widget that allows one to search by zip, city or county.  When the 
search results load, I also want the map to zoom to the extent of the 
selected features.  I added some code to the search results page 
(SearchResults.phtml) that does this.

However, there are some bugs that I've not been able to figure 
out.  I've attached the code and am hoping that someone might have a 
chance to take a look.

If you look at lines 502-504 in SearchResults.phtml you can see that 
I am setting the map extent and saving the session.  If I save and 
open the map file that is generated, I see that the map extent is 
updated but the reference map extent remains the same.  I'd like to 
figure out how to get the keymap to draw properly when the extent 
changes.  The other problem is that the map navigation tools don't 
recognize that the extent has changed.  If I attempt to zoom in after 
using the search tool, the zoom in tool is still working with the 
original map extent.

To see these bugs in action, visit the link above, search for the zip 
code 94546 and then try to zoom in on a park!

Any suggestions on how I can fix these problems would be much appreciated.

Regards,
Jennifer


****************************
Jennifer Shanks
GreenInfo Network
116 New Montgomery, Suite 738
San Francisco, CA 94105
(415) 979-0343 x 306
(415) 979-0371 FAX
**GreenInfo has completely re-done our web site
Go to: www.greeninfo.org

-------------- next part --------------
<?php
/**
  _____________________________________________________________________________
 |
 | Search Results Page
 |
 | @project     GreenInfo
 | @revision    $Id: QueryResults.phtml,v 1.8 2004/12/03 03:31:55 pspencer Exp $
 | @purpose     This is the search results page.
 | @author      William A. Bronsema, C.E.T. (wbronsema at dmsolutions.ca)
 | @copyright
 | <b>Copyright (c) 2006 DM Solutions Group Inc.</b>
 | Permission is hereby granted, free of charge, to any person obtaining a
 | copy of this software and associated documentation files (the "Software"),
 | to deal in the Software without restriction, including without limitation
 | the rights to use, copy, modify, merge, publish, distribute, sublicense,
 | and/or sell copies of the Software, and to permit persons to whom the
 | Software is furnished to do so, subject to the following conditions:
 |
 | The above copyright notice and this permission notice shall be included
 | in all copies or substantial portions of the Software.
 |
 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 | DEALINGS IN THE SOFTWARE.
 |_____________________________________________________________________________

 **/

// set session specific defines
define("LOAD_MAPSESSION", 1);
if(!defined("COMMON"))
define( "COMMON",  urldecode( $_REQUEST['common'] ) );

// include the session code to re-establish session
include_once( dirname(__FILE__)."/../session.inc.php" );

$szResultsPage = $_SESSION['gszAppWebPath'].
            'widgets/Search/ZoomFeature.phtml?common='.urlencode( COMMON ).
            '&sid='.session_id();


// init vars
$szError = '';
$szNoResultsMsg = '<b>No Results Found</b>';

// abort of any information is missing
if ( !isset( $_REQUEST['result_geom'] ) and !isset( $_REQUEST['result_geom2'] ) and !isset( $_REQUEST['result_geom3'] ) || !isset( $_REQUEST['bound_geom'] ) ||
     !isset( $_REQUEST['bound_field'] ) || !isset( $_REQUEST['bound_value'] ) )
{
    $szError = 'Invalid Search Criteria';
}

// make sure the inputs are valid
switch( $_REQUEST['bound_geom'] )
{
    // check zip code
    case 'zip_code_query':

        // cast to an int
        $_REQUEST['bound_value'] = intval( $_REQUEST['bound_value'] );
        if ( $_REQUEST['bound_value'] == 0 )
        {
            $szError .= 'Please specify a numeric value for a zip code.';
        }
        break;
}

// only proceed if no errors
if ( $szError == '' )
{
    // build SQL(s)
    
    //build variables


    
    if ($_REQUEST['bound_geom'] == 'agency_query')
    {
    	 $szTableSQL = 'select * from '.$_REQUEST['result_geom'].
         ' where LOWER('.$_REQUEST['bound_field'].')=LOWER(\''.
       	 $_REQUEST['bound_value'].'\') ORDER BY min_unit_n ASC';
    }
    else 
    {
      if ($_REQUEST['result_geom'] != "" ){
	  $szTableSQLc = 'select * from '.$_REQUEST['result_geom'].
      	  ' where intersects(the_geom,(select the_geom as my_geom from '.
      	  $_REQUEST['bound_geom'].' where LOWER('.$_REQUEST['bound_field'].
      	  ')=LOWER(\''.
       	 $_REQUEST['bound_value'].'\')))ORDER BY park_name ASC';
	    }

     if ($_REQUEST['result_geom2'] != ""){
         $szTableSQLp = 'select * from '.$_REQUEST['result_geom2'].
      	  ' where intersects(the_geom,(select the_geom as my_geom from '.
      	  $_REQUEST['bound_geom'].' where LOWER('.$_REQUEST['bound_field'].
      	  ')=LOWER(\''.
       	 $_REQUEST['bound_value'].'\')))ORDER BY min_unit_n ASC';     
            }
	    
     if ($_REQUEST['result_geom3'] != ""){
	   $szTableSQLt = 'select * from '.$_REQUEST['result_geom3'].
      	  ' where intersects(the_geom,(select the_geom as my_geom from '.
      	  $_REQUEST['bound_geom'].' where LOWER('.$_REQUEST['bound_field'].
      	  ')=LOWER(\''.
       	 $_REQUEST['bound_value'].'\')))ORDER BY moniker1 ASC';   
            }

    }

            if ($_REQUEST['bound_geom'] != 'agency_query')
            { 
                $szSQLBbox = 'SELECT xmin(env), ymin(env), xmax(env), ymax(env) FROM (SELECT extent(the_geom) AS env from (select the_geom from '.$_REQUEST['bound_geom'].' where LOWER('.$_REQUEST['bound_field'].')=LOWER(\''.$_REQUEST['bound_value'].'\')) as foo) as foo';

            } else {
		$szSQLBbox = 'SELECT xmin(env), ymin(env), xmax(env), ymax(env) FROM (SELECT extent(the_geom) AS env from ('.$szTableSQL.') as foo) as foo';
	    }
   


    // include the connection info
    include( '../connect.inc.php' );

    // open db
    $oDb = pg_connect( 'host='.PG_HOST.' dbname='.PG_DATABASE.' user='.
           PG_USER.' password='.PG_PASSWORD );
    if ( !$oDb )
    {
        exit( 'Unable to connect to the postgres database.  Postgres said:'.
            pg_last_error() );
    }


    // get the map object
    $oMap = &$oMapSession->oMap;
     $oMap->save('c:/temp/junk_orig.map');
    
    
    //zoom the map to extent of selected features
    getBbox($oDb, $szSQLBbox, $szNoResultsMsg, $oMap, $oMapSession);

    // turn off all three highlight layers
        $oLayer = $oMap->getLayerByName( 'parks_highlight' ); 
	$oLayer->set( 'status', MS_OFF ); 
	$oLayer = $oMap->getLayerByName( 'campgrounds_highlight' ); 
	$oLayer->set( 'status', MS_OFF ); 
        $oLayer = $oMap->getLayerByName( 'Trails_highlight' ); 
	$oLayer->set( 'status', MS_OFF );     

    // get the corresponding layer object
if ($_REQUEST['result_geom2'] != ""){
        $szSQL = 'the_geom from ('.$szTableSQLp.') as foo using unique gid using srid=-1';    
	// turn on highlight layer
	    
           $oLayer = $oMap->getLayerByName( 'parks_highlight' );
            $oLayer->set( 'status', MS_ON );

            // set the data string
            $oLayer->set( 'data', $szSQL);
	    $szTitle="Park";
	        $szTable = buildTable( $oDb, $szTableSQLp, $szTitle, $szFindWhat, $szResultsPage); 
}
if ($_REQUEST['result_geom'] != ""){
        // process campgrounds
	     $szSQL = 'the_geom from ('.$szTableSQLc.') as foo using unique gid using srid=-1';
            // turn on highlight layer
            $oLayer = $oMap->getLayerByName( 'campgrounds_highlight' );
            $oLayer->set( 'status', MS_ON );

            // set the data string
            $oLayer->set( 'data', $szSQL );
	    $szTitle="Campground";
	        $szTable2 = buildTable( $oDb, $szTableSQLc, $szTitle, $szFindWhat, $szResultsPage); 
}

if ($_REQUEST['result_geom3'] != ""){
        // process trails
	     $szSQL = 'the_geom from ('.$szTableSQLt.') as foo using unique gid using srid=-1';
            // turn on highlight layer
            $oLayer = $oMap->getLayerByName( 'Trails_highlight' );
            $oLayer->set( 'status', MS_ON );
            // set the data string
            $oLayer->set( 'data', $szSQL );
	    $szTitle="Regional Trail";
	    $szTable3 = buildTable( $oDb, $szTableSQLt, $szTitle, $szFindWhat, $szResultsPage); 
}



    

     
    
    // run the table query on the postgres db and build table
    //$szTable = buildTable( $oDb, $szTableSQL, $szSQLBbox, $szNoResultsMsg, $oMap);       

    // close the db
    pg_close( $oDb );
    
}
else
{
    $szTable = $szNoResultsMsg;
}

/**
  _____________________________________________________________________________
 |
 |  buildTable()
 |
 |  Postcondition:  This function executes the give SQL and builds the
 |                  appropriate table;
 |
 |  @param $oDB object - database object.
 |  @param $szSQL string - SQL to run.
 |  @param $szNoResultsMsg string - message to return if no results.
 |  @return string - HTML string of table.
 |  @desc Build table from given SQL.
 |_____________________________________________________________________________

 **/

function buildTable( $oDb, $szSQL, $szTitle, $szFindWhat, $szResultsPage)
{
    // init
    $szReturn = "<table class=\"layoutTable\" width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"2\">\n";

    // execute the query
    $oResult = @pg_query( $oDb, $szSQL);
       if ( !$oResult )
    {
       return( pg_last_error() );
    }

    // check for nothing found
    $nCount = pg_num_rows( $oResult );
    if ( $nCount == 0 )
    {
       $szNoResultsMsg = "<tr><td class=\"tbTitle\">" .$szTitle ."</td></tr>\n";
       $szNoResultsMsg .= "<tr><td><br><b>No Results Found</b></td></tr></table>";
       return $szNoResultsMsg;
    }
    
        // open header row
   // $szReturn .= "<tr><td></td>";
    // close header row
   // $szReturn .= "</tr></table>\n";
    
    // loop and build the select option items
    for( $i=0; $i<$nCount; $i++ )
    {
        // get the next result
        $axResults = pg_fetch_assoc( $oResult, $i );

        // add row for each
        $szReturn .= "<table class=\"layoutTable\" width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >";

        // loop and add cells
        foreach( $axResults as $key=>$xCell )
        {
	                         if (strcasecmp($key, "min_unit_n") == 0 )
	                         {
					     $hold_name = $xCell;
	                         }


	                         elseif (strcasecmp($key, "min_access") == 0 )
	                         {
	                             $access = $xCell;
                                   if ($access == 'RA')
                                   {
							$access = "Restricted Public Access";
                                   } 
                                   elseif ($access == 'OA')
					     {
							$access = "Open Public Access";
                                   } 

	                         }

	                         elseif (strcasecmp($key, "acres") == 0 )
	                         {
					     $gin_acres = round($xCell, 1);

	                         }

	                         elseif (strcasecmp($key, "link") == 0 )
				       {
					     $link = $xCell;

	                         }
					elseif (strcasecmp($key, "unit_id") == 0)
	                         {
					     $xml_id_p = $xCell;

	                         }
					elseif (strcasecmp($key, "xml_id") == 0)
	                         {
					     $xml_id_c = $xCell;

	                         }

   					elseif (strcasecmp($key, "park_name") == 0 )
				       {
					     $park_name = $xCell;
	                         }
					elseif (strcasecmp($key, "website") == 0 )
				       {
                                   $website = $xCell;

	                         }
					elseif (strcasecmp($key, "moniker1") == 0 )
				       {
	                             $moniker = $xCell;

	                         }
					 elseif (strcasecmp($key, "status") == 0 )
	                         {
					     $status = $xCell;

	                         }
					 
	                         
	                         elseif (strcasecmp($key, "image") == 0 )
                               {
				 		//$image = $xCell;
                                      $image = "ChinaCamp.jpg";

	                         }

                               elseif (strcasecmp($key, "min_image") == 0 )
                               {
				 		$image = $xCell;

	                         }

	                     }
			         if ($hold_name) {
					 $szReturn  .="<tr><td width=\"85%\"><font size=\"-3\">".$szTitle.":</font> <font color=\"007560\"><b>". $hold_name. "</b></font> | <a href='javascript:void(0)' onClick=\"zoomFeature('". $hold_name ."', '". $szResultsPage ."')\"; style='text-decoration:none'>Map &#187</a><BR>\n";

                          }

				    if ($access) {
					 $szReturn  .= "$access";
                             $szReturn  .= "&nbsp&nbsp&nbsp&nbsp&nbsp";
                              $access = "";
				  }

                        if ($gin_acres) {
					 $szReturn  .= "   " .$gin_acres. " acres<br>\n";
                            $gin_acres = "";
				  }
                        if ($moniker) {
					 $szReturn  .="<tr><td width=\"85%\"><font size=\"-3\">".$szTitle.":</font> <font color=\"007560\"><b>". $moniker. "</b></font><BR>\n";
                             $moniker ="";
				  }
				if ($status) {
					 $szReturn  .="<tr><td width=\"85%\">". $status. "<BR>\n";
                             $status ="";
				  }


				  if ($link) {
					 if (strlen($link) > 47)
					 {
						$link2 = substr($link, 0, 47);
						$szReturn  .= "Go to:&nbsp<a href=\"javascript:void(window.open('". $link. "','ParkSite','left=100,top=100,width=800,height=600,toolbar=1,scrollbars=yes,resizable=0'));\">$link2</a><br>";
				         } else {
						$szReturn  .= "Go to:&nbsp<a href=\"javascript:void(window.open('". $link. "','ParkSite','left=100,top=100,width=800,height=600,toolbar=1,scrollbars=yes,resizable=0'));\">$link</a><br>"; 
					 }
					 
                                         $link = "";
					 $link2 = "";
				  }
				  if ($xml_id_p) {
					 $szReturn  .= "<tr><td class=\"transitlink\" ><a href=\"javascript:void(window.open('http://www.transit.511.org/tripplanner/index.asp?ts=". $hold_name. "','Transit','left=100,top=100,width=800,height=600,toolbar=1,scrollbars=yes,resizable=0'));\">Transit information</a>\n";
					 $szReturn  .= " | <a href=\"javascript:void(window.open('../../googlemap_p.html?id=". $xml_id_p. "','Directions','left=100,top=100,width=800,height=600,toolbar=1,scrollbars=yes,resizable=0'));\">Driving directions</a></td>\n";
                             $hold_name = "";
                             $xml_id_p ="";
				  }
				  if ($park_name) {
					 $szReturn  .="<tr><td width=\"85%\"><font size=\"-3\">".$szTitle.": </font><font color=\"007560\"><b>". $park_name. "</b></font><BR>\n";

				  }
				  if ($website) {
					 if (strlen($website) > 47)
					 {
						$website2 = substr($website, 0, 47);
						$szReturn  .= "Go to:&nbsp<a href=\"javascript:void(window.open('". $website. "','ParkSite','left=100,top=100,width=800,height=600,toolbar=1,scrollbars=yes,resizable=0'));\">$website2</a><br>";
				         } else {
						$szReturn  .= "Go to:&nbsp<a href=\"javascript:void(window.open('". $website. "','ParkSite','left=100,top=100,width=800,height=600,toolbar=1,scrollbars=yes,resizable=0'));\">$website</a><br>"; 
					 }
					 
                                         $website = "";
					 $website2 = "";
				  }

	
                        if ($xml_id_c) {
				         $szReturn  .= "<tr><td class=\"transitlink\" ><a href=\"javascript:void(window.open('http://www.transit.511.org/tripplanner/index.asp?ts=". $park_name. "','Transit','left=100,top=100,width=800,height=600,toolbar=1,scrollbars=yes,resizable=0'));\">Transit information</a>\n";
					 $szReturn  .= " | <a href=\"javascript:void(window.open('../../googlemap_c.html?id=". $xml_id_c. "','Directions','left=100,top=100,width=800,height=600,toolbar=1,scrollbars=yes,resizable=0'));\">Driving directions</a></td>\n";
                             $xml_id_c =""; 
                             $park_name="";
				  }

				  
				  
				  //if ($image) {
	                    //     $szReturn  .="<td width=\"15%\" align=\"right\"><img height=\"45\" src=\"../../images/". $image. "\"></td></tr>\n";
                          //    $image = "";
				  //}
                  $szReturn  .= "<tr><td><img width=\"285\" src=\"../../images/divider.gif\"></td></tr>";
	    		$szReturn  .= "</table><br>";
    
    }

    return $szReturn;

// end buildTable() function

}

/**
  _____________________________________________________________________________
 |
 |  getBbox()
 |
 |  Postcondition:  This function executes the given SQL, gets the bounding
 |                  box of selected features and sets the map extent to the
 |                  bounding box extent.
 |
 |  @param $oDB object - database object.
 |  @param $szSQLBbox string - SQL to run.
 |  @param $szNoResultsMsg string - message to return if no results.
 |  @param $oMap object - map object
 |_____________________________________________________________________________

 **/

function getBbox( $oDb, $szSQLBbox, $szNoResultsMsg, $oMap, $oMapSession )
{
    //get the bounding box
     
    $oBbox = @pg_query( $oDb, $szSQLBbox );
           if ( !$oBbox )
        {
           return( pg_last_error() );
        }
        
	  // check for nothing found
        $nCount = pg_num_rows( $oBbox );
         if ( $nCount == 0 )
        {
        return $szNoResultsMsg;
        }

 
        // loop and get the x and y values
          for( $j=0; $j<1; $j++ )
            {
                // get the next result
                $axResults = pg_fetch_assoc( $oBbox, $j );
                
                    // loop and add title
                    foreach( $axResults as $key=>$value )
                    {
                       
                        if ($key == "xmin")
                        {
                          $xmin= $value;
                          $xmin = $xmin - 1500;

                        }
                        if ($key == "ymin")
    		    	{
    		           $ymin= $value;
                       $ymin = $ymin - 1500;
    		    	}
    		        if ($key == "xmax")
    		    	{
    		      	   $xmax= $value;
                           $xmax = $xmax + 1500;

    		      
    		    	}
    		    	if ($key == "ymax")
    		    	{
    		           $ymax= $value;
                       $ymax = $ymax + 1500;
                        }
                    }
            }         
            
      //set the zoom envelope
       if ($xmin > 0)
       {
       $oMap->setExtent($xmin, $ymin, $xmax, $ymax);
       $_SESSION['gszCurrentState'] = $oMapSession->saveState();
       $oMap->save('c:/temp/junk.map'); 
         


       } 
    //end get the bounding box
}

?>
<html>
<head>
<link href="../popup.css" rel="stylesheet" type="text/css">

<script language="javascript">
window.onload = function() {
    // check for errors
    if ( '<?php echo $szError ?>' == '' )
    {
        // redraw the main map
        parent.redrawMap();
    }
    else
    {
        alert( '<?php echo $szError ?>' );
    }
}


function zoomFeature (hold_name, szResultsPage) {
	// set the IFRAME page source
	top.frames['zoomFeature'].location.href = szResultsPage + '&min_unit_n=' + hold_name;
}

</script>
</head>
<body>
<?php 
echo $szTable;
echo $szTable2; 
echo $szTable3;
?>
</body>
</html>


More information about the Chameleon mailing list