Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html >
- <head>
- <meta name="viewport" content="width=device-width,initial-scale=1">
- <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 bodyHeight = $('body').height();
- $("#map").css('height',bodyHeight);
- var map = new google.maps.Map(document.getElementById('map'), {
- center: new google.maps.LatLng(-33.863276, 151.207977),
- zoom: 12
- });
- var infoWindow = new google.maps.InfoWindow;
- // Change this depending on the name of your PHP or XML file URL:https://storage.googleapis.com/mapsdevsite/json/mapmarkers2.xml
- downloadUrl('php/json.php', function(data) {
- var xml = data.responseXML;
- var markers = xml.documentElement.getElementsByTagName('marker');
- Array.prototype.forEach.call(marker, function(markerElem) {
- var title = markerElem.getAttribute('title');
- var comment = markerElem.getAttribute('comment');
- //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 = title
- infowincontent.appendChild(strong);
- infowincontent.appendChild(document.createElement('br'));
- var text = document.createElement('text');
- text.textContent = comment
- 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=******&callback=initMap">
- </script>
- </body>
- </html>
- <?php
- ini_set("display_errors", On);
- error_reporting(E_ALL);
- require("phpsqlajax_dbinfo.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;
- }
- // Opens a connection to a MySQL server
- $connection=mysql_connect ('localhost', $username, $password);
- if (!$connection) {
- die('Not connected : ' . mysql_error());
- }
- // Set the active MySQL database
- $db_selected = mysql_select_db($database, $connection);
- if (!$db_selected) {
- die ('Can't use db : ' . mysql_error());
- }
- // Select all the rows in the markers table
- $query = "SELECT * FROM marker WHERE 1";
- $result = mysql_query($query);
- if (!$result) {
- die('Invalid query: ' . mysql_error());
- }
- header("Content-type: text/xml");
- // Start XML file, echo parent node
- echo '<markers>';
- // Iterate through the rows, printing XML nodes for each
- while ($row = @mysql_fetch_assoc($result)){
- // Add to XML document node
- echo '<marker ';
- echo 'title="' . parseToXML($row['name']) . '" ';
- echo 'comment="' . parseToXML($row['address']) . '" ';
- echo 'lat="' . $row['Latitude'] . '" ';
- echo 'lng="' . $row['Longitude'] . '" ';
- //echo 'type="' . $row['type'] . '" ';
- echo '/>';
- }
- // End XML file
- echo '</markers>';
- ?>
Add Comment
Please, Sign In to add comment