[英]I want to only show markers within a certain radius of geolocation?
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. 在我的页面上,加载时会获取您的位置并在您的位置上设置标记,然后我希望它从数据库中加载所有标记并将其设置在地图上,但我只想加载距离其1公里以内的标记位置。 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: 然后setMarker函数调用如下:
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? 一切正常且繁琐,并将数据库中的所有标记加载到地图上,但是我怎么只加载距离我1公里的标记呢? I read about computeDistanceBetween() but I can't find an example to save my life.
我读了有关computeDistanceBetween()的文章,但找不到能挽救生命的示例。 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);
}
You will need to use the Haversine formaula . 您将需要使用Haversine公式 。 The code below uses this formula using PDO .
下面的代码通过PDO使用此公式。 You will need change it for
$mysqli
. 您将需要为
$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);
first, you need create a table, call places, for example, and this need have the fields lat and long. 首先,您需要创建一个表格,例如呼叫地点,并且此字段的字段为lat和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
http://wiki.linuxservertech.com/faq/index.php?action=artikel&cat=4&id=7&artlang=en
hope this help 希望这个帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.