过滤谷歌地图范围内的标记

时间:2016-06-14 07:48:51

标签: javascript asp.net google-maps google-maps-api-3

google.maps.event.addListener(map, 'idle', function (event) 
                            {
                                 var ne = map.getBounds().getNorthEast();
                                //northeast lat and long

                                var sw = map.getBounds().getSouthWest();
                               //southwest lat and long

                                 var x = document.getElementById('hdnControl').value;
                               // x contains all the markers retrived from database 


                              // code part to filter markers withing bounds

                            }

var x包含所有标记,我想放置在bounds内的标记。我可以过滤边界内的标记吗? 任何帮助将不胜感激

  x contains value as
[
 {"Latitude":19.2094000000,"Longitude":73.0939000000},
 {"Latitude":19.2244070000,"Longitude":73.1545760000},
 {"Latitude":19.1659242536,"Longitude":82.2436523438},
 {"Latitude":18.3336694458,"Longitude":80.4309082031}
]

//as it has been serialized with
//dt is datatable retrived from database
JavaScriptSerializer oSerializer = new JavaScriptSerializer();

        var Result = (from c in dt.AsEnumerable()
                      select new
                      {
                          Latitude = c.Field<Decimal>("Latitude"),
                          Longitude = c.Field<Decimal>("Longitude")
                      }).ToList();

        hdnControl.Value = oSerializer.Serialize(Result);

1 个答案:

答案 0 :(得分:1)

LatLngBounds对象附带一个contains()方法,它接受一个LatLng点,如果该点恰好在边界内,则返回true,如果在外,则返回false。

这是一个示例代码,指示标记是否在边界内:

function check_is_in_or_out(marker){
return map.getBounds().contains(marker.getPosition());
}

这是一张相关的SO票,讨论如何使用bounds.contains()check if map markers are within selected bounds