Jump to content
MakeWebGames

Recommended Posts

Posted

Hello, when I reach for an address it doesn't work!

	<!DOCTYPE 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>Locator</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 style="margin:0px; padding:0px;" onload="initMap()">
    <div>
         <label for="raddressInput">Search:</label>
         <input type="text" id="addressInput" size="15"/>
        <label for="radiusSelect">Radius:</label>
        <select id="radiusSelect" label="Radius">
          <option value="50" selected>50 Miles</option>
          <option value="30">30 Miles</option>
          <option value="20">20 Miles</option>
          <option value="10">10 Miles</option>
        </select>
	        <input type="button" id="searchButton" value="Search"/>
    </div>
    <div><select id="locationSelect" style="width: 10%; visibility: hidden"></select></div>
    
	
    
    
    
    
    <div id="map" style="width: 100%; height: 90%"></div>
    <script>
      var customLabel = {
        restaurant: {
          label: 'R'
        },
        bar: {
          label: 'B'
        }
      };
      
      var map;
      var markers = [];
      var infoWindow;
      var locationSelect;
	        function initMap() {
            
       /* var map = new google.maps.Map(document.getElementById('map'), {
          center: new google.maps.LatLng(-33.863276, 151.207977),
          zoom: 12
        });*/
        
          var sydney = {lat: -33.863276, lng: 151.207977};
          map = new google.maps.Map(document.getElementById('map'), {
            center: sydney,
            zoom: 11,
            mapTypeId: 'roadmap',
            mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}
          });
        
        
        
        
        var infoWindow = new google.maps.InfoWindow;
        
        
        searchButton = document.getElementById("searchButton").onclick = searchLocations;
	          locationSelect = document.getElementById("locationSelect");
          locationSelect.onchange = function() {
            var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
            if (markerNum != "none"){
              google.maps.event.trigger(markers[markerNum], 'click');
            }
          };
        
	       
          // Change this depending on the name of your PHP or XML file
          downloadUrl('markers.xml', function(data) {
            var xml = data.responseXML;
            var markers = xml.documentElement.getElementsByTagName('marker');
            Array.prototype.forEach.call(markers, function(markerElem) {
              var id = markerElem.getAttribute('id');
              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);
              infowincontent.appendChild(document.createElement('br'));
              
              var text = document.createElement('text');
              text.textContent = type
              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 createMarker(latlng, name, address) {
  var html = "<b>" + name + "</b> <br/>" + address;
  var marker = new google.maps.Marker({
    map: map,
    position: latlng
  });
  google.maps.event.addListener(marker, 'click', function() {
    infoWindow.setContent(html);
    infoWindow.open(map, marker);
  });
  markers.push(marker);
}
function createOption(name, distance, num) {
  var option = document.createElement("option");
  option.value = num;
  option.innerHTML = name;
  locationSelect.appendChild(option);
}       
	     function searchLocations() {
         var address = document.getElementById("addressInput").value;
         var geocoder = new google.maps.Geocoder();
         geocoder.geocode({address: address}, function(results, status) {
           if (status == google.maps.GeocoderStatus.OK) {
            searchLocationsNear(results[0].geometry.location);
           } else {
             alert(address + ' not found');
           }
         });
       }
       function searchLocationsNear(center) {
         clearLocations();
	         var radius = document.getElementById('radiusSelect').value;
         var searchUrl = 'locator.php?lat=' + center.lat() + '&lng=' + center.lng() + '&radius=' + radius;
         downloadUrl(searchUrl, function(data) {
           var xml = parseXml(data);
           var markerNodes = xml.documentElement.getElementsByTagName("marker");
           var bounds = new google.maps.LatLngBounds();
           for (var i = 0; i < markerNodes.length; i++) {
             var id = markerNodes[i].getAttribute("id");
             var name = markerNodes[i].getAttribute("name");
             var address = markerNodes[i].getAttribute("address");
             var distance = parseFloat(markerNodes[i].getAttribute("distance"));
             var latlng = new google.maps.LatLng(
                  parseFloat(markerNodes[i].getAttribute("lat")),
                  parseFloat(markerNodes[i].getAttribute("lng")));
	             createOption(name, distance, i);
             createMarker(latlng, name, address);
             bounds.extend(latlng);
           }
           map.fitBounds(bounds);
           locationSelect.style.visibility = "visible";
           locationSelect.onchange = function() {
             var markerNum = locationSelect.options[locationSelect.selectedIndex].value;
             google.maps.event.trigger(markers[markerNum], 'click');
           };
         });
       }
       
function clearLocations() {
  infoWindow.close();
  for (var i = 0; i < markers.length; i++) {
    markers[i].setMap(null);
  }
  markers.length = 0;
	  locationSelect.innerHTML = "";
  var option = document.createElement("option");
  option.value = "none";
  option.innerHTML = "See all results:";
  locationSelect.appendChild(option);
  locationSelect.style.visibility = "visible";
}       
       
       
       
       
       
       
       
      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=AIzaSyAiD-SlkJEGkpgDYr9m14O98uy-ulcB9sU&&callback=initMap">
    </script>
  </body>
</html>
	

 

locator.php - 

	<?php
error_reporting(E_ALL & ~E_NOTICE);
require("configuration/connect.php");
	// Start XML file, create parent node
	$doc = new DOMDocument('1.0', 'UTF-8');
$node = $doc->createElement("markers");
	try {
	$database = new Connection();
$db = $database->openConnection();
	
// Get parameters from URL
$center_lat = $_GET["lat"];
$center_lng = $_GET["lng"];
$radius = $_GET["radius"];
	//$center_lat = $_GET['lat'] = (isset($_GET['lat']) && is_float($_GET['lat'])) ? abs(intval($_GET['lat'])) :
    
//$center_lng = $_GET['lng'] = (isset($_GET['lng']) && is_float($_GET['lng'])) ? abs(intval($_GET['lng'])) : '';
	//$radius = $_GET['lng'] = (isset($_GET['radius']) && is_float($_GET['radius'])) ? abs(intval($_GET['radius'])) : '';
 
	
//$query = 'SELECT * FROM markers';
// Set the appropriate content-type header and output the XML
header('Content-type: text/xml');
$query = sprintf("SELECT id, name, address, lat, lng, type, ( 3959 * acos( cos( radians('%s') ) * cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20",
    $center_lat,
    $center_lng,
    $center_lat,
    $radius);
 
$stmt = $db->prepare($query);
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$stmt->execute();
$result = $stmt->fetchAll();
// Iterate through the rows, adding XML nodes for each
foreach ($result as $row) {
$entry = $doc->createElement('marker');
$entry->setAttribute('id', $row['id']);
$entry->setAttribute('name', $row['name']);
$entry->setAttribute('address', $row['address']);
$entry->setAttribute('lat', $row['lat']);
$entry->setAttribute('lng', $row['lng']);
$entry->setAttribute('type', $row['type']);
$entry->setAttribute('distance', $row['distance']);
$node->appendChild($entry);
} 
$doc->appendChild($node);
	 
// Save the XML
echo $doc->saveXML();
echo 'Bytes written: '.$doc->save("markers.xml");
	$database->closeConnection();
	}
catch (PDOException $e) {
echo "There is some problem in connection: " . $e->getMessage();
}
?>
	

 

 

Can someone help me out on this please?

Screenshot 2019-07-07 at 01.13.24.png

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...