I'm having a problem putting points on the map (I'm using the Google Maps API), the data is coming from the database correctly (I tested echoing on another page), but the points are not being created. (urgent for TCC).
generateMap.php
require_once './conexao.php';
require_once './gerarCoordenadas.php';
session_start();
function parseToXML($htmlStr) {
$xmlStr = str_replace('<', '<', $htmlStr);
$xmlStr = str_replace('>', '>', $xmlStr);
$xmlStr = str_replace('"', '"', $xmlStr);
$xmlStr = str_replace("'", ''', $xmlStr);
$xmlStr = str_replace("&", '&', $xmlStr);
return $xmlStr;
}
mysqli_query($conexao, "SET NAMES 'utf8'");
mysqli_query($conexao, 'SET character_set_connection=utf8');
mysqli_query($conexao, 'SET character_set_client=utf8');
mysqli_query($conexao, 'SET character_set_results=utf8');
// Select all the rows in the markers table
$sql = "SELECT * FROM listarEnderecoMapa WHERE cpf_cliente = 50374864837";
$resultado = mysqli_query($conexao, $sql);
// header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row = mysqli_fetch_assoc($resultado)) {
$coords = limparCoordenada($row['coord']);
$endereco = $row['logradouro'] . ', ' . $row['num'] . ', ' . $row['bairro'] . ', ' . $row['cidade'] . ' - ' . $row['estado'];
$_SESSION['mapa'] = $endereco . '/' . $coords[0] . '/' . $coords[1];
ECHO $endereco . '/' . $coords[0] . '/' . $coords[1];
// Add to XML document node
echo '<marker ';
echo 'name="' . parseToXML($row['nome']) . '" ';
echo 'address="' . parseToXML($endereco) . '" ';
echo 'lat="' . parseToXML($coords[0]) . '" ';
echo 'lng="' . parseToXML($coords[1]) . '" ';
echo 'type="' . parseToXML($row['id_tp_usuario']) . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
teste_mapa.php
<?php
require_once './gerarCoordenadas.php';
require_once './banco-chamado.php';
session_start();
$user = 'leandro';// $_SESSION['leandro'];
$cpf = pegaCpfUsuarioLogado($conexao, $user);
$coord = gerarCoordenadaEndTec($conexao, $cpf);
// aqui fecha o php (estava dando bug no stack quando eu coloco a tag
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Using MySQL and PHP with Google Maps</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
var customLabel = {
restaurant: {
label: 'R'
},
bar: {
label: 'B'
}
};
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
// está retornando -23.5990514/-46.912299
center: new google.maps.LatLng(<?php echo $coord[0].', '. $coord[1]; ?>),
zoom: 12
});
var infoWindow = new google.maps.InfoWindow;
downloadUrl('resultado.php', function(data) {
var xml = data.responseXML;
var markers = xml.documentElement.getElementsByTagName('marker');
Array.prototype.forEach.call(markers, function(markerElem) {
var name = markerElem.getAttribute('name');
var address = markerElem.getAttribute('address');
var type = markerElem.getAttribute('type');
var point = new google.maps.LatLng(
parseFloat(markerElem.getAttribute('lat')),
parseFloat(markerElem.getAttribute('lng')));
var infowincontent = document.createElement('div');
var strong = document.createElement('strong');
strong.textContent = name
infowincontent.appendChild(strong);
infowincontent.appendChild(document.createElement('br'));
var text = document.createElement('text');
text.textContent = address
infowincontent.appendChild(text);
var icon = customLabel[type] || {};
var marker = new google.maps.Marker({
map: map,
position: point,
label: icon.label
});
marker.addListener('click', function() {
infoWindow.setContent(infowincontent);
infoWindow.open(map, marker);
});
});
});
}
function downloadUrl(url, callback) {
var request = window.ActiveXObject ?
new ActiveXObject('Microsoft.XMLHTTP') :
new XMLHttpRequest;
request.onreadystatechange = function() {
if (request.readyState == 4) {
request.onreadystatechange = doNothing;
callback(request, request.status);
}
};
request.open('GET', url, true);
request.send(null);
}
function doNothing() {}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBoulP8cXm0-wo9kFtNIf4nAsRuC2g1xcU&callback=initMap"></script></body></html>
Generate.php
<?phpfunctiongerarCoordenadaEndTec($conexao,$cpf){$sql="select ST_AsText(coordenadas) as coord from tbl_endereco where cpf_usuario = $cpf";
$resultado = mysqli_query($conexao, $sql);
$row = mysqli_fetch_array($resultado, MYSQLI_ASSOC);
$coord = $row['coord'];
$coord = limparCoordenada($coord);
return $coord;
}
function limparCoordenada($addr) {
$itemsLimpar = array("POINT(", ")");
$addr = str_replace($itemsLimpar, "", $addr);
$coordLimpa = explode(" ", $addr);
return $coordLimpa;
}