[英]Detect when user enter circle
I am working on a app based on google maps.我正在开发基于谷歌地图的应用程序。
I added a circle to the map ( https://ibb.co/PhZy0t6 ) and I have been tring to detect if the user entered the circle .我在地图上添加了一个圆圈 ( https://ibb.co/PhZy0t6 ),我一直在尝试检测用户是否进入了圆圈。
The user is a marker I added with the user current location.用户是我添加的用户当前位置的标记。
I tried to do like the following:我试着做如下:
// Circle code:
circle = mMap.addCircle(new CircleOptions()
.center(new LatLng(addressLatLng.latitude, addressLatLng.longitude))
.radius(70)
.strokeColor(Color.RED)
);
// The rest
float[] distance = new float[2];
Location.distanceBetween(userLocation.latitude, userLocation.longitude, circle.getCenter().latitude,circle.getCenter().longitude,distance);
if (distance[0] <= circle.getRadius()) {
Toast.makeText(this, "User Enter Circle", Toast.LENGTH_SHORT).show();
}
However it didn't worked..然而它没有奏效..
Thanks for any help谢谢你的帮助
So finally thanks to @Axiumin ,I did this:所以最后感谢@Axiumin,我做到了:
@Override
public void onLocationChanged(Location location) {
userLocationMarker.setPosition(new LatLng(location.getLatitude(), location.getLongitude()));
Location userLocation = new Location("userLocation");
userLocation.setLatitude(location.getLatitude());
userLocation.setLongitude(location.getLongitude());
Location circleLocation = new Location("circleLocation");
circleLocation.setLatitude(circle.getCenter().latitude);
circleLocation.setLongitude(circle.getCenter().longitude);
float distance = userLocation.distanceTo(circleLocation);
if (distance <= 70){ // If distance is less than 70 Meters
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.