Estoy tratando de mostrar un marcador en mi mapa de folletos. Esto me da el siguiente error: TypeError: t is null
Código PHP que utiliza la API de Google Maps para obtener coordenadas:
<?php if (isset($_POST["checkAddress"])) { //Checks if action value exists $checkAddress = $_POST["checkAddress"]; $plus = str_replace(" ", "+", $checkAddress); $json = file_get_contents('https://maps.googleapis.com/maps/api/geocode/json?address=' . $plus . '&key=KEY'); $obj = json_decode($json); $mapLat = $obj->results[0]->geometry->location->lat; $mapLng = $obj->results[0]->geometry->location->lng; $coords = ('' . $mapLat . ', ' . $mapLng . ''); echo $coords; } ?>jQuery que ejecuta el script PHP y debe mostrar las coordenadas en el mapa del folleto:
$(document).ready(function() { $("#button").click(function(){ $.ajax({ url: "geo.php", method: "POST", data: { checkAddress: $("#largetxt").val() }, success: function(response){ console.log(response); var marker = L.marker([response]).addTo(map); } }); }); });Oh, eres tú otra vez.
Estás recibiendo una cadena como respuesta.
L.marker espera [lat, lng] pero le estás dando ["lat, lng"] Una cadena en lugar de dos flotantes.
Para arreglar esto en JavaScript:
success: function(response){ var coordinates = response.split(", "); //create an array containing lat and lng as strings coordinates[0] = parseFloat(coordinates[0]); //convert lat string to number coordinates[1] = parseFloat(coordinates[1]); //convert lng string to number var marker = L.marker(coordinates).addTo(map); }