mplodowski / formbuilder-plugin-public

https://octobercms.com/plugin/renatio-formbuilder
https://octobercms.com/plugin/renatio-formbuilder
2 stars 0 forks source link

Geolocation field type #24

Closed NielsvandenDries closed 1 year ago

NielsvandenDries commented 1 year ago

Is your feature request related to a problem? Please describe. The Request is not related to a Problem, Just a new feature.

Describe the solution you'd like I like to see a field type for Geolocation, if used is gived the user a latitude/longitude of his current location. The geolocation should automatic update and should be send in the notification via Mail.

Describe alternatives you've considered I tried to code it myself, but my knowledge of javascript is not that great, I myself had created a geolocation.js that provides the location, Then tried to place it in a disabled textfield but unfortunately it didn't work.

Additional context Geolocation-screenshot-20221013

nvandendries1984 commented 1 year ago

I created the following JavaScript in "plugins/renatio/formbuilder/assets/js/geolocation.js"

window.onload = getLocation;

var x = document.getElementById("geoloc");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition)
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    x.setAttribute('value', position.coords.latitude + ", " + position.coords.longitude)
}

As a Field Type i created:

<div class="{{ wrapper_class }} form-group">
    {% if label %}
        <label for="{{ name }}" class="{{ label_class }}">{{ label }}</label>
    {% endif %}

    <input 
        id="geoloc"
        type="geoloc" 
        name="{{ name }}" 
        class="{{ class }} form-control"
        placeholder="{{ placeholder }}"
        {{ custom_attributes|raw }}
        disabled
    >
    <button onClick="window.location.reload();" type="button" class="btn btn-info" style="width: inherit;">Refresh GPS location</button>

    {% if comment %}
        <p class="help-block">{{ comment|raw }}</p>
    {% endif %}
</div>

I looks good but the set value with the Javascript is not sent with the notification Mail nor set in the Log. Is there some that can help me with this?

NielsvandenDries commented 1 year ago

Fixed it: It's working now

FieldType:

<div class="{{ wrapper_class }} form-group">
    {% if label %}
        <label for="{{ name }}" class="{{ label_class }}">{{ label }}</label>
    {% endif %}

    <input 
        id="geoloc"
        type="geoloc" 
        name="{{ name }}" 
        class="{{ class }} form-control"
        value=""
        placeholder="{{ placeholder }}"
        {{ custom_attributes|raw }}
    >
    <button onClick="window.location.reload();" type="button" class="btn btn-info" style="width: inherit;">Refresh GPS location</button>

    {% if comment %}
        <p class="help-block">{{ comment|raw }}</p>
    {% endif %}
</div>
NielsvandenDries commented 1 year ago

image

mplodowski commented 1 year ago

Wow, great work!

I will add this field in next version.

NielsvandenDries commented 1 year ago

Wow, great work!

I will add this field in next version.

Cool, I use it to add GPS location when a field engineer is working on a utility site for TV/Internet (because there are no home numbers)

NielsvandenDries commented 1 year ago

Forgot to document that the geolocation.js has to be added to RenderForm.php

NielsvandenDries commented 1 year ago

Going to upload it to discussion board for you.