1

JavaScript Google Maps API V3

I'm trying to convert the address to Latitude/Longitude using geocode from Google Maps API v3 but it's not working.

The rest of the code is working fine.

I'm sure this a very simple problem !!

the code

        var map;
    function initialize() {
            var infoWindow = new google.maps.InfoWindow;
            var myLatLng = new google.maps.LatLng(-23.000516, -43.413473);
            var myOptions = {
                zoom: 15,
                center: myLatLng,
                mapTypeId: google.maps.MapTypeId.ROADMAP
            };

    map = new google.maps.Map(document.getElementById('mapa'), myOptions);

        // Download do XML
        downloadUrl("http://podium.pvidesign.com.br/mapa/casas.xml", function(data) {
            var icone = 'http://podium.pvidesign.com.br/mapa/imagens/casa.png';
            var imoveis = data.documentElement.getElementsByTagName("imovel");
                for (var i = 0; i < imoveis.length; i++) {
                    //var latlng = new google.maps.LatLng(parseFloat(imoveis[i].getAttribute("lat")), parseFloat(imoveis[i].getAttribute("lng")));
                    var logradouro = imoveis[i].getAttribute("logradouro");
                    var endereco = imoveis[i].getAttribute("endereco");
                    var numero = imoveis[i].getAttribute("numero");
                    var bairro = imoveis[i].getAttribute("bairro");
                    var cidade = imoveis[i].getAttribute("cidade");
                    var imagem = imoveis[i].getAttribute("img");
                    var end_completo = logradouro + " " + endereco + ", " + numero + ", " + bairro + ", " + cidade;
                    var nome = imoveis[i].getAttribute("nome");
                    var desc = imoveis[i].getAttribute("desc");
                    var html = "<b>" + nome + "</b><br>" + "<img src='" + imagem + "'><br>" + desc + "<br>" + end_completo;

                    // Coletando Lat e Long atraves do endereço
                    var geocoder = new google.maps.Geocoder();
                    geocoder.geocode( {'address': end_completo}, function(results, status) {

                            var latLong = results[0].geometry.location;

                    }); // Fim Coletando

                    var houseMarker = new google.maps.Marker({
                        title:nome,
                        position:latLong,
                        map:map,
                        icon:icone
                    });
                bindInfoWindow(houseMarker, map, infoWindow, html);
                } 
            }); 

        // Balão de informação
        function bindInfoWindow(houseMarker, map, infoWindow, html) {
            google.maps.event.addListener(houseMarker, 'mouseover', function() {
            infoWindow.setContent(html);
            infoWindow.open(map, houseMarker);
            });
            google.maps.event.addListener(houseMarker, 'mouseout', function() {
            infoWindow.close();
            });
        };

    }; 
Kara
  • 6,115
  • 16
  • 50
  • 57

1 Answers1

2

Translating from Portuguese (wild guess) it looks like you're trying to geocode an address that contains: street, address, number, neighborhood and city. It may be too much, I'd try with just street, number, city and see if that helps.

Also, your code is very optimistic, there is no check for status code in the response. You should be checking for Status Codes in the response, if it isn't google.maps.GeocoderStatus.OK then there won't be any result in the response.

Try the Google Maps API v3 Geocoder Tool with different sample inputs (from your data) to see how it behaves, you might find issues with how you are formatting the address, the data itself, etc. Try to make your addresses look like what you'd use to ship a package.

Also be aware that your approach will only be able to geocode up to 10 address when loading the map. If you need to show more, you probably need to use server-side geocoding. Have a look at the Geocoding Strategies article and make sure to use the right service for your use case.

miguev
  • 4,481
  • 21
  • 41