Contents
Simple example code to query a database to get a point coordinates and display a point on the map accordingly to its coordinates.
There are 4 files, 1 on client side, 2 in common and 1 on server side
CLIENT SIDE (ClientYourPluingName.php)
<?php
class ClientYourPluingName extends ClientPlugin
implements ServerCaller {
private $vehiculeCoords;
public function initialize() {
$this->vehiculeCoords = array();
// get coord from db
$db = $this->getDb();
$sql = 'SELECT * FROM "your_table_with_values" WHERE any_condition_you_want';
$result = $db->query($sql);
if (DB::isError($result)) {
throw new CartoclientException($result->getMessage());
}
$row = NULL;
while ($result->fetchInto($row, DB_FETCHMODE_ASSOC)) {
$this->vehiculeCoords['x'] = $row['name_of_the_x_coord_column'];
$this->vehiculeCoordsY['y'] = $row['name_of_the_y_coord_column'];
}
}
private function getDb() {
if (is_null($this->db)) {
$dsn = $this->getConfig()->databaseDsn;
// print "dsn= ".$dsn."<br>";
if (!$dsn) {
throw new CartoclientException('Search database DSN not found');
}
$this->db = DB::connect($dsn);
//check for db connection error, throw exception if needed
Utils::checkDbError();
}
return $this->db;
}
/**
* @see ServerCaller::buildRequest()
*/
public function buildRequest() {
// we call for a new shape only if we have a x and y
if ($this->vehiculeCoords['x'] != '' and $this->vehiculeCoords['y'] != '') {
$newElement = new Point($this->vehiculeCoords['x'], $this->$this->vehiculeCoords['y']);
$styledShape = new StyledShape();
$styledShape->shape = $newElement;
$shapeStyle = new ShapeStyle();
$shapeStyle->symbol = $this->type;
$shapeStyle->size = 20;
$shapeStyle->color->setFromRGB(51, 0, 204);
$shapeStyle->outlineColor->setFromRGB(0, 0, 0);
$styledShape->shapeStyle = $shapeStyle;
$YourPluingNameRequest = new YourPluingNameRequest();
$YourPluingNameRequest->shapes = array($styledShape);
return $YourPluingNameRequest;
}
}
/**
* @see ServerCaller::initializeResult()
*/
public function initializeResult($YourPluingNameResult) {}
/**
* @see ServerCaller::handleResult()
*/
public function handleResult($YourPluingNameResult) {}
?>
COMMON (YourPluingName.php)
<?php
class YourPluingNameRequest extends CwSerializable {
/**
* Styled shapes to be drawn
* @var array
*/
public $shapes;
/**
* @see CwSerializable::unserialize()
*/
public function unserialize($struct) {
$this->shapes = self::unserializeObjectMap($struct, 'shapes',
'StyledShape');
}
}
/**
* Result
* @package Plugins
*/
class YourPluingNameResult {}
?>
COMMON (yourPluingName.wsdl.inc)
<!-- outline -->
<complexType name="Color">
<all>
<element name="r" type="xsd:int"/>
<element name="g" type="xsd:int"/>
<element name="b" type="xsd:int"/>
</all>
</complexType>
<complexType name="ShapeStyle">
<all>
<element name="symbol" type="xsd:int"/>
<element name="size" type="xsd:int"/>
<element name="color" type="types:Color"/>
<element name="outlineColor" type="types:Color"/>
<element name="backgroundColor" type="types:Color"/>
<element name="transparency" type="xsd:int"/>
</all>
</complexType>
<complexType name="LabelStyle">
<all>
<element name="font" type="xsd:int"/>
<element name="size" type="xsd:int"/>
<element name="color" type="types:Color"/>
<element name="outlineColor" type="types:Color"/>
<element name="backgroundColor" type="types:Color"/>
</all>
</complexType>
<complexType name="StyledShape">
<all>
<element name="shapeStyle" type="types:ShapeStyle"/>
<element name="labelStyle" type="types:LabelStyle"/>
<element name="shape" type="types:Shape"/>
<element name="label" type="xsd:string"/>
</all>
</complexType>
<complexType name="ArrayOfStyledShape">
<complexContent>
<restriction base="enc11:Array">
<attribute ref="enc11:arrayType" wsdl:arrayType="types:StyledShape[]"/>
</restriction>
</complexContent>
</complexType>
<complexType name="OutlineRequest">
<all>
<element name="className" type="xsd:string"/>
<element name="shapes" type="types:ArrayOfStyledShape"/>
<element name="maskMode" type="xsd:boolean"/>
</all>
</complexType>
<complexType name="OutlineResult">
<all>
<element name="className" type="xsd:string"/>
<element name="area" type="xsd:double"/>
</all>
</complexType>
SERVER SIDE (ServerYourPluingName.php)
<?php
class ServerYourPluingName extends ClientResponderAdapter {
/**
* @var Logger
*/
private $log;
/**
* @var objet
*/
private $shapes;
/**
* Constructor
*/
function __construct() {
parent::__construct();
$this->log =& LoggerManager::getLogger(__CLASS__);
}
/**
* @see ClientResponder::initializeRequest()
*/
function initializeRequest($requ) {
if ($requ)
$this->shapes = $requ->shapes;
}
/**
* Result is set in initializeRequest but Outline must be called
* in handlePreDrawing
* @see ClientResponder::handlePreDrawing()
*/
function handlePreDrawing($requ) {
if ($requ) {
$pluginManager = $this->serverContext->getPluginManager();
if (empty($pluginManager->outline))
throw new CartoserverException("outline plugin not loaded, "
. "and needed to draw the new element");
$pluginManager->outline->draw($this->shapes);
}
}
}
?>