none
ajuda com java script RRS feed

  • Pergunta

  • boa tarde

    		var geocoder;
            var map;
            
            function FindLocation() {
                geocoder = new google.maps.Geocoder();
                geocoder.geocode({ 'location': marker.getPosition() }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        codeAddress();
                    }
                    else {
                        alert("Geocode was not successful for the following reason: " + status);
                    }
                });
            }
    
            function initialize() {
                if (typeof (google) != "undefined") {
                    geocoder = new google.maps.Geocoder();
                    var options =
                    {
                        zoom: 17,
                        center: new google.maps.LatLng(0, 0),
                        mapTypeId: google.maps.MapTypeId.HYBRID,
                        mapTypeControl: true,
                        mapTypeControlOptions:
                        {
                            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
                            poistion: google.maps.ControlPosition.TOP_RIGHT,
                            mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.SATELLITE]
                        },
                        navigationControl: true,
                        navigationControlOptions:
                        {
                            style: google.maps.NavigationControlStyle.ZOOM_PAN
                        },
                        scaleControl: true,
                        disableDoubleClickZoom: true,
                        streetViewControl: false,
                        draggableCursor: 'move'
                    };
    
                    map = new google.maps.Map(document.getElementById("divGeoPosicao"), options);
    
                    google.maps.event.addListener(map, 'click', function (e) {
                        //Determine the location where the user has clicked.
                        var location = e.latLng;
    
                        if (marker == null) {
                            //Create a marker and placed it on the map.
                            AdicionarMarket(location);
                        }
                        else {
                            marker.setPosition(location);
                        }
    
                        
                        FindLocation();
                    });
                    codeAddress();
                }
            }
            google.maps.event.addDomListener(window, "load", initialize);
    
            function codeAddress() {
                if (typeof (google) != "undefined") {
                    var address = document.getElementsByName("Endereco").Value;
                    geocoder.geocode({ 'address': address }, function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            map.setCenter(results[0].geometry.location);
                            if (marker)
                                marker.setMap(null);
                          
                        }
                        //else {
                        //    alert('Geocode was not successful for the following reason: ' + status);
                        //}
                    });
                }
            }
    
    
            function GeoPosicao(Endereco) {
                $('#divGeoPosicao').show();
                document.getElementsByName("Endereco").Value = Endereco;
                codeAddress();
                
                if (typeof (google) != "undefined") {
                    google.maps.event.trigger(map, 'resize');
                }
            }

    quando eu clico num botao, entra na function GeoPosicao;

    quando cai na function codeAddress(), acusa um erro que o 

    geocoder.geocode

    não existe....

    eu coloquei a chamada da api no script

    alguma ajuda?

    quarta-feira, 29 de abril de 2015 19:36

Todas as Respostas

  • Tente isso:

    var geocoder = new google.maps.Geocoder();
    var map;
            
    function FindLocation() {
        //geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'location': marker.getPosition() }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                codeAddress();
            }
            else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
    Instanciei o geocoder em outro ponto.


    Att. Andre de Mattos Ferraz


    quarta-feira, 29 de abril de 2015 19:43
  • funcionou ... agora ocorre outro erro... 

       function GeoPosicao(Endereco) {
                $('#divGeoPosicao').show();
                document.getElementsByName("Endereco").Value = Endereco;
                codeAddress();
                
                if (typeof (google) != "undefined") {
                    google.maps.event.trigger(map, 'resize');
                }
            }

    avisa que o map esta "undefined", porém ele foi definido na function initialize

    quarta-feira, 29 de abril de 2015 19:57
  • No seu código original tem uma função "initialize", chame ela logo no ínicio (esqueça aquela alteração que sugeri).

    	var geocoder;
            var map;
            
            function FindLocation() {
                geocoder = new google.maps.Geocoder();
                geocoder.geocode({ 'location': marker.getPosition() }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        codeAddress();
                    }
                    else {
                        alert("Geocode was not successful for the following reason: " + status);
                    }
                });
            }
    
            function initialize() {
                if (typeof (google) != "undefined") {
                    geocoder = new google.maps.Geocoder();
                    var options =
                    {
                        zoom: 17,
                        center: new google.maps.LatLng(0, 0),
                        mapTypeId: google.maps.MapTypeId.HYBRID,
                        mapTypeControl: true,
                        mapTypeControlOptions:
                        {
                            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
                            poistion: google.maps.ControlPosition.TOP_RIGHT,
                            mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.SATELLITE]
                        },
                        navigationControl: true,
                        navigationControlOptions:
                        {
                            style: google.maps.NavigationControlStyle.ZOOM_PAN
                        },
                        scaleControl: true,
                        disableDoubleClickZoom: true,
                        streetViewControl: false,
                        draggableCursor: 'move'
                    };
    
                    map = new google.maps.Map(document.getElementById("divGeoPosicao"), options);
    
                    google.maps.event.addListener(map, 'click', function (e) {
                        //Determine the location where the user has clicked.
                        var location = e.latLng;
    
                        if (marker == null) {
                            //Create a marker and placed it on the map.
                            AdicionarMarket(location);
                        }
                        else {
                            marker.setPosition(location);
                        }
    
                        
                        FindLocation();
                    });
                    codeAddress();
                }
            }
            google.maps.event.addDomListener(window, "load", initialize);
    
            function codeAddress() {
                if (typeof (google) != "undefined") {
                    var address = document.getElementsByName("Endereco").Value;
                    geocoder.geocode({ 'address': address }, function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            map.setCenter(results[0].geometry.location);
                            if (marker)
                                marker.setMap(null);
                          
                        }
                        //else {
                        //    alert('Geocode was not successful for the following reason: ' + status);
                        //}
                    });
                }
            }
    
    
            function GeoPosicao(Endereco) {
                $('#divGeoPosicao').show();
                document.getElementsByName("Endereco").Value = Endereco;
                codeAddress();
                
                if (typeof (google) != "undefined") {
                    google.maps.event.trigger(map, 'resize');
                }
            }
    
    initialize();

    Coloquei ela no final do script, acho que isso vai resolver.


    Att. Andre de Mattos Ferraz

    quarta-feira, 29 de abril de 2015 20:17
  • não deu certo... 
    quarta-feira, 29 de abril de 2015 20:38
  • mas de algum erro?

    Att. Andre de Mattos Ferraz

    quarta-feira, 29 de abril de 2015 20:54
  • coloquei uma flag.. pq no post eu perdia o valor do map

    ficou assim:

    var geocoder = new google.maps.Geocoder();
         var map;
         
            var flagInitialize = false;
            
            function FindLocation() {
                geocoder = new google.maps.Geocoder();
                geocoder.geocode({ 'location': marker.getPosition() }, function (results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        codeAddress();
                    }
                    else {
                        alert("Geocode was not successful for the following reason: " + status);
                    }
                });
            }
    
            function initialize() {
                flagInitialize = true;
                if (typeof (google) != "undefined") {
                    //geocoder = new google.maps.Geocoder();
                    var options =
                    {
                        zoom: 17,
                        center: new google.maps.LatLng(0, 0),
                        mapTypeId: google.maps.MapTypeId.HYBRID,
                        mapTypeControl: true,
                        mapTypeControlOptions:
                        {
                            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
                            poistion: google.maps.ControlPosition.TOP_RIGHT,
                            mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.SATELLITE]
                        },
                        navigationControl: true,
                        navigationControlOptions:
                        {
                            style: google.maps.NavigationControlStyle.ZOOM_PAN
                        },
                        scaleControl: true,
                        disableDoubleClickZoom: true,
                        streetViewControl: false,
                        draggableCursor: 'move'
                    };
    
                    map = new google.maps.Map(document.getElementById("divGeoPosicao"), options);
    
                    google.maps.event.addListener(map, 'click', function (e) {
                        //Determine the location where the user has clicked.
                        var location = e.latLng;
    
                        if (marker == null) {
                            //Create a marker and placed it on the map.
                            AdicionarMarket(location);
                        }
                        else {
                            marker.setPosition(location);
                        }
    
                        //EscreverPosicaoLatitude();
                        FindLocation();
                    });
                    codeAddress();
                }
            }
            google.maps.event.addDomListener(window, "load", initialize);
    
            function codeAddress() {
    
                var options;
                if (flagInitialize == false) {
                    options =
                    {
                        zoom: 17,
                        center: new google.maps.LatLng(0, 0),
                        mapTypeId: google.maps.MapTypeId.HYBRID,
                        mapTypeControl: true,
                        mapTypeControlOptions:
                        {
                            style: google.maps.MapTypeControlStyle.DROPDOWN_MENU,
                            poistion: google.maps.ControlPosition.TOP_RIGHT,
                            mapTypeIds: [google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.TERRAIN, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.SATELLITE]
                        },
                        navigationControl: true,
                        navigationControlOptions:
                        {
                            style: google.maps.NavigationControlStyle.ZOOM_PAN
                        },
                        scaleControl: true,
                        disableDoubleClickZoom: true,
                        streetViewControl: false,
                        draggableCursor: 'move'
                    };
    
                    map = new google.maps.Map(document.getElementById("divGeoPosicao"), options);
                }
    
                if (typeof (google) != "undefined") {
                    var address = document.getElementsByName("Endereco").Value;
                    geocoder.geocode({ 'address': address }, function (results, status) {
                        if (status == google.maps.GeocoderStatus.OK) {
                            map.setCenter(results[0].geometry.location);
                            if (marker)
                                marker.setMap(null);
                            
                        }
                        //else {
                        //    alert('Geocode was not successful for the following reason: ' + status);
                        //}
                    });
                }
            }
    
    
            function GeoPosicao(Endereco) {
              
    
                $('#divGeoPosicao').show();
                document.getElementsByName("Endereco").Value = Endereco;
                codeAddress();
    
                if (typeof (google) != "undefined") {
                    google.maps.event.trigger(map, 'resize');
                }
            }

    parou de dar erro... mas o mapa que precisa exibir no div geo posicao.. nao aparece.. estou vendo poruqe..

    pelo visto;;; no post nao entra no initialize

    divGeoPosicao

    • Editado Hawk B quarta-feira, 29 de abril de 2015 21:00
    quarta-feira, 29 de abril de 2015 20:59
  • Onde você está chamando esses scripts? No head do html ou dentro da tag body? Se estiver no head tente colocá-lo dentro do body.

    Att. Andre de Mattos Ferraz

    quinta-feira, 30 de abril de 2015 11:24