tener un mapa de Google V3, no se puede actualizar map.setCenter desde la entrada del usuario

StackOverflow https://stackoverflow.com/questions/8901242

  •  29-10-2019
  •  | 
  •  

Pregunta

Soy nuevo en esto, pero tengo un mapa de Google V3 que se carga de la información de geocodos del navegador W3C del usuario, y saco el país, el estado y la ciudad del objeto Geocode y actualizan los cuadros de entrada. Si el usuario ingresa una dirección de la calle, quiero geocodificarla a través de Google y cambiar MAP.SetCenter y SetZoom, y mostrar el mapa actualizado. Cuando eso funciona, quiero agregar un marcador e infowindow. A pesar de las horas de investigación y los ensayos, no puedo hacer que funcione el geocódigo y la actualización. La herramienta de desarrollador en Chrome parece indicar que la ejecución cesa/falla en la línea geocoder.geocode que se indica en negrita a continuación. Aquí hay código relevante.

var map, geocoder, marker, infowindow; //global variables

function initializeMap() {
// Try W3C Geolocation to geolocate desktop user
//initialize Google map, geolocate desktop user, and display after page loads
//find country, state, and city for the user's location  -  this all works
}
window.onload = initializeMap;

//change the Google Map after a user enters a street address
$("#streetAddress").blur(function() {
    //if state, city and street address locations are present
if ( $(this).val().length > 0 && $("#state").val().length > 0 && $("#city3").val().length > 0  ) {
    var gAddress =  [$(this).val() + ", " + $("#city3").val() + ", " + $("#state").val()] ;
    //get coordinates of this address
    //if no geocode object exists, create one
    if (!geocoder) {
    geocoder = new google.maps.Geocoder(); //create Geocoder object else use existing one from initializeMap() ?
    }       
    //create a GeocoderRequest object with user's street address
    var geoCoderRequest = {address:  gAddress} 
    //make a Geocoder request
    geocoder.geocode( geoCoderRequest, function(results, status) {  **//this line fails**
    //check if status is OK
            if ( status === google.maps.GeocoderStatus.OK) {
            //update center of existing map on location returned for this address
            map.setCenter(results[0].geometry.location);
            map.setZoom(14);
            }   
    });         
    } else {
            return; //no action if gAddress is incomplete
        }
});

No hay solución correcta

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top