CartoWeb 3.5.0
Download Now!


CartoWeb
Advanced Geographical Information System for the Web

Get the X,Y coordinate of a point after a click on the map.

This is a simple plugin that allow to recover the x,y coordinate of a point on the map after the user has clicked on it.

You can use it with Ajax on or off.

get it here: geoloc.zip

Installation

  • download the geoloc.zip file and extract the content in the plugins folder ( either in your project folder or in the main Cartoweb folder ).

  • activate the plugin in your project's client.ini file. The plugin name is geoloc

  • add the javascript include for the plugin in your project main cartoclient.tpl template header.

  {if $geoloc_active}
    <script type="text/javascript" src="{r type=js plugin=geoloc}geoloc.js{/r}"></script>
    {if $ajaxOn|default:''}
    <script type="text/javascript" src="{r type=js plugin=geoloc}Geoloc.ajax.js{/r}"></script>
    {/if}
  {/if}

/!\ you must add the code AFTER {include file="cartoclient_ajaxHeader.tpl"} and AFTER {include file="dhtmlcode.tpl"} /!\

  • make a php cw3setup.php --install so the js and images are copied/linked to the htdocs folder.

  • clear the smarty cache on the server and your browser cache

Thats all, you should now see a new tool in your toolbar whose name is 'geoloc'. If you select it and clic on the map, it should return a message with 'geo_x=......,geo_y=.......'

You can very easily modify the plugin to do what you want with php, instead of returning a simple message.

File content

ClientGeoloc.php

<?php
/**
 * This plugin let the user select a point on the map and recover the coordinate 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * @copyright 2005 Camptocamp SA
 * @package Plugins
 * @version $Id: ClientGeoloc.php,v 1.0 2020/10/12 13:58:45 ochriste Exp $
 */

class ClientGeoloc extends ClientPlugin
                   implements FilterProvider, Sessionable,
                              GuiProvider, ToolProvider, Ajaxable {
  
    /**
     * @var Logger
     */
    private $log;

    /**
     * @var XY coordinates
     */
    private $Geo_x;
    private $Geo_y;

    /**
     * @var pluginStatus
     * status of the plugin, activated or not
     */
    private $pluginStatus;

    /** 
     * Constructor
     */
    public function __construct() {
        parent::__construct();  
        $this->log =& LoggerManager::getLogger(__CLASS__);

        $this->pluginStatus = true;
    }

    /**
     * @see Sessionable::loadSession()
     */
    public function loadSession($sessionObject) {
        $this->Geo_x = isset($sessionObject['x']) ? $sessionObject['x'] : '';
        $this->Geo_y = isset($sessionObject['y']) ? $sessionObject['y'] : '';
    }

    /**
     * Reinitializes session-saved tool.
     * @see Sessionable::createSession()
     */
    public function createSession(MapInfo $mapInfo, InitialMapState $initialMapState) {
        $this->Geo_x = '';
        $this->Geo_y = '';
    }

    /**
     * @see Sessionable::saveSession()
     */
    public function saveSession() {
        return array('x' => $this->Geo_x,
                     'y' => $this->Geo_y,
        );
    }
    
    /**
     * @see FilterProvider::filterGetRequest()
     */
    public function filterGetRequest(FilterRequestModifier $request) {}

    /**
     * @see FilterProvider::filterPostRequest()
     */
    public function filterPostRequest(FilterRequestModifier $request) {}

    /**
     * @see GuiProvider::handleHttpPostRequest()
     */
    public function handleHttpPostRequest($request) {

        if (isset($request['tool']) && $request['tool'] == 'geoloc') {
            if (isset($request['selection_coords'])) {
                $arr_coord = explode(',',$request['selection_coords']);
                if (sizeof($arr_coord) == 2) {
                    $this->Geo_x = round($arr_coord[0]);
                    $this->Geo_y = round($arr_coord[1]);
                    $this->getCartoclient()->addMessage('geo_x='.$this->Geo_x.',geo_y='.$this->Geo_y);
                }
            }
        }
    }

    /**
     * Recover the tool received via url
     * @see GuiProvider::handleHttpGetRequest()
     */
    public function handleHttpGetRequest($request) {}
  
      /**
     * This method factors the plugin output for both GuiProvider::renderForm()
     * and Ajaxable::ajaxGetPluginResponse().
     * @return array array of variables and html code to be assigned
     */
    protected function renderFormPrepare() {
        return array('geoloc_active' => $this->pluginStatus);
    }

    /**
     * @see GuiProvider::renderForm()
     */
    public function renderForm(Smarty $template) {
        $template->assign($this->renderFormPrepare());
    }

    /**
     * @see ToolProvider::handleMainmapTool()
     */
    public function handleMainmapTool(ToolDescription $tool, Shape $mainmapShape) {}

    /**
     * @see ToolProvider::handleKeymapTool()
     */
    public function handleKeymapTool(ToolDescription $tool, Shape $keymapShape) {}

    /**
     * @see ToolProvider::handleApplicationTool()
     */
    public function handleApplicationTool(ToolDescription $tool) {}

    /**
     * @see ToolProvider::getTools()
     */
    public function getTools() {
         return array(new ToolDescription('geoloc', true, 150, 1));
    }

    /**
     * @see Ajaxable::ajaxGetPluginResponse()
     */
    public function ajaxGetPluginResponse(AjaxPluginResponse $ajaxPluginResponse) {
        $output = $this->renderFormPrepare();
    }
    
    /**
     * @see Ajaxable::ajaxHandleAction()
     */
    public function ajaxHandleAction($actionName, PluginEnabler $pluginEnabler) {
        switch ($actionName) {
            case 'Geoloc.DoIt':
                $pluginEnabler->disableCoreplugins();
                $pluginEnabler->enablePlugin('geoloc');
            break;
        }
    }
}

?>

geoloc.js

// main geoloc tool declaration, properties and methodes
Map.prototype.geoloc = function(aDisplay) {
  this.selectionPoint(aDisplay, 'Geoloc.DoIt');
};

Geoloc.ajax.js

AjaxPlugins.Geoloc = {};

/*
 * Geoloc plugin's Actions
 */
 
AjaxPlugins.Geoloc.Actions = {};

AjaxPlugins.Geoloc.Actions.DoIt = {
    buildPostRequest: function(argObject) {
        return AjaxHandler.buildPostRequest();
    }  
};

CartoWeb Wiki: HowToGetXYCoordinatesOnClick (last edited 2020-10-12 13:10:20 by OliverChristen)

© 2002-2007 CartoWeb by Camptocamp SA - Wiki powered by MoinMoin