我只想在某个地理定位半径内显示标记? [英] I want to only show markers within a certain radius of geolocation?

查看:76
本文介绍了我只想在某个地理定位半径内显示标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的页面上,当它加载时,它会获取您的位置并在您的位置设置标记,然后我希望它从我的数据库加载所有标记并将其设置在地图上,但我只想加载位于其中的标记1公里的位置。我试图设置它,但我遇到了一些麻烦。

因此,标记从数据库中加载,如下所示:

 <?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 - >关(); $ mysqli - >关闭();?>

然后setMarker函数调用如下所示:

 函数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);
}

这一切都可以正常工作,并将数据库中的所有标记加载到地图,但我怎么可能只加载距离我1公里的地图?我读了about computeDistanceBetween(),但我找不到一个拯救我生命的例子。



我最终找到了一种方法来完成这个任务,最终在这里处理起来更容易,更快速。未来:

 <?php while($ stmt  - > fetch()){?> 
var longi =<?php echo $ gLongitude;?>;
var lati =<?php echo $ gLatitude;?>;
var title =<?php echo $ gTitle;?>;
var content ='巴士抵达:'+<?php echo $ gWeekdayDay;?>;
database.push({latitude:lati,longitude:longi,markerTitle:title,content:content});
<?php} $ stmt - >关(); $ mysqli - >关闭();?>

for(var i = 0; i< database.length; i ++){
createMarker(database [i] .latitude,database [i] .longitude,database [i]。 markerTitle,initialLocation,
database [i] .content);


函数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);


函数setMarker(myPosition,latLongMarker,title,content){
$ b $ distance = google.maps.geometry.spherical.computeDistanceBetween(latLongMarker,myPosition);
console.log(''+ latLongMarker +'和原始位置'+ myPosition +'的距离等于'+距离';
updateResults();
if(distance< setDistance){
addMarker(latLongMarker,title,content);
stopsfound ++;
updateResults();
console.log(content);



函数addMarker(位置,标题,内容){
console.log('正在添加标记'+内容);
marker = new google.maps.Marker({
position:position,
map:map,
title:title
});
bindInfoWindow(marker,content);
markersArray.push(marker);


解决方案

您需要使用 Haversine formaula 。下面的代码使用这个公式使用 <强> PDO 。您将需要更改 $ mysqli

  $ stmt = $ (弧度(θ))* cos(弧度(lat))* cos(弧度(lng) - 弧度(θ))+ sin(sinθ)弧度(?))* sin(弧度(lat))))AS距离距离mytable HAVING距离< 1 ORDER BY距离ASC LIMIT 0,20); 
//赋值参数
$ stmt-> bindParam(1,$ center_lat); //坐标位置
$ stmt-> bindParam(2,$ center_lng); //坐标位置
$ stmt-> bindParam(3,$ center_lat);

此查询用于 DEMO


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);
}

解决方案

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

这篇关于我只想在某个地理定位半径内显示标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆