0

I use Yii framework with GeoExt to develop map based web application.In this app i have a feature that user can draw polygon and when click on it i send polygon information with ajax to controller action to save this polygon on database.For saving polygon on database i need coordinates of polygon.For get polygon coordinates i use below code in my controller action.But i don't know i can get polygon coordinates or no for understand about it i send coordinates to $.ajax success function in my action code but i get error in my browser console!
Action Code:

public function actionTest()
{
   if(isset($_POST['polygon']) && isset($_POST['name'])){
            $str = $_POST['polygon'];
            $polygon = CJSON::decode($str);
            $coordinates = $polygon->polygon->geometry->coordinates[0];
   }
   echo CJSON::encode(array('polygon'=> $coordinates));
}

$.ajax code:

function save(feature) {
        var geojson_format = new OpenLayers.Format.GeoJSON();
        var str = geojson_format.write(feature);
        str = str.replace(/,/g, ', ');
        Ext.MessageBox.prompt('Name', 'Please enter district name:', function(btn, text){
                if (btn == 'ok' && text.length > 0){
                    <?php 
                        echo CHtml::ajax(array(
                            'url'=>array('site/test'),
                            'data'=>array('polygon'=>'js:str',
                                'name'=>'js:text'),
                            'type'=>'POST',

                            'success'=>"function(data){
                                    console.log(data);
                                }"
                        ));
                    ?>
                    $("#output").val(str);
                }
                else if(btn == 'ok' && text.length == 0)
                    Ext.MessageBox.alert('Error', 'Please Enter Name For Polygon To Save');
            });
}

Error in browser console:

POST http://localhost/FleetManagement/index.php/site/test 500 (PHP Error) jquery.js:8102 jQuery.ajaxTransport.send jquery.js:8102 jQuery.extend.ajax jquery.js:7580 modify.mode/FleetManagement/:254(anonymous function)

But when i use below code for action i can get String in console:

public function actionTest()
{
   if(isset($_POST['polygon']) && isset($_POST['name'])){
            $str = $_POST['polygon'];
            $polygon = CJSON::decode($str);
   }
   echo CJSON::encode(array('polygon'=> $polygon ));
}

after using above code i can get below string in browser console:

{"polygon":{"type":"Feature","properties":[],"geometry":{"type":"Polygon","coordinates":[[[-7302732.4720101,6527844.6333235],[-3193477.8319711,6606116.1502766],[-5111129.9973226,5001550.0527375],[-6637424.5779086,4884142.7773079],[-7772361.5737289,5158093.0866438],[-7302732.4720101,6527844.6333235]]]},"crs":{"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}}}}

why i can't get coordinates and how can i do this?
I see below code for output of var_dump in chrome console!

<pre class='xdebug-var-dump' dir='ltr'>
<b>array</b>
'type' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'Feature'</font> <i>(length=7)</i>
'properties' <font color='#888a85'>=&gt;</font> 
<b>array</b>
<i><font color='#888a85'>empty</font></i>
'geometry' <font color='#888a85'>=&gt;</font> 
<b>array</b>
'type' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'Polygon'</font> <i>(length=7)</i>
'coordinates' <font color='#888a85'>=&gt;</font> 
<b>array</b>
0 <font color='#888a85'>=&gt;</font> 
<b>array</b>
 ...
'crs' <font color='#888a85'>=&gt;</font> 
<b>array</b>
'type' <font color='#888a85'>=&gt;</font> <small>string</small> <font color='#cc0000'>'name'</font> <i>(length=4)</i>
'properties' <font color='#888a85'>=&gt;</font> 
<b>array</b>

i don't know why coordinates doesn't appear in console an show ... stead of themes!

4

1 回答 1

1

我不确定您为什么要尝试将多边形坐标传回;听起来好像您应该将其保存到您的 actionTest 中的数据库中,并且无需将其发送回客户端?

无论如何,我认为您的代码应该在这一行抛出 PHP 错误:

$coordinates = $polygon->polygon->geometry->coordinates[0];

你确定 $polygon 是一个对象吗?我认为它是一个数组,所以你需要使用数组引用,例如:

$coordinates = $polygon['polygon']['geometry']['coordinates'][0];

在任何情况下,在您的 actionTest 中执行一些 var_dump() 以确保值是您期望的值,然后再将其传递回您的 JS。

于 2012-08-01T16:48:37.203 回答