1

I'm having trouble getting the position of an address. However, this problem occurs only the first time that I run, ie it is necessary that every time I make the query, I click twice, for only the second values are obtained.

I believe the problem is due to be asynchronous method. But I am not able to solve the problem. Some of his friends could help me.

$('#btnTracar').click(function(){
if (geocoder){
    geocoder.geocode({ 'address': document.getElementById('txtStart').value }, function(results, status){
        if (status == google.maps.GeocoderStatus.OK) {
            mapStart = results[0].geometry.location;
        } else { alert("Não foi possível carregar a localização. \nDescrição do Erro: " + status); }
    });

    geocoder.geocode({ 'address': document.getElementById('txtEnd').value }, function(results, status){
        if (status == google.maps.GeocoderStatus.OK) {
            mapEnd = results[0].geometry.location;
        } else { alert("Não foi possível carregar a localização. \nDescrição do Erro: " + status); }
    });

    calcularRota();
}
});

1 Answer 1

1

Solution:

        $('#btnTracar').click(function(){
            if ($.trim($("#txtStart").val()) == ""){
                alert("Favor preencher o campo de Origem Corretamente.");
                return;
            }

            if ($.trim($("#txtEnd").val()) == ""){
                alert("Favor preencher o campo de Origem Corretamente.");
                return;
            }

            if (geocoder){
                geocoder.geocode({ 'address': document.getElementById('txtStart').value }, function(results, status){
                    if (status == google.maps.GeocoderStatus.OK){
                        mapStart = results[0].geometry.location;
                        geocoder.geocode({ 'address': document.getElementById('txtEnd').value }, function(results, status){
                            if (status == google.maps.GeocoderStatus.OK){
                                mapEnd = results[0].geometry.location;
                                calcularRota();
                            }
                        });
                    }
                });
            }
        });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.