简体   繁体   中英

Get polygon coordinates from geojson in yii framework

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!

I'm not sure why you're trying to pass the polygon coordinates back out; it sounds as if you should be saving it into the database in your actionTest, and there's no need to send it back to the client?

In any case, I think your code should be throwing a PHP error at this line:

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

Are you sure $polygon is an object? I would think that it's an array, so you would need to use array reference, eg:

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

In any case, do some var_dump()'s in your actionTest to make sure the values are what you expect them to be before passing it back to your JS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM