pigochu / yii2-jquery-locationpicker

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

Coordinates not back to model #5

Closed dacom-dark-sun closed 7 years ago

dacom-dark-sun commented 7 years ago

I use example for back coordinates to model, but always receive a null. Code:

<?php echo $form->field($model, 'coordinates')->widget('\pigolab\locationpicker\CoordinatesPicker' , [
        'key' => 'my_key_here',
        'valueTemplate' => '{latitude},{longitude}' ,
        'options' => [
            'style' => 'width: 100%; height: 400px',  // map canvas width and height
        ] ,
        'searchBoxOptions' => [ // searchBox html attributes
            'style' => 'width: 300px;', // Optional , default width and height defined in css coordinates-picker.css
        ],
        'mapOptions' => [
            // google map options
            // visit https://developers.google.com/maps/documentation/javascript/controls for other options

        ],
      'clientOptions' => [
            'location' => [
                'latitude'  => 46.15242437752303 ,
                'longitude' => 2.7470703125,
            ],
            'radius'    => 300,
            'inputBinding' => [
                'latitudeInput'     => new JsExpression("$('#us2-lat')"),
                'longitudeInput'    => new JsExpression("$('#us2-lon')"),
                'radiusInput'       => new JsExpression("$('#us2-radius')"),
                'locationNameInput' => new JsExpression("$('#us2-address')")
            ]
]      
    ]);
?>

In model: ... { public $coordinates; .. .. What problem can be here?

pigochu commented 7 years ago

Could you post your mysql table schema and model class source code ?

dacom-dark-sun commented 7 years ago

Im sorry for long answer, but actually it is my mistake with validation rules. Your widget words perfectly, but one question more: How i can bind click event for get coordinates, that use not use drag and drop principle?

pigochu commented 7 years ago

@dacom-dark-sun

it's very easy, you can get the hidden input value

image

alert($('#locationpicker-coordinates').val() );
pigochu commented 7 years ago

@dacom-dark-sun

Oh . . I misunderstand what you mean

I need read jquery-locationpick or google map document how to do that

pigochu commented 7 years ago

Try it

    $js = '
    function(c){
        var map = jQuery(c).locationpicker("map").map;
        google.maps.event.addListener(map, "click", function(e) {
            var marker = jQuery(c).locationpicker("map").marker;
            var pos = e.latLng;
            jQuery(c).locationpicker("location" , {latitude: pos.lat(), longitude: pos.lng()});
            google.maps.event.trigger(marker, "dragend");
            // e.stopPropagation();
          });
    };

then in the CoordinatesPicker add oninitialized event

        'clientOptions' => [
            'addressFormat' => 'street_number',
            'radius'    => 300,
            'oninitialized' => new JsExpression($js),
        ],
dacom-dark-sun commented 7 years ago

Ok, thank you very much!) i will try it ASAP..

Can i disable auto-render default location? For.. not render any markers by default, but if user click or choice some place, model must receive coordinates.. Cause not every place need mark location from user, but if we have default position, this is can create some confusion..

pigochu commented 7 years ago

I think it possible ,

Options has a "markerVisible" can be set to false.

You can read all options from document http://logicify.github.io/jquery-locationpicker-plugin and try to implement your event.

dacom-dark-sun commented 7 years ago

Thanks a lot, i was solve it: jQuery(c).locationpicker("map").marker.visible = true; and load by default with false.