Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

356
Visualizações
How do I send a Latitude & Longitude Value For AJAX to get Information From GeoNames

I need some help and advice if possible.

I'm working on a task for an online course and I'm stumped. I have the below html, trying to send a latitude value to ajax for a php request to geonames API and return the nearest ocean. I have tried a couple of different ways as shown below in the html code.

    <select id="latitude">
        <option value= "3.4360">3.4360</option>
        <option value="lat">133.7751</option>
        <option value="lat">106.3468</option>
    </select>

    <select id="longitude">
        <option value= 55.3781>55.3781</option>
        <option value="lng">25.2744</option>
        <option value="lng">56.1304</option>
    </select>

And this is the ajax request that should be pulling the info from the above html.

$('#btnRun').click(function() {
    $.ajax({
        url: "libs/php/getCountryInfo.php",
        type: 'POST',
        dataType: 'json',
        data: {
                lat: parseInt($('#latitude').val()),
                lng: parseInt($('#longitude').val())
            },
            success: function(result) {

                console.log(JSON.stringify(result));

                if (result.status.name == "ok") {

                    $('#txtDistance').html(result['data'][0]['distance']);
                    $('#txtGeonameID').html(result['data'][0]['geonameid']);
                    $('#txtName').html(result['data'][0]['name']);
                }
            
            },
            error: function(jqXHR, textStatus, errorThrown) {
                // your error code
            }
        }); 
    
    });

I don't think the two link up at all and I had a look at the parseInt option as well but I am out of ideas now.

Thank you for any help you can give.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The <option> tags in your Html are wrong, the value attribute is set to the strings lat and lng and not the actual latitudes and longitudes.

<select id="latitude">
    <option value="3.4360">3.4360</option>
    <option value="133.7751">133.7751</option>
    <option value="106.3468">106.3468</option>
</select>

<select id="longitude">
    <option value="55.3781">55.3781</option>
    <option value="25.2744">25.2744</option>
    <option value="56.1304">56.1304</option>
</select>

Also, if you exclude the value attribute then the text inside the option is considered its value. The markup below is equivalent to the one above.

<select id="latitude">
    <option>3.4360</option>
    <option>133.7751</option>
    <option>106.3468</option>
</select>

<select id="longitude">
    <option>55.3781</option>
    <option>25.2744</option>
    <option>56.1304</option>
</select>

Latitude and longitude values aren't integers so you cannot use parseInt on them, also there is no reason to parse them as numeric values as they will be converted back to String to be sent in the ajax call

$.ajax({
    url: "libs/php/getCountryInfo.php",
    type: 'POST',
    dataType: 'json',
    data: {
            lat: $('#latitude').val(),
            lng: $('#longitude').val()
    },
    success: function(result) {

        console.log(JSON.stringify(result));

        if (result.status.name == "ok") {

            $('#txtDistance').html(result['data'][0]['distance']);
            $('#txtGeonameID').html(result['data'][0]['geonameid']);
            $('#txtName').html(result['data'][0]['name']);
        }
        
    },
    error: function(jqXHR, textStatus, errorThrown) {
            // your error code
    }
}); 
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda