I have this
Controller
public function actionCreate()
{
$model = new HospitalInfo();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->hospital_id]);
} else {
return $this->render('create', [
'model' => $model,
]);
}
}
model
public function attributeLabels()
{
return [
'hospital_id' => 'Hospital ID',
'hospital_code' => 'Hospital Code',
'hospital_name' => 'Hospital Name',
'description' => 'Description',
'address' => 'Address',
'city_id' => 'LGA/Town',
'state_id' => 'State',
'country_id' => 'Country',
'longitude' => 'Longitude',
'latitude' => 'Latitude',
'gsm' => 'Mobile Line',
'email' => 'Email',
];
}
public function getHospitalCity() {
return $this->hasOne(City::className(), ['city_id' => 'city_id']);
}
public function getHospitalState() {
return $this->hasOne(State::className(), ['state_id' => 'state_id']);
}
public function getHospitalCountry() {
return $this->hasOne(Country::className(), ['country_id' => 'country_id']);
}
View
<div class="col-xs-12 col-lg-12">
<div class="<?php echo $model->isNewRecord ? 'box-success' : 'box-info'; ?> box view-item col-xs-12 col-lg-12">
<div class="hospital-info-form">
<?php $form = ActiveForm::begin([
'id' => 'hospital-info-form',
'enableAjaxValidation' => true,
'fieldConfig' => [
'template' => "{label}{input}{error}",
],
]); ?>
<div class="col-xs-12 col-lg-12 no-padding">
<div class="col-sm-12">
<?= $form->field($model, 'hospital_name')->textInput(['maxlength' => 250]) ?>
</div>
</div>
<div class="col-xs-12 col-lg-12 no-padding">
<div class="col-sm-6">
<?= $form->field($model, 'hospital_code')->textInput(['readonly' => 'readonly', 'maxlength' => 50]) ?>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'hmo_id')->widget(Select2::classname(), [
'data' => ArrayHelper::map(app\modules\hmo\models\HmoDetail::find()->all(),'hmo_id','hmo_name'),
'language' => 'en',
'options' => ['placeholder' => 'Select HMO ...'],
// 'disabled'=>'true',
'pluginOptions' => [
'allowClear' => true
],
]); ?>
</div>
</div>
<div class="col-xs-12 col-lg-12 no-padding">
<div class="col-sm-6">
<?= $form->field($model, 'gsm')->textInput(['maxlength' => 15]) ?>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'email')->textInput(['maxlength' => 100]) ?>
</div>
</div>
<div class="col-xs-12 col-lg-12 no-padding">
<div class="col-sm-12">
<?= $form->field($model, 'address')->textarea(['rows' => 3]) ?>
</div>
</div>
<div class="col-xs-12 col-lg-12 no-padding">
<div class="col-sm-4">
<?php echo $form->field($model,'country_id')->dropDownList(ArrayHelper::map(\app\models\Country::find()->where(['is_status' => 0])->all(),'country_id','country_name'),
[
'prompt'=>Yii::t('app', '--- Select Country ---'),
'onchange'=>'
$.get( "'.Url::toRoute('dependent/hospital_state').'", { id: $(this).val() } )
.done(function( data ) {
$( "#'.Html::getInputId($model, 'state_id').'" ).html( data );
}
);'
]
); ?>
</div>
<div class="col-sm-4">
<?php echo $form->field($model,'state_id')->dropDownList(ArrayHelper::map(\app\models\State::find()->where(['state_country_id' => $model->country_id, 'is_status' => 0])->all(),'state_id','state_name'),
[
'prompt'=>Yii::t('app', '--- Select State ---'),
'onchange'=>'
$.get( "'.Url::toRoute('dependent/hospital_city').'", { id: $(this).val() } )
.done(function( data ) {
$( "#'.Html::getInputId($model, 'city_id').'" ).html( data );
}
);'
]
); ?>
</div>
<div class="col-sm-4">
<?php echo $form->field($model,'city_id')->dropDownList(ArrayHelper::map(\app\models\City::find()->where(['city_state_id' => $model->state_id, 'is_status' => 0])->all(),'city_id','city_name'), ['prompt'=> Yii::t('app', '--- Select City ---')]);
?>
</div>
</div>
<div class="col-xs-12 col-lg-12 no-padding">
<div class="col-xs-12 col-sm-4 col-lg-4">
</div>
<div class="col-xs-12 col-sm-4 col-lg-4">
<?= $form->field($model, 'longitude')->textInput() ?>
</div>
<div class="col-xs-12 col-sm-4 col-lg-4">
<?= $form->field($model, 'latitude')->textInput() ?>
</div>
</div>
<div class="col-xs-12 col-lg-12 no-padding">
<div class="col-sm-12">
<?= $form->field($model, 'description')->textarea(['rows' => 3]) ?>
</div>
</div>
<div class="form-group col-xs-12 col-sm-6 col-lg-4 no-padding">
<div class="col-xs-6">
<?= Html::submitButton($model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Update'), ['class' => $model->isNewRecord ? 'btn btn-block btn-success' : 'btn btn-block btn-info']) ?>
</div>
<div class="col-xs-6">
<?= Html::a(Yii::t('app', 'Cancel'), ['index'], ['class' => 'btn btn-default btn-block']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
</div>
</div>
</div>
Please I want to automatically get the geolocation, the latitude and longitude, when the user enter the addres, country, state, and town.
Please help me. How do I do this in Yii2.