1

Criei uma barra para o usuário entrar com o local de destino desejado. Esse endereço é capturado através da função click() do jQuery.

Até esse ponto o código está funcionando, porém, por algum motivo, a função de geodecodificação da API não está funcionando.

JavaScript:

var map;
var startPoint;
var endPoint;
var geocoder;

/****************************************************   
    gets user's location to set up starPoint and
    displays google maps
/****************************************************/

function initialize() {  
    var marker = null;
    var geoMarker = null;
    directionsDisplay = new google.maps.DirectionsRenderer(); 

    /****************************************************
        defines user's start location
    /****************************************************/
    var mapOptions = {                                                                                                                                    
        zoom: 16,
        panControl: true                                                                
    }

    map = new google.maps.Map(document.getElementById('map-canvas'),
    mapOptions);

    if(navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            startPoint = new google.maps.LatLng(position.coords.latitude,
                                   position.coords.longitude);

            map.setCenter(startPoint);

            /****************************************************   
                mark on the map user's current position
            /****************************************************/
            // marker = new google.maps.Marker( {                                               
                // position: startPoint,                
                // map: map,                                                                    
                // title: "startPoint",
                // zIndex: 1

            // });   

            geoMarker = new GeolocationMarker(map);
            geoMarker.setMinimumAccuracy(100);

        }, function() {
            handleNoGeolocation(true);
        });
    } 
    else {
        handleNoGeolocation(false);
    }   
}       

function handleNoGeolocation(errorFlag) {
    if (errorFlag) {
        alert('Error: The Geolocation service failed.');
    } else {
        alert('Error: Your browser doesn\'t support geolocation.');
    }
}

$(document).ready(function(){
    initialize(); // Esta função inicializa o mapa.

    $("#searchButton").click(function() {
        if ($("#address").val() != "") {
            geocoder = new google.maps.Geocoder();
            endPoint = $("#address").val();
            geocoder.geocode({address: endPoint}, function(result, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    alert("geocode ok");
                }
            });
        }
    });
});

Na função de callback, testo se a geodecodificação funcionou, o que nunca acontece.

HTML:

<body>
    <div id="form">
        <form name="addressBar" method="post" action="teste.html" style="z-index: 1">
            <fieldset> 
                <div class="fields">
                    <label for="">Address:</label> 
                    <input type="text" name="addressBar" id="address">
                    <input type="image" name="searchButton" id="searchButton" 
                    src="img/searchButton.jpg">
                </div>
            </fieldset>
        </form>
    </div>
    <div id="map-canvas" style="position: absolute; top: 0; width: 100%; height: 100%; z-index: 0">
    </div>
</body>
7
  • Qual erro que dá? você tem certeza que o código chega a chamar a api do maps? 16/06/2015 às 13:40
  • Sim ele chama. Consigo criar o mapa e marcar um ponto de referência. O erro é que a função de geodecodificação não funciona.
    – absentia
    16/06/2015 às 15:29
  • O que você quer dizer com "função de geodecodificação não funciona"? 16/06/2015 às 16:29
  • Ela deveria exibir `alert("geocode ok")´, mas o callback não é chamado.
    – absentia
    16/06/2015 às 21:54
  • E se você der um alert em status? 17/06/2015 às 12:46

0

Você deve fazer log-in para responder a esta pergunta.

Pesquise outras perguntas com a tag .