我在地图上画了一个圆,现在我想检测用户(或者我自己)是否在圆内。
Circle circle = map.addCircle(new CircleOptions()
.center(new LatLng(14.635594, 121.032962))
.radius(55)
.strokeColor(Color.RED)
);
我有这段代码:
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();
}
在我的LocationListener中,我有以下内容:
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
pLong = location.getLongitude();
pLat = location.getLatitude();
}
如果我在distanceBetween
函数中传入标记的坐标,则它的工作是正确的,但是即使我的位置在半径内,也会显示“外部”。您有什么正确的解决方法吗?请帮忙。谢谢!在图片上可以看到一个文本视图,其中有5个数字(圆形纬度、圆形经度、距离索引0处的距离、距离索引1处的距离、距离2)。 distance 是一个浮点数组,用于存储圆的中心与用户位置之间的距离。我将半径设置为100,单位应该是米,但是如您所见,在distance
数组的值为: 1.334880E7 , -81.25308990478516 , -10696092987060547 。计算距离的公式是什么?此外,10的1次幂乘以1.多一些约等于1300万,比100要大得多。根据Circle的文档(圆的半径,以米为单位指定。应为0或更大。)和distanceBetween (计算两个地点之间的近似距离,以米为单位),我不知道为什么会得到这个结果。请帮忙解释一下,非常感谢!