pigochu / yii2-jquery-locationpicker

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

LocationPicker value lost on update. #13

Closed dev-myatminsoe closed 6 years ago

dev-myatminsoe commented 6 years ago

I've stored coordinates as string. It is showing correctly on map when updating. But after clicking update the coordinates changed to (40.7324319,-73.82480777777776).

rdafisher commented 6 years ago

It appears that just after loading the widget the field containing the coordinates are changed from the models current value back to the default JQuery Location Picker coordinates (Which you can see here: http://logicify.github.io/jquery-locationpicker-plugin/#examples)

If you use dev tools to watch the elements you can see the field loads with the correct values, and is then quickly changed. The map remains in position with the pointer on the stored location - however unless you manually drag the pin, if you update the model it will override the coordinates with the defaults!

I think this issue may be originating somewhere in the JS, as the CoordinatesPicker.php sets the client location correctly.

@pigochu do you have any idea why this might be happening? Can I provide anything that might shed light on this? or are we just being silly and missing something obvious.

This is how I am calling the widget on my update form:

I am using version 0.2.2 of the code.


echo $form->field($model, 'map_coords')->widget('\pigolab\locationpicker\CoordinatesPicker' , [
    'key' => 'AIzaSyABf6lUkWRDoWHQ7DvytGHa1I-v6KV3yes' ,   // require , Put your google map api key
    'valueTemplate' => '{latitude},{longitude}' , // Optional , this is default result format
    'options' => [
        'style' => 'width: 100%; height: 600px',  // map canvas width and height
    ] ,
    'enableSearchBox' => true , // Optional , default is true
    'searchBoxOptions' => [ // searchBox html attributes
        'style' => 'width: 300px;', // Optional , default width and height defined in css coordinates-picker.css
    ],
    'searchBoxPosition' => new JsExpression('google.maps.ControlPosition.TOP_LEFT'), // optional , default is TOP_LEFT
    'mapOptions' => [
        // google map options
        // visit https://developers.google.com/maps/documentation/javascript/controls for other options
        //'mapTypeControl' => true, // Enable Map Type Control
        'mapTypeControlOptions' => [
            'style'    => new JsExpression('google.maps.MapTypeControlStyle.HORIZONTAL_BAR'),
            'position' => new JsExpression('google.maps.ControlPosition.TOP_LEFT'),
        ],
        'streetViewControl' => true, // Enable Street View Control
    ],
    'clientOptions' => [
        // jquery-location-picker options
        'radius'    => 30,
        'addressFormat' => 'street_number',
        'zoom' => 16,
        'onchanged' => new JsExpression('function(currentLocation, radius, isMarkerDropped) {console.log("New Location",$("#places-location").val());}'),
        //'onlocationnotfound' => new JsExpression('function(locationName) {console.log("ONLOCATIONNOTFOUND");}'),
        //'oninitialized' => new JsExpression('function (component) {console.log("ONINITIALIZED", {component:component});}'),
    ],
])->label('Location:  Drag the pin to the desired location.');```
pigochu commented 6 years ago

I have no error in my test.

I think it could happed some js error or exception . can you see any javascripts error in dev console ?

rdafisher commented 6 years ago

Nothing in the console. No exceptions shown.

I can see the transition happen before my eyes when watching the elements in Chrome dev tools. The field with the coordinates loads in with the correctly set value, then at around about the "Document Ready" state, the values flash and change to the jquery-locationpicker-plugin default of 40.7324319,-73.82480777777776.

So... some javascript somewhere must be changing the DOM on on document ready.

I could probably code around this behaviour, as the UI position of the pin stays in the correct location - so the user won't be any wiser. Perhaps I could use my own JS to reset the input's value to what it should be.

Or I could add a beforeSave() method to the model which looks for the default coordinates, and if present swaps them for those on the unedited version of the record.

But if you do not see the same issue on your test system, it must be something relating either to the version of jquery-locationpicker-plugin or possibly even another JS library.

pigochu commented 6 years ago

I run "composer show" in my test project

bower-asset/bootstrap                    v3.3.7   The most popular front-end framework for developing responsive...
bower-asset/inputmask                    3.3.11   Inputmask is a javascript library which creates an input mask....
bower-asset/jquery                       3.2.1
bower-asset/jquery-locationpicker-plugin 0.1.16   This plug-in allows finding and selecting a location on the Go...
bower-asset/punycode                     v1.3.2
bower-asset/typeahead.js                 v0.11.1
bower-asset/yii2-pjax                    2.0.7.1
cebe/markdown                            1.1.2    A super fast, highly extensible markdown parser for PHP
doctrine/lexer                           v1.0.1   Base library for a lexer that can be used in Top-Down, Recursi...
egulias/email-validator                  2.1.3    A library for validating emails against several RFCs
ezyang/htmlpurifier                      v4.9.3   Standards compliant HTML filter written in PHP
fzaninotto/faker                         v1.7.1   Faker is a PHP library that generates fake data for you.
insolita/yii2-migration-generator        2.3      Set of gii tools for generating  files for migration by schema...
phpspec/php-diff                         v1.1.0   A comprehensive library for generating differences between two...
skywalkapps/bootstrap-notifications      0.9.0
swiftmailer/swiftmailer                  v6.0.2   Swiftmailer, free feature-rich PHP mailer
yiisoft/yii2                             2.0.13.1 Yii PHP Framework Version 2
yiisoft/yii2-bootstrap                   2.0.7    The Twitter Bootstrap extension for the Yii framework
yiisoft/yii2-composer                    2.0.5    The composer plugin for Yii extension installer
yiisoft/yii2-debug                       2.0.13   The debugger extension for the Yii framework
yiisoft/yii2-faker                       2.0.3    Fixture generator. The Faker integration for the Yii framework.
yiisoft/yii2-gii                         2.0.6    The Gii extension for the Yii framework
yiisoft/yii2-swiftmailer                 2.1.0    The SwiftMailer integration for the Yii framework
bvanleeuwen1995 commented 6 years ago

I can report that I experience the same problem, I set the location trough the code that should be default, the point shows that correct location, but the value of the input gets changed to a other value right around the document ready mark.

pigochu commented 6 years ago

hi all

I try to fix the the bug ,. could you test it from branches "dev" ? I only changed "CoordinatesPicker.php" , so the fast way is replace the file on your project , if it can work , I will release a new version.

https://github.com/pigochu/yii2-jquery-locationpicker/blob/dev/CoordinatesPicker.php

rdafisher commented 6 years ago

I have done a quick test, just swapping out the file as suggested, and it seems to work!

Looking at hidden input on dev tools - it now has the correct value when the page loads, rather than the default location.

Can this go into master branch as soon as you have a moment please?

Thanks @pigochu, I appreciate your time looking at this.

rdafisher commented 6 years ago

@bvanleeuwen1995 have you had time to test the fix?

pigochu commented 6 years ago

I merged to master and tag version 0.2.3

so you can update via composer