仅在指定的圈子/区域中显示谷歌地图标记

时间:2022-11-30 21:18:02

I want to show the markers in the specified area only! For example, I want to draw the circle with radius suppose 3 or any and center (any LatLng). Then, I need to display the marker only in the regions of that circle. How can I achieve that? Is it possible?

我只想在指定区域显示标记!例如,我想绘制半径为假设3或任意和中心(任何LatLng)的圆。然后,我需要仅在该圆圈的区域中显示标记。我怎样才能做到这一点?可能吗?

Update: Solution to my question

更新:解决我的问题

function isMarkerInArea(circle, marker)
{
   return (circle.getBounds().contains(marker.getPosition()));
};

Description:

  • circle.getBounds() returns the latLng bounds of circle
  • circle.getBounds()返回circle的latLng边界

  • marker.getPosition() returns the latLng of the marker
  • marker.getPosition()返回标记的latLng

  • and finally circle.getBounds().contains(marker.getPosition()) , returns the boolean value if the marker is within the circle all not.
  • 最后是circle.getBounds()。contains(marker.getPosition()),如果标记在圆圈内,则返回布尔值。

Finally, set the visibility of the marker as per the boolean returned!

最后,根据返回的布尔值设置标记的可见性!

2 个解决方案

#1


1  

The best approach (assuming your have an array of markers) would be to use circle's contains() function. An example:

最好的方法(假设你有一个标记数组)将使用circle的contains()函数。一个例子:

function isMarkerInArea(circle, marker){
   return (circle.getBounds().contains(marker.getPosition());
};

This function will return a boolean. If it is true you set display the marker, otherwise you hide it.

该函数将返回一个布尔值。如果确实如此,则设置显示标记,否则隐藏它。

#2


0  

Hard to know exactly what you are doing but I'm going to assume you have an array of markers and you want to only display the ones within a certain circle you define. You can use the square of the differences to calculate a fairly decent distance (if it's small). Say you have a known center point of your circle Cx and Cy and an array P. Compare this formula for array item i: (P[i]x-Cx)(P[i]x-Cx) + (P[i]y-Cy)(P[i]y-Cy) to the square of the distance you care about (say 3000 meters or whatever). Don't bother with a square root as this is processing intensive. Just work with the squares. Then only draw the markers that are under your limit.

很难确切知道你在做什么,但我会假设你有一系列标记,你只想显示你定义的某个圆圈内的标记。您可以使用差异的平方来计算相当不错的距离(如果它很小)。假设你有一个已知的圆Cx和Cy的中心点和一个数组P.比较数组项i的这个公式:(P [i] x-Cx)(P [i] x-Cx)+(P [i] y-Cy)(P [i] y-Cy)到您关心的距离的平方(比如3000米或其他)。不要打扰平方根,因为这是处理密集型的。只需使用正方形。然后只绘制低于限制的标记。

Do I understand your problem correctly? Perhaps you are doing something different than what I immagine.

我能正确理解您的问题吗?也许你正在做的事情和我想象的不同。

#1


1  

The best approach (assuming your have an array of markers) would be to use circle's contains() function. An example:

最好的方法(假设你有一个标记数组)将使用circle的contains()函数。一个例子:

function isMarkerInArea(circle, marker){
   return (circle.getBounds().contains(marker.getPosition());
};

This function will return a boolean. If it is true you set display the marker, otherwise you hide it.

该函数将返回一个布尔值。如果确实如此,则设置显示标记,否则隐藏它。

#2


0  

Hard to know exactly what you are doing but I'm going to assume you have an array of markers and you want to only display the ones within a certain circle you define. You can use the square of the differences to calculate a fairly decent distance (if it's small). Say you have a known center point of your circle Cx and Cy and an array P. Compare this formula for array item i: (P[i]x-Cx)(P[i]x-Cx) + (P[i]y-Cy)(P[i]y-Cy) to the square of the distance you care about (say 3000 meters or whatever). Don't bother with a square root as this is processing intensive. Just work with the squares. Then only draw the markers that are under your limit.

很难确切知道你在做什么,但我会假设你有一系列标记,你只想显示你定义的某个圆圈内的标记。您可以使用差异的平方来计算相当不错的距离(如果它很小)。假设你有一个已知的圆Cx和Cy的中心点和一个数组P.比较数组项i的这个公式:(P [i] x-Cx)(P [i] x-Cx)+(P [i] y-Cy)(P [i] y-Cy)到您关心的距离的平方(比如3000米或其他)。不要打扰平方根,因为这是处理密集型的。只需使用正方形。然后只绘制低于限制的标记。

Do I understand your problem correctly? Perhaps you are doing something different than what I immagine.

我能正确理解您的问题吗?也许你正在做的事情和我想象的不同。