On my page, when it loads it gets your location and sets a marker on your spot, then I want it to load all markers from my database and set them on the map, but I only want to load markers that are within 1km of their location. I'm trying to set it up but I'm having a bit of trouble.
So the markers are loaded from the database like so:
<?php while($stmt -> fetch()) { ?> var longi = "<?php echo $gLongitude; ?>"; var lati = "<?php echo $gLatitude; ?>"; var title = "<?php echo $gTitle; ?>"; setMarker(lati, longi, title); <?php } $stmt -> close(); $mysqli -> close();?>
and then the setMarker function calls like this:
function setMarker(lati, longi, title) { var latLongMarker = new google.maps.LatLng(lati,longi); marker = new google.maps.Marker({ position: latLongMarker, map: map, draggable: false, title: title }); arrMarkers.push(marker); }
That all works fine and dandy, and loads all the markers from the database onto the map but how might I load only ones that are 1km from me? I read about computeDistanceBetween() but I can't find an example to save my life. Thanks everybody in advance.
I ended up working out a way to do it that ended up being much easier and faster to process here you go for anybody looking for this in the future:
<?php while($stmt -> fetch()) { ?> var longi = "<?php echo $gLongitude; ?>"; var lati = "<?php echo $gLatitude; ?>"; var title = "<?php echo $gTitle; ?>"; var content = 'Bus arrives at: ' + "<?php echo $gWeekdayDay; ?>"; database.push({latitude: lati, longitude: longi, markerTitle: title, content: content}); <?php } $stmt -> close(); $mysqli -> close();?> for (var i = 0; i < database.length; i++) { createMarker(database[i].latitude, database[i].longitude, database[i].markerTitle, initialLocation, database[i].content); } function createMarker(lati, longi, title, myPosition, content) { var latLongMarker = new google.maps.LatLng(lati, longi); distanceCompare.push({position: latLongMarker, markerTitle: title}); setMarker(myPosition, latLongMarker, title, content); } function setMarker(myPosition, latLongMarker, title, content) { distance = google.maps.geometry.spherical.computeDistanceBetween(latLongMarker, myPosition); console.log('Distance of '+latLongMarker+ 'and original position' + myPosition + 'Is equal to '+distance); updateResults(); if (distance < setDistance) { addMarker(latLongMarker, title, content); stopsfound++; updateResults(); console.log(content); } } function addMarker(position, title, content) { console.log('Adding Marker ' + content); marker = new google.maps.Marker({ position: position, map: map, title: title }); bindInfoWindow(marker, content); markersArray.push(marker); }