Commit 1b0b689c3d9e6b10f6e5099052604f1c95424663
1 parent
96e1054c
stock map
Showing
5 changed files
with
76 additions
and
6 deletions
Show diff stats
migrations/m170721_133313_create_city_table.php
models/City.php
| ... | ... | @@ -7,7 +7,8 @@ |
| 7 | 7 | use artbox\core\behaviors\LanguageBehavior; |
| 8 | 8 | use yii\db\ActiveRecord; |
| 9 | 9 | use yii\web\Request; |
| 10 | - | |
| 10 | + use yii\helpers\Json; | |
| 11 | + | |
| 11 | 12 | /** |
| 12 | 13 | * This is the model class for table "city". |
| 13 | 14 | * |
| ... | ... | @@ -18,9 +19,8 @@ |
| 18 | 19 | * @property Language[] $languages |
| 19 | 20 | * @property CityLang[] $modelLangs |
| 20 | 21 | * @property Shop[] $shops |
| 21 | - * | |
| 22 | 22 | * @method bool saveWithLangs() |
| 23 | - * @method bool loadWithLangs( Request $request) | |
| 23 | + * @method bool loadWithLangs( Request $request ) | |
| 24 | 24 | * @method void generateLangs() |
| 25 | 25 | */ |
| 26 | 26 | class City extends ActiveRecord |
| ... | ... | @@ -56,6 +56,13 @@ |
| 56 | 56 | [ 'status' ], |
| 57 | 57 | 'boolean', |
| 58 | 58 | ], |
| 59 | + [ | |
| 60 | + [ | |
| 61 | + 'coords', | |
| 62 | + 'coordsArr', | |
| 63 | + ], | |
| 64 | + 'safe', | |
| 65 | + ], | |
| 59 | 66 | ]; |
| 60 | 67 | } |
| 61 | 68 | |
| ... | ... | @@ -96,4 +103,14 @@ |
| 96 | 103 | { |
| 97 | 104 | return $this->hasMany(Shop::className(), [ 'city_id' => 'id' ]); |
| 98 | 105 | } |
| 106 | + | |
| 107 | + public function setCoordsArr($value) | |
| 108 | + { | |
| 109 | + $this->coords = Json::encode($value); | |
| 110 | + } | |
| 111 | + | |
| 112 | + public function getCoordsArr() | |
| 113 | + { | |
| 114 | + return Json::decode($this->coords); | |
| 115 | + } | |
| 99 | 116 | } | ... | ... |
views/city/_form.php
| ... | ... | @@ -8,6 +8,8 @@ |
| 8 | 8 | /* @var $model artbox\stock\models\City */ |
| 9 | 9 | /* @var $form yii\widgets\ActiveForm */ |
| 10 | 10 | /* @var $modelLangs artbox\stock\models\CityLang */ |
| 11 | + | |
| 12 | + //\artbox\stock\assets\StockAsset::register($this); | |
| 11 | 13 | ?> |
| 12 | 14 | |
| 13 | 15 | <div class="city-form"> |
| ... | ... | @@ -20,7 +22,24 @@ |
| 20 | 22 | 'form' => $form, |
| 21 | 23 | ] |
| 22 | 24 | ) ?> |
| 23 | - | |
| 25 | + <?= $form->field($model, 'coordsArr[lat]') | |
| 26 | + ->textInput( | |
| 27 | + [ | |
| 28 | + 'value' => ( isset($model->coordsArr[ 'lat' ]) ) ? $model->coordsArr[ 'lat' ] : '', | |
| 29 | + 'id' => 'lat', | |
| 30 | + 'size' => 10, | |
| 31 | + ] | |
| 32 | + ) | |
| 33 | + ->label(false); ?> | |
| 34 | + <?= $form->field($model, 'coordsArr[lng]') | |
| 35 | + ->textInput( | |
| 36 | + [ | |
| 37 | + 'value' => ( isset($model->coordsArr[ 'lng' ]) ) ? $model->coordsArr[ 'lng' ] : '', | |
| 38 | + 'id' => 'lng', | |
| 39 | + 'size' => 10, | |
| 40 | + ] | |
| 41 | + ) | |
| 42 | + ->label(false); ?> | |
| 24 | 43 | <?= $form->field($model, 'sort') |
| 25 | 44 | ->textInput() ?> |
| 26 | 45 | |
| ... | ... | @@ -37,3 +56,27 @@ |
| 37 | 56 | <?php ActiveForm::end(); ?> |
| 38 | 57 | |
| 39 | 58 | </div> |
| 59 | +<script> | |
| 60 | + window.onload = function() { | |
| 61 | + $(document) | |
| 62 | + .on('blur', '#city_title', function() { | |
| 63 | + var address = $("#city_title") | |
| 64 | + .val(); | |
| 65 | + console.log(address); | |
| 66 | + var geocoder = new google.maps.Geocoder(); | |
| 67 | + geocoder.geocode({'address': address}, function(results, status) { | |
| 68 | + console.log(status); | |
| 69 | + if (status == google.maps.GeocoderStatus.OK) { | |
| 70 | + | |
| 71 | + $("#lat") | |
| 72 | + .val(results[ 0 ].geometry.location.lat()); | |
| 73 | + $("#lng") | |
| 74 | + .val(results[ 0 ].geometry.location.lng()); | |
| 75 | + } else { | |
| 76 | + alert("Geocode was not successful for the following reason: " + status); | |
| 77 | + } | |
| 78 | + }); | |
| 79 | + }); | |
| 80 | + } | |
| 81 | + | |
| 82 | +</script> | ... | ... |
views/city/_form_language.php
| ... | ... | @@ -7,7 +7,7 @@ |
| 7 | 7 | use yii\widgets\ActiveForm; |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * @var CityLang $model_lang | |
| 10 | + * @var CityLang $model_lang | |
| 11 | 11 | * @var Language $language |
| 12 | 12 | * @var ActiveForm $form |
| 13 | 13 | * @var View $this |
| ... | ... | @@ -15,7 +15,12 @@ |
| 15 | 15 | ?> |
| 16 | 16 | <?php |
| 17 | 17 | $attributeField = $form->field($model_lang, '[' . $language->id . ']title') |
| 18 | - ->textInput([ 'maxlength' => true ]); | |
| 18 | + ->textInput( | |
| 19 | + [ | |
| 20 | + 'maxlength' => true, | |
| 21 | + 'id' => 'city_title', | |
| 22 | + ] | |
| 23 | + ); | |
| 19 | 24 | echo $attributeField; |
| 20 | 25 | ?> |
| 21 | 26 | ... | ... |