pigochu / yii2-jquery-locationpicker

jquery location picker widget for yii2
20 stars 7 forks source link

An error and a typo #1

Closed arjenmeijer closed 8 years ago

arjenmeijer commented 8 years ago

Thank you for publishing this code. It does help me.

  1. Add a ' after 'enableSearchBox in de CoördinatesPicker
  2. and I am getting this error: PHP Parse Error – yii\base\ErrorException syntax error, unexpected '.', expecting ',' or ';'

in vendor/pigochu/yii2-jquery-locationpicker/CoordinatesPickerAsset.php at line 12

use yii\web\View; use yii\base\InvalidConfigException; /**

Any idea's how to solve this problem?

pigochu commented 8 years ago

I am sorry . I only tested this widget on PHP 5.6 . In PHP 5.5 , "defined variable" can not put in class property .....

I already fixed it on PHP 5.5. now version is 0.1.1 please update this widget try again

composer update pigochu/yii2-jquery-locationpicker

thank Q ^^

arjenmeijer commented 8 years ago

Thank you for the quick response. I am testing the code. Next question: echo $form->field($model, 'coordinates') => 'coordinates' is an array with two fields? Just 0 and 1 or longitude of latitude as keys?

pigochu commented 8 years ago

$model->coordinates type is string , the format define in CoordinatesPicker::valueTemplate

u can use explode() convert to array , example :

list($lat , $long) = explode(',' , $model->coordinates);
arjenmeijer commented 8 years ago

Thank you! The input example is working fine. However, I am lost at implementing the last example. The problem is, my model has a latitude and longitude field, not an array of two fields. How do I code this when calling the widget?

pigochu commented 8 years ago

I think this widget is not suitable for use in ActiveRecord , because ActiveField can not use two fields in the same time . I would create a FormModel to do validation then save to ActiveRecord in my case.

Another way, maybe you can define clientOptions for hidden field

<?php
    echo $form->field($model, 'latitude')->hiddenInput();
    echo $form->field($model, 'longitude')->hiddenInput();

    echo $form->field($model, 'coordinates')->widget('\pigolab\locationpicker\CoordinatesPicker' , [
        // your other setting ....
        'clientOptions' => [
            'radius'    => 300,
                'inputBinding' => [
                    'latitudeInput'     => new JsExpression("$('#"  .Html::getInputId($this->model, "latitude").  "')"),
                    'longitudeInput'    => new JsExpression("$('#"  .Html::getInputId($this->model, "longitude").  "')"),
                ]
        ] ,

    ]);
?>

But the field "coordinates" you stiil define in your Model , you need think how to process it when save() or find()

pigochu commented 8 years ago

I think a simple way maybe u can define a class CoordinatesModel extends Model CoordinatesModel just have one field coordinates So you don't need change any code in your original Model .

<?php
    echo $form->field($model, 'latitude')->hiddenInput()->label(false);
    echo $form->field($model, 'longitude')->hiddenInput()->label(false);

   // $model change to $model2 , $model2 is CoordinatesModel 
    echo $form->field($model2, 'coordinates')->widget('\pigolab\locationpicker\CoordinatesPicker' , [
        // your other setting ....
        'clientOptions' => [
            'radius'    => 300,
                'inputBinding' => [
                    'latitudeInput'     => new JsExpression("$('#"  .Html::getInputId($model, "latitude").  "')"),
                    'longitudeInput'    => new JsExpression("$('#"  .Html::getInputId($model, "longitude").  "')"),
                ]
        ] ,

    ]);
?>
arjenmeijer commented 8 years ago

Thank you! I changed my model with a mysql field name = gps type point I entered the value GeomFromText 'POINT(52 5)',0

I copied the latest example in a form and changed the name coordinates to gps. I got:

PHP Notice – yii\base\ErrorException Undefined offset: 0 in /var/www/html/pro.3de5.nl/vendor/pigochu/yii2-jquery-locationpicker/CoordinatesPicker.php

$pattern = '/' . strtr($this->valueTemplate , [ '{latitude}' => '(?P.)' , '{longitude}' => '(?P.)' ]). '/';

    preg_match_all($pattern, $coordinates, $matches , PREG_SET_ORDER);

    $this->clientOptions['location'] = [
        'latitude' => floatval($matches[0]['latitude']),             <- error !!
        'longitude' => floatval($matches[0]['longitude']),

So I am using the wrong type in mysql?

Moreover, in a new form the input widget bombs out. I have to set the longitude and latitude first. Is this expected behaviour?

pigochu commented 8 years ago

You don't need change your original model , I have update README.MD , and post example for two fields auto binding.

If you want to use Point type

  1. u need define CoordinatesPicker's 'valueTemplate' for match mysql point format , I don't know whats the correct format
  2. I think yii2 dosent support Point type for ActiveRecord, so u need convert again ...
pigochu commented 8 years ago

I found the api maybe can help you convert every type

https://geophp.net/api.html

arjenmeijer commented 8 years ago

Thank you! I have got a simple model working thanks to your help.

pigochu commented 8 years ago

^^ You're welcome

pigochu commented 8 years ago

I wrote a wiki , how to do auto conversion when you use mysql Point type

https://github.com/pigochu/yii2-jquery-locationpicker/wiki/How%20to%20use%20PHP%20trait%20do%20simple%20conversion%20MySQL%20Spatial%20Type%20between%20ActiveRecord