Reputation: 14824
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);
}
Upvotes: 1
Views: 1806
Reputation: 72
first, you need create a table, call places, for example, and this need have the fields lat and long. Then you need create a query that get the places of a radius (km or milles) here an example of the query
public function getRecents($nro_places, $lat='-33.45287', $lng='-70.58598') {
$query = Doctrine_Query::create();
$query->select('p.*, ( 6371 * acos( cos( radians('.$lat.') ) * cos( radians( m.lat ) ) * cos( radians( m.lng ) - radians('.$lng.') ) + sin( radians('.$lat.') ) * sin( radians( m.lat ) ) ) ) AS distance'); //this is all you need
$query->from('Places p, p.Maps m');
$query->having('distance < 3'); //3kms
$query->where('status = 1'); //places enabled
$query->orderBy('p.created_at DESC, distance ASC');
$query->limit($nro_places);
$result = $query->execute(array(), Doctrine_Core::HYDRATE_ARRAY);
return $result;
}
Here an example of the query with the query that you need. http://wiki.linuxservertech.com/faq/index.php?action=artikel&cat=4&id=7&artlang=en
hope this help
Upvotes: 0
Reputation: 7228
You will need to use the Haversine formaula. The code below uses this formula using PDO. You will need change it for $mysqli
.
$stmt = $dbh->prepare("SELECT name, lat, lng, (6372.8 * acos( cos( radians(?) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(?) ) + sin( radians(?) ) * sin( radians( lat ) ) ) ) AS distance FROM mytable HAVING distance < 1 ORDER BY distance ASC LIMIT 0 , 20");
// Assign parameters
$stmt->bindParam(1,$center_lat);//Coordinates of location
$stmt->bindParam(2,$center_lng);//Coordinates of location
$stmt->bindParam(3,$center_lat);
This query was used in this DEMO
Upvotes: 2