Hello! It is necessary that when new coordinates are added to the database, the marker is placed on the map without refreshing the page. I have not worked with Ajax before (if it is for this), so I find it difficult to implement, I will be grateful for the help. The page to generate a marker in the database (from Google documentation):
<?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; } $result = $pdo->query("SELECT * FROM markers WHERE 1"); header("Content-type: text/xml"); // Start XML file, echo parent node echo '<markers>'; // Iterate through the rows, printing XML nodes for each while ($row = $result->fetch()){ // ADD TO XML DOCUMENT NODE echo '<marker '; echo 'id="' . $row['id'] . '" '; echo 'address="' . parseToXML($row['address']) . '" '; echo 'description="' . parseToXML($row['description']) . '" '; echo 'lat="' . $row['lat'] . '" '; echo 'lng="' . $row['lng'] . '" '; echo 'username="' . parseToXML($row['username']) . '" '; echo '/>'; } // End XML file echo '</markers>'; Map page of the map itself and markers:
<?php require_once "config.php" ?> <!DOCTYPE HTML> <html> <head> <title>Los Santos Police Department - 911 Online Map</title> <!-- Disallow users to scale this page --> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <link rel="shortcut icon" href="https://www.lacity.org/sites/g/files/wph571/themes/site/favicon.ico" type="image/vnd.microsoft.icon" /> <style type="text/css"> /* Allow the canvas to use the full height and have no margins */ html, body, #map-canvas { height: 100%; margin: 0 } </style> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> </head> <body> <!-- The container the map is rendered in --> <div id="map-canvas"></div> <!-- Load all javascript --> <script src="http://maps.google.com/maps/api/js?sensor=false"></script> <script src="js/SanMap.min.js"></script> <script> var customIcons = { restaurant: { icon: 'http://labs.google.com/ridefinder/images/mm_20_blue.png', shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' }, bar: { icon: 'http://labs.google.com/ridefinder/images/mm_20_red.png', shadow: 'http://labs.google.com/ridefinder/images/mm_20_shadow.png' } }; var infoWindow = new google.maps.InfoWindow; downloadUrl("genxml.php", function(data) { var xml = data.responseXML; var markers = xml.documentElement.getElementsByTagName("marker"); for (var i = 0; i < markers.length; i++) { var address = markers[i].getAttribute("address"); var description = markers[i].getAttribute("description"); var username = markers[i].getAttribute("username"); var point = new SanMap.getLatLngFromPos( parseFloat(markers[i].getAttribute("lat")), parseFloat(markers[i].getAttribute("lng"))); var html = "<strong>911 ВЫЗОВ: </strong>" + username + "<br/>" + "<strong>ЛОКАЦИЯ: </strong>" + address + "<br/>" + "<strong>ОПИСАНИЕ: </strong>" + description; var icon = customIcons[username] || {}; var marker = new google.maps.Marker({ map: map, position: point, icon: icon.icon, shadow: icon.shadow }); bindInfoWindow(marker, map, infoWindow, html); } }); function bindInfoWindow(marker, map, infoWindow, html) { google.maps.event.addListener(marker, 'click', function() { infoWindow.setContent(html); 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() {} var mapType = new SanMapType(2, 5, function (zoom, x, y) { return x == -1 && y == -1 ? "tiles/map.outer.png" : "tiles/sanandreas." + zoom + "." + x + "." + y + ".png";//Where the tiles are located }); var satType = new SanMapType(2, 3, function (zoom, x, y) { return x == -1 && y == -1 ? null : "tiles/map." + zoom + "." + x + "." + y + ".png";//Where the tiles are located }); var map = SanMap.createMap(document.getElementById('map-canvas'), {'Карта': mapType, 'Спутник': satType }, 2, null, false, 'Спутник'); </script> </body> SanMap is a map for a game that allows you to work with a game map, not our Earth.
ajax- whether the data in the database has changed - every 5 seconds, for example. If changed - redraw markers. You can go in the direction of the web-socket "ov. - Moonvvell