回答
只是遍历所有你必须标记并使用以下函数获取标记从具体坐标的距离:computeDistanceBetween()
:
要计算这个距离,可以呼叫computeDistanceBetween(),它传递 两个LatLng对象。
我发现它是here。然后过滤掉所有足够接近的标记。
见给这个问题的答案:Google Maps Api v3 - find nearest markers
你基本上需要通过你的标记阵列循环,并用一个公式来从给定的点计算出它们的距离(圆心代表你的搜索半径)。
然后,您可以排除这比半径更远的任何标记,你将留下哪些是圈子里面的标记列表广告。
装入geometry library和使用computeDistanceBetween()
从您的中心点找到的每个标记的距离。
如果距离为半径内,显示标记。
var targetLat=marker.getPosition().lat();
var targetLng=marker.getPosition().lng();
var targetLoc = new GLatLng(targetLat,targetLng);
var center= new GLatLng(centerLat, centerLng);
var distanceInkm=center.distanceFrom(targetLoc)/1000;
if(distanceInkm < radius){
// To add the marker to the map, call setMap();
marker.setMap(map);
}
'GLatLng'在V3中不推荐使用,现在叫做'google.maps.LatLng'现在 – Faloude 2017-10-11 14:12:21
这是一个工作示例。点击地图绘制与选定的半径的半径,它会显示所有从all_locations
示例阵列落入半径内的标记物。点击标记以查看距半径中心的距离(以米为单位)。 (点击某处在纽约的二街看到的例子标记 - 地图已经集中到那个位置)
var map = null;
var radius_circle = null;
var markers_on_map = [];
//all_locations is just a sample, you will probably load those from database
var all_locations = [
\t {type: "Restaurant", name: "Restaurant 1", lat: 40.723080, lng: -73.984340},
\t {type: "School", name: "School 1", lat: 40.724705, lng: -73.986611},
\t {type: "School", name: "School 2", lat: 40.724165, lng: -73.983883},
\t {type: "Restaurant", name: "Restaurant 2", lat: 40.721819, lng: -73.991358},
\t {type: "School", name: "School 3", lat: 40.732056, lng: -73.998683}
];
//initialize map on document ready
$(document).ready(function(){
var latlng = new google.maps.LatLng(40.723080, -73.984340); //you can use any location as center on map startup
var myOptions = {
zoom: 14,
center: latlng,
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
\t
\t google.maps.event.addListener(map, 'click', showCloseLocations);
});
function showCloseLocations(e) {
\t var i;
\t var radius_km = $('#radius_km').val();
\t var address = $('#address').val();
\t //remove all radii and markers from map before displaying new ones
\t if (radius_circle) {
\t \t radius_circle.setMap(null);
\t \t radius_circle = null;
\t }
\t for (i = 0; i < markers_on_map.length; i++) {
\t \t if (markers_on_map[i]) {
\t \t \t markers_on_map[i].setMap(null);
\t \t \t markers_on_map[i] = null;
\t \t }
\t }
\t
\t var address_lat_lng = e.latLng;
\t radius_circle = new google.maps.Circle({
\t \t center: address_lat_lng,
\t \t radius: radius_km * 1000,
\t \t clickable: false,
\t \t map: map
\t });
\t if(radius_circle) map.fitBounds(radius_circle.getBounds());
\t for (var j = 0; j < all_locations.length; j++) {
\t \t (function (location) {
\t \t \t var marker_lat_lng = new google.maps.LatLng(location.lat, location.lng);
\t \t \t var distance_from_location = google.maps.geometry.spherical.computeDistanceBetween(address_lat_lng, marker_lat_lng); //distance in meters between your location and the marker
\t \t \t if (distance_from_location <= radius_km * 1000) {
\t \t \t \t var new_marker = new google.maps.Marker({
\t \t \t \t \t position: marker_lat_lng,
\t \t \t \t \t map: map,
\t \t \t \t \t title: location.name
\t \t \t \t });
\t \t \t \t google.maps.event.addListener(new_marker, 'click', function() {
\t \t \t \t \t alert(location.name + " is " + distance_from_location + " meters from my location");
\t \t \t \t });
\t \t \t \t markers_on_map.push(new_marker);
\t \t \t }
\t \t })(all_locations[j]);
\t }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("maps", "3",{other_params:"sensor=false&libraries=geometry"});
</script>
<body style="margin:0px; padding:0px;" >
<select id="radius_km">
\t <option value=1>1km</option>
\t <option value=2>2km</option>
\t <option value=5>5km</option>
\t <option value=30>30km</option>
</select>
<div id="map_canvas" style="width:500px; height:300px;">
</body>
嗨,代码片段不再适用 – aaa 2017-08-11 06:15:02
@ChingChongPa它适用于Firefox。它取决于浏览器设置等,因为它从https页面加载http脚本。在你的本地主机上试用它,应该在任何浏览器中工作。如果您收到'没有API密钥'错误,则可以尝试加载Google Maps JS api,如下所述:https://developers.google.com/maps/documentation/javascript/tutorial,而不是google.load(“从示例中映射“..”。 – 2017-08-11 14:06:32
- 1. 找到一个圆的半径,直径,面积和圆周
- 2. 寻找围绕指定的点的给定半径
- 3. 如何用DBSCAN找到一个簇的半径
- 4. 给定一个曲面,找到被曲面切成一半的零件
- 5. 得到给定半径内的所有标记在SQL
- 6. HTML - 找到一个给定的标签
- 7. 找到一个给定的字符串
- 8. searchkick在位置半径找到
- 9. 如何找到一个给定的“路径字符串的一部分”用C#
- 10. 确定一个坐标是否在另一个的半径内
- 11. 查找给定路径的根路径
- 12. 在给定路径找不到文件
- 13. 做一个径向渐变半径200px
- 14. 创建一定半径的多边形
- 15. 找到给定输入的唯一路径名称
- 16. 特定圆半径
- 17. 查找城市半径
- 18. 查找记录在一个半径的PostgreSQL
- 19. “找不到路径的一部分”寻找一个UNC路径
- 20. 确定给定半径算法内的点
- 21. 得到给定一个路径和文件名路径名
- 22. JQuery的css边框半径特定的角半径?
- 23. 一个NSBundle对于给定的路径
- 24. 如何在给定中心点和半径大小时绘制一个球体?
- 25. 确定椭圆的X半径和Y半径
- 26. 单个半径`边界半径'作为特定面的百分比
- 27. 返回中的R的观察给定半径内的属性
- 28. 当前的位置是在一个特定的半径Android
- 29. 制作一个特定的边框半径CSS
- 30. 如何让玩家锁定其半径内的一个敌人?
是的,这工作很多感谢! – 2012-04-24 15:28:55