-1
Hello, I am learning to use google maps and wanted to trace routes between a location saved in the database and the location of who enter the page(get customer’s address), can anyone help me? Currently I already have the code that allows to create points on the map through BD. The filenames are: config.php implementacao.php geo.php
DATABASE USED:
CREATE DATABASE DB_MARCADORES CREATE TABLE TB_MARCAS ( MARC_CODIGO INT NOT NULL AUTO_INCREMENT PRIMARY KEY , MARC_NOME VARCHAR( 60 ) NOT NULL , MARC_ENDERECO VARCHAR( 80 ) NOT NULL , MARC_LATITUDE FLOAT( 10, 6 ) NOT NULL , MARC_LONGITUDE FLOAT( 10, 6 ) NOT NULL , MARC_TIPO VARCHAR( 30 ) NOT NULL ) ENGINE = MYISAM ;
NOTE: the format of the address follows the pattern: N° of the house Street Name of the Street
<?php
$endereco="localhost";
$usuario="root";
$senha="usbw";
$banco="DB_MARCADORES";
$MySQLi=new mysqli($endereco,$usuario,$senha,$banco,3307);
if(mysqli_connect_errno()){
die(mysqli_connect_error());
exit();
}
mysqli_set_charset($MySQLi,"utf8");
session_start();
?>
<?php
require("config.php");
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;
}
// Select all the rows in the markers table
$result_markers = "SELECT * FROM TB_MARCAS";
$resultado_markers = mysqli_query($MySQLi, $result_markers);
header("Content-type: text/xml");
// Start XML file, echo parent node
echo '<markers>';
// Iterate through the rows, printing XML nodes for each
while ($row_markers = mysqli_fetch_assoc($resultado_markers)){
// Add to XML document node
echo '<marker ';
echo 'name="' . parseToXML($row_markers['MARC_NOME']) . '" ';
echo 'address="' . parseToXML($row_markers['MARC_ENDERECO']) . '" ';
echo 'lat="' . $row_markers['MARC_LATITUDE'] . '" ';
echo 'lng="' . $row_markers['MARC_LONGITUDE'] . '" ';
echo 'type="' . $row_markers['MARC_TIPO'] . '" ';
echo '/>';
}
// End XML file
echo '</markers>';
<div id="map"></div>
<script>
var customLabel = {
restaurant: {
label: 'R'
},
bar: {
label: 'B'
}
};
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(-11.371964, -59.239523),
zoom: 3
});
var infoWindow = new google.maps.InfoWindow;
// Change this depending on the name of your PHP or XML file
downloadUrl('implementacao.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>