pigochu/yii2-jquery-locationpicker

An error and a typo

arjenmeijer opened this issue · 12 comments

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;
/**

  • LocationPickerAsset
    *
  • @author pigo
    */
    class CoordinatesPickerAsset extends \yii\web\AssetBundle
    {
    public $sourcePath = DIR . '/assets'; // <-- the line that generates the error
    public $css = [
    'coordinates-picker.css'
    ];
    }
  1. yii\base\ErrorHandler::handleFatalError()

Any idea's how to solve this problem?

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 ^^

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?

$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);

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?

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()

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").  "')"),
                ]
        ] ,

    ]);
?>

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?

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 ...

I found the api maybe can help you convert every type

https://geophp.net/api.html

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

^^ You're welcome