Java 如何检查用户是否在圈内 Google 地图 v2

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20044081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:38:01  来源:igfitidea点击:

How to check if user is inside a Circle Google maps v2

javaandroidgoogle-maps

提问by Jeongbebs

I have a circle on my map. Now I want to detect if the user (or me) is inside the circle.

我的地图上有一个圆圈。现在我想检测用户(或我)是否在圆圈内。

 Circle circle = map.addCircle(new CircleOptions()
     .center(new LatLng(14.635594, 121.032962))
     .radius(55)
     .strokeColor(Color.RED)
     );

I have this code:

我有这个代码:

 LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    LocationListener ll = new myLocationListener();
    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,ll);



     Location.distanceBetween( pLat,pLong,
                circle.getCenter().latitude, circle.getCenter().longitude, distance);

            if( distance[0] > circle.getRadius()  ){
                Toast.makeText(getBaseContext(), "Outside", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getBaseContext(), "Inside", Toast.LENGTH_LONG).show();
            }

And on myLocationListener I have this:

在 myLocationListener 上,我有这个:

public void onLocationChanged(Location location) {
            // TODO Auto-generated method stub
             pLong  = location.getLongitude();
             pLat = location.getLatitude();

        }

It works correctly if I parameter inside distanceBetweenis the coordinates of marker, however, the toast displays Outsideeven though my location is inside the radius.

如果我的参数 insidedistanceBetween是标记的坐标,它可以正常工作,但是,即使我的位置在半径内,吐司也会显示外部

enter image description here

在此处输入图片说明

Any ideas how to do this correctly? Please help. Thanks!

任何想法如何正确地做到这一点?请帮忙。谢谢!

EDIT

编辑

I discovered something odd.

我发现了一些奇怪的东西。

On the picture, you can see I have a textView above which has 5 numbers (circle Latitude, circle longitude, distance at index 0 , distance at index 1 , distance2). distanceis a float array to store the distance between the center of the circle and the user location. I set the radius to 100, and I think the unit is meters, however, as you can see, the values at the distancearray are : 1.334880E7, -81.25308990478516, -10696092987060547. What is the formula for the computation of the distance? And also, 1.something times 10 raise to 7 is about 13 million which is really greater than 100. Please help its really confusing right now. According to documentation of Circle (The radius of the circle, specified in meters. It should be zero or greater.) and distanceBetween (Computes the approximate distance in meters between two locations) so I don't know why is this the result.

在图片上,您可以看到我有一个 textView 上面有 5 个数字(圆纬度,圆经度,索引 0 处的距离,索引 1 处的距离,距离2)。distance是一个浮点数组,用于存储圆心和用户位置之间的距离。我将半径设置为 100,我认为单位是米,但是,如您所见,distance数组中的值是:1.334880E7-81.25308990478516-10696092987060547. 距离的计算公式是什么?而且,1.something 10 加到 7 大约是 1300 万,这确实大于 100。请帮助它现在真的很混乱。根据 Circle (圆的半径,以米为单位指定。它应该为零或更大。)和 distanceBetween (计算两个位置之间以米为单位的近似距离)的文档,所以我不知道为什么会这样。

enter image description here

在此处输入图片说明

回答by MaciejGórski

Use GoogleMap.setOnMyLocationChange(OnMyLocationChangeListener)instead of LocationManager. This way you will get Locations that are the same as blue dot locations.

使用GoogleMap.setOnMyLocationChange(OnMyLocationChangeListener)代替LocationManager。这样,您将获得Location与蓝点位置相同的 s。

回答by kaiser

tl;dr? jsFiddle here- look at your console output.

tl;博士jsFiddle 在这里- 看看你的控制台输出。

Basically there're two ways to do this:

基本上有两种方法可以做到这一点:

  1. Check if the (marker of) the user is inside the Circle Bounds
  2. Compute the distance between the user and the center of the Circle. Then check if it is equal or smaller than the Circle Radius. This solution needs the sphericallibrary to work.
  1. 检查用户的(标记)是否在圆形边界内
  2. 计算用户和圆的中心之间的距离。然后检查它是否等于或小于圆半径。此解决方案需要spherical库才能工作。

Circle Bounds

圆形边界

Just add a circle:

只需添加一个圆圈:

circle = new google.maps.Circle( {
        map           : map,
        center        : new google.maps.LatLng( 100, 20 ),
        radius        : 2000,
        strokeColor   : '#FF0099',
        strokeOpacity : 1,
        strokeWeight  : 2,
        fillColor     : '#009ee0',
        fillOpacity   : 0.2
    } )

and then check if the marker is inside:

然后检查标记是否在里面:

circle.getBounds().contains( new google.maps.LatLng( 101, 21 ) );

At a first glance you might thinkthis works. But it doesn't. In the background google (still) uses a rectangle, so everything inside the rectangular bounding box, but outside the circle will be recognized as insidethe latLng bounds. It's wrong and a known problem, but it seems Google doesn't care.

乍一看,您可能认为这有效。但事实并非如此。在背景谷歌(静止)使用的矩形,所以一切矩形边框内,但外侧的圆将被识别为内部的经纬度范围。这是错误的,也是一个已知问题,但似乎 Google 并不关心。

If you now think that it would work with rectangular bounds, then you're wrong. Those don't work either.

如果您现在认为它适用于矩形边界,那么您就错了。那些也行不通。

Spherical Distance

球面距离

The easiest and best way is to measure the distance. Include the spherical library by appending &library=sphericalto your google maps script call. Then go with

最简单和最好的方法是测量距离。通过附加&library=spherical到您的 google maps 脚本调用来包含球形库。然后去

google.maps.geometry.spherical.computeDistanceBetween( 
    new google.maps.LatLng( 100, 20 ),
    new google.maps.LatLng( 101, 21 )
) <= 2000;

回答by Christian Abella

I know this question had been asked more than a year ago but I have the same problem and fixed it using the distanceBetweenstatic function of Location.

我知道一年多前就有人问过这个问题,但我遇到了同样的问题,并使用distanceBetweenLocation的静态函数修复了它。

float[] distance = new float[2];

Location.distanceBetween(latLng.latitude, latLng.longitude, circle.getCenter().latitude,circle.getCenter().longitude,distance);

if ( distance[0] <= circle.getRadius())
{
    // Inside The Circle
}
else
{
    // Outside The Circle
}