Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

184
Vistas
google api key as a variable

<script src="jquery.js"></script>

<script>
$.get('http://localhost/ajax_send_key.php',function (data){
    api_key = data;
});
</script>

<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key= - my api key - &libraries=places">
// this one work

// src="https://maps.googleapis.com/maps/api/js?key={api_key}&libraries=places">
// Google Maps JavaScript API error: InvalidKeyMapError

// src="https://maps.googleapis.com/maps/api/js?key=" + api_key + "&libraries=places">
// TypeError: Cannot read properties of undefined (reading 'Autocomplete') at initialize

</script>

<script>
lat = '';
function initialize() { // google places wohl für ortseingabe und dann vorschläge
  var input = document.getElementById('in');
  var places = new google.maps.places.Autocomplete(input);
    google.maps.event.addListener(places, 'place_changed', function () {
       var place = places.getPlace();   
       var lat = place.geometry.location.lat();
    });     
    return (lat);
}   
window.addEventListener('load', initialize);
</script>

<html>
input
<input id='in' type = 'text' STYLE = 'width: 220px' ;></input>
</html>

hello,

I have a problem with my Api Key, when I try to use it as a variable. how do I put it into the src="https://maps.googleapis.com/maps/api/js?key=
It works if it is not a variable.

thanks georg

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Don't create the script tag on load. Wait for the key response to come back first before creating it, and then you can create the src appropriately. You'll also have to wait for Google's script to load before calling initialize.

$.get('http://localhost/ajax_send_key.php',function (data){
    const script = document.body.appendChild(document.createElement('script'));
    script.src = `https://maps.googleapis.com/maps/api/js?key=${data}&libraries=places`;
    script.addEventListener('load', initialize);
});
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda