简体   繁体   English

半径/最接近的结果 - Google Maps API

[英]Radius/nearest results - Google Maps API

First of all I'm using Google Maps API v3. 首先,我使用的是Google Maps API v3。 I have one big map that shows all the results pulled from a database, now I like to implement a feature that shows a radius of nearest results within X kilometres from the current location (served by HTML5 geolocation). 我有一张大地图显示从数据库中提取的所有结果,现在我想实现一个功能,显示距离当前位置X公里内最近的结果半径(由HTML5地理位置提供)。

As the map contains all the results, I want to be able to add X kilometres, then it will get submitted that results will be returned the nearest results according to the current location of the user. 由于地图包含所有结果,我希望能够添加X公里,然后它将被提交,结果将根据用户的当前位置返回最近的结果。 The whole map should then remove all the out-of-radius markers, leaving the markers that are inside a radius of X kilometres. 然后整个地图应移除所有半径外的标记,留下半径为X公里的标记。

I'm requesting an effective code, tutorial or an article on how to do this. 我正在请求有效的代码,教程或有关如何执行此操作的文章。

Here the JSFiddle Demo: 这里的JSFiddle演示:

I suppose this is one way to do it. 我想这是一种方法。 You can use Google Map V3 API's circle to create the bounds from your center point or current location. 您可以使用Google Map V3 API的圆圈创建中心点或当前位置的边界。 You can specify the radius of the circle in meters with setRadius(radius:number) . 您可以使用setRadius(radius:number)指定圆的半径(以米为setRadius(radius:number) With the created circle you can loop through your markers and see if they are within the circle's bound using Circle's getBounds object which is a LatLngBounds and check if the marker is within bound or not. 使用创建的圆圈,您可以循环标记并使用Circle的getBounds对象(即LatLngBounds)查看它们是否在圆圈内,并检查标记是否在边界内。

I went ahead and created a small demo that has five points on a map, and when you click on the create circle button it'll make the first marker the center point and draws a circle with a radius of 5000 and filters out the markers that are not with in bound using the method described above: 我继续创建了一个在地图上有五个点的小型演示,当你点击创建圆形按钮时,它会使第一个标记成为中心点并绘制一个半径为5000的圆,并过滤掉那些标记。不使用上述方法绑定:

function createRadius(dist) {
    var myCircle = new google.maps.Circle({
        center: markerArray[markerArray.length - 1].getPosition(),
        map: map,
        radius: dist,
        strokeColor: "#FF0000",
        strokeOpacity: 0.8,
        strokeWeight: 2,
        fillColor: "#FF0000",
        fillOpacity: 0.35
    });
    var myBounds = myCircle.getBounds();

    //filters markers
    for(var i=markerArray.length;i--;){
         if(!myBounds.contains(markerArray[i].getPosition()))
             markerArray[i].setMap(null);
    }
    map.setCenter(markerArray[markerArray.length - 1].getPosition());
    map.setZoom(map.getZoom()+1);
}

This solution just has one major flaw - it fetches data in a rectangle, not in a circle. 这个解决方案只有一个主要缺陷 - 它以矩形而不是圆形方式获取数据。

If you don't want to query any more data than required - use the haversine formula in SQL: 如果您不想查询超出要求的数据 - 请在SQL中使用hasrsine公式

SELECT
    id,
    (3959 *
        acos(
            cos(radians(37)) *
            cos(radians(lat)) *
            cos(radians(lng) - radians(-122)) +
            sin(radians(37)) *
            sin(radians(lat))
        )
    ) AS distance
FROM markers
HAVING distance < 25
ORDER BY distance
LIMIT 0 , 20;

Also checkout Creating a Store Locator with PHP, MySQL & Google Maps 另外结帐使用PHP,MySQL和Google Maps创建商店定位器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM