Get value by clicking a button and display it in an input text html5 and geolocation of google maps

-1

good morning I greet you.

What happens is that I want to obtain the coordinates of google maps, by clicking on a button and that the information appears in an input type text file.

This is my code:

function getLocation(){
if(navigator.geolocation){
navigator.geolocation.getCurrentPosition(showPosition);
    }else{
        document.getElementById("coord").innerHTML = "Ohhh! Lo Lamentamos."
    }
}

function showPosition(posicion){
    document.getElementById("coord").innerHTML=""+ posicion.coords.latitude + posicion.coords.longitude;
}
<script src="https://code.jquery.com/jquery-1.11.0.js" integrity="sha256-zgND4db0iXaO7v4CLBIYHGoIIudWI5hRMQrPB20j0Qw=" crossorigin="anonymous"></script>

<button onclick="getLocation()" class="localizar" title="Localizar"><img src="../imagenes/localizacion.png" alt="Icono localización" width="90%"></button>

<input type="text" class="form-control" name="lug_den" id="coord" placeholder="¿DONDÉ?" autocomplete="off" required maxlength="61">

I appreciate the help you can give me, I have been looking at other posts and forums and I have not found the solution. Thanks.

    
asked by FARID FORERO 27.08.2018 в 17:41
source

1 answer

0

Hi @farid, you're using innerHTML in an input, so it does not work. If you are already using jquery you can change the showPosition() function like this:

function showPosition(posicion){
    $('#coord').val(posicion.coords.latitude +' : '+ posicion.coords.longitude);
}

the val() jquery method updates the value of the input. I hope it serves you!

    
answered by 27.08.2018 / 17:58
source