I am using GoogleMap fragment to show some location data using markers. What I want to do is, draw a circle with a radius of specific distance on map, so that the circle will cover certain area approximately on map. I am not sure where to start , I am already having a map displaying location data, from location data I can figure out at what distance those points are from centre location.
Any help would be great.
CircleOptions circleOptions = new CircleOptions()
.center(new LatLng(37.4, -122.1))
.radius(1000)); // In meters
// Get back the mutable Circle
Circle circle = myMap.addCircle(circleOptions);
For detailed info check : https://developers.google.com/maps/documentation/android-api/shapes
Use this function where you need to pass LatLng as paramater when you call it.
private void drawCircle(LatLng point){
CircleOptions circleOptions = new CircleOptions();
// Specifying the center of the circle
circleOptions.center(point);
// Radius of the circle
circleOptions.radius(20);
// Border color of the circle
circleOptions.strokeColor(Color.BLACK);
// Fill color of the circle
circleOptions.fillColor(0x30ff0000);
// Border width of the circle
circleOptions.strokeWidth(2);
// Adding the circle to the GoogleMap
googleMap.addCircle(circleOptions);
}
Use GoogleMap Circle API. You just need to pass the Required inputs and it's done. Follow link from example. e.g from docs , a simple code structure will look like this
Circle circle = map.addCircle(new CircleOptions()
.center(new LatLng(-33.87365, 151.20689)) // center point of map
.radius(10000) // radius of circle to cover on map
.strokeColor(Color.RED) //color of circle boundary
.fillColor(Color.BLUE)); //color of circle to fill ,
// make sure choose the light color close to transparent
Related
I develop a JavaFX application and I am struggling with solving this problem:
I have an array of points (x, y) that I am plotting over a scatter chart, you can see in the picture below they appear as a small gray points.
Those points I'd like to enclose with the smallest circle, so for that I have a function called minimalCircle that receives an array of points and while using the Welzl's algorithm it returns the minimal enclosing circle (radius + center point)
Circle c = minimalCircle(points);
The problem occurs when I am trying to plot the circle I retrieve from the function, I make a series containing 1 point which is the center of the circle, and applying a circle shape with the radius of the circle
private XYChart.Series<Number, Number> minimalCircleSeries(Point[] points) {
XYChart.Series series = new XYChart.Series();
Circle c = minimalCircle(points);
javafx.scene.shape.Circle pointCircle = new javafx.scene.shape.Circle(c.center.x, c.center.y, c.r);
XYChart.Data data = new XYChart.Data(c.center.x, c.center.y);
data.setNode(pointCircle);
series.getData().add(data);
return series;
}
Unfortunately when it draws the circle it refers the radius size by pixels while its actually suppose to be real units on the graph, and that cause the circle in the picture below to not be the smallest enclosing circle.
I am trying to find a way to convert the radius scale so the circle will be plotted correctly or to find another way to plot a circle over a scatter chart
Thank you very much for your help !
Example image of the chart
I was able to draw circle on map around my current location but I want to show the markers only inside that circle and hide other markers from map.
You need to first draw a circle and then compare each marker either it is inside or outside circle, by using this method.
private void checkCircle(){
float[] distance = new float[2];
Location.distanceBetween(marker_latitude, marker_longitude,
circle.getCenter().latitude, circle.getCenter().longitude, distance);
if(distance[0] > circle.getRadius() ){
Toast.makeText(getContext(), "Outside", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getContext(), "Inside", Toast.LENGTH_LONG).show();
}
}
If you are getting that your marker is in outside than dont need to add in map.
Earthquake threat circle on the map
I am using UnfoldingMaps to display earthquake information on the map.
I plan to show the threat circle on the map.
A circle is drawn given its radius and center position in pixels. How to get the radius is the problem I met.
Suppose I have the threat circle radius R in kilometers and the center marker A.
I want to create a marker B on the circle so that I can use the screen distance as the screen radius.
I decided to create B with the same longitude but a different latitude from A. I change R to delta latitude.
But after drawing the circle I found it is not the right one since the red triangular should be in the circle according to their distance.
The main difficulty is exactly how to calculate screen radius according to kilometers.
public void calcThreatCircleOnScreen(UnfoldingMap map) {
float radius = 0;
float deltaLat=(float) (threatCircle()/6371/2/3.1415927*360);
Location centerLocation = this.getLocation();
Location upperLocation = new Location(centerLocation);
upperLocation.setLat(centerLocation.getLat() + deltaLat);
SimplePointMarker upperMarker = new SimplePointMarker(upperLocation);
ScreenPosition center = this.getScreenPosition(map);
ScreenPosition upper = upperMarker.getScreenPosition(map);
radius = Math.abs(upper.y - center.y);
setThreatCircleOnScreen(radius);
}
This is going to depend on two things: the zoom level of the map, and the projection you're using.
You need to unproject kilometers to pixels, and you can probably figure out how to do that using google and the Unfolding API.
For example, I found a MercatorProjection class that contains a constructor that takes a zoom level, and methods for projecting and unprojecting points between world coordinates and pixel coordinates.
That's just a starting point, since I'm not sure what units those methods are taking, but hopefully this is a direction for you to take your googling and experimenting.
I'd recommend trying to get something working and posting an MCVE if you get stuck. Good luck.
Now I have the answer for this question. Hope it will be helpful for others.
Earthquake threat circle on the map
My early solution to calculate radius in pixels from km is correct. I think it a simple and powerful idea (independent of projecting API)
The only problem is I should use diameter rather than radius in drawing the circle. I should draw with d=2r like this
float d = 2 * threatCircleRadius();
pg.noFill();
pg.ellipse(x,y,d,d);
I found another cleaner solution like below by consulting the author of UnfoldingMaps. (https://github.com/tillnagel/unfolding/issues/124)
My early solution first changes distance to delta latitude, then create new location by changing latitude.
The new solution use the API GeoUtils.getDestinationLocation(sourceLocation, compassBearingDegree, distanceKm) to directly get the new location!
In addition, I needn't create a new marker to find its screen position.
public void calcThreatCircleOnScreen(UnfoldingMap map) {
float radius = 0;
Location centerLocation = this.getLocation();
Location upperLocation = GeoUtils.getDestinationLocation(centerLocation, 0, threatCircle());
//SimplePointMarker upperMarker = new SimplePointMarker(upperLocation);
ScreenPosition center = map.getScreenPosition(centerLocation);
ScreenPosition upper = map.getScreenPosition(upperLocation);
radius = PApplet.dist(center.x, center.y, upper.x, upper.y);
setThreatCircleOnScreen(radius);
}
I rotated my sprite 90 degrees and I want to do the same with my rectangle to be able to use them for collision, but the rotate() method is not available on rectangles.
This is what I did:
treeSpr=new Sprite(new Texture(Gdx.files.internal("tree.png")));
treeSpr.setPosition(250,700);
treeSpr.rotate(90f);
//Rectangle
treeRect=new Rectangle(treeSpr.getX(),treeSpr.getHeight(),
treeSpr.getWidth(),treeSpr.getHeight());
The other answer is basically correct; however, I had some issues with the positioning of the polygons using that method. Just some clarification:
LibGDX does not support rotated Rectangles when using the Intersector for collision dectection. If you need rotated rectangles, you should use the Polygon for collision detection instead.
Building a Rectangular Polygon:
polygon = new Polygon(new float[]{0,0,bounds.width,0,bounds.width,bounds.height,0,bounds.height});
Don't forget to set the origin of the Polygon if you are going to rotate it:
polygon.setOrigin(bounds.width/2, bounds.height/2);
Now you can rotate the collision polygon:
polygon.setRotation(degrees);
Also, somewhere in your code, you will likely want to update the position of the collision polygon to match your sprite:
polygon.setPosition(x, y);
We can even draw our polygon on screen (for debug purposes):
drawDebug(ShapeRenderer shapeRenderer) {
shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
shapeRenderer.polygon(polygon.getTransformedVertices());
shapeRenderer.end();
}
Collision Detection:
The overlapConvexPolygons() of the Intersector:
boolean collision = Intersector.overlapConvexPolygons(polygon1, polygon2)
As mentioned in the other answer, this method only works if:
using convex polygons, which the rectangle is
performing polygon to polygon checks, e.g.: you cannot mix rectangles and
polygons
Rotation
You could create a Polygon from the rectangle or from the sprite (supplying the vertices in order for the polygon constructor) and use it's rotate(float degrees) method:
treePoly = new Polygon(new float[] {
treeRect.x, treeRect.y,
treeRect.x, treeRect.y + treeRect.height,
treeRect.x + treeRect.width, treeRect.y + treeRect.height,
treeRect.x + treeRect.width, treeRect.y
});
treePoly.rotate(45f);
Collision Detection
Collision checks then could be done via the Intersector class:
Intersector.overlapConvexPolygons(polygon1, polygon2)
Keep in mind though, this method only works if:
you use convex polygons, which the rectangle is
you do polygon to polygon checks, e.g.: you cannot mix rectangles and polygons
I think something like it can help, I can not test now,
//Rectangle
treeRect=new Rectangle(treeSpr.getX(),
treeSpr.getY(),
treeSpr.getHeight(), //now is change width by height
treeSpr.getWidth()); //now is change height by width
Note: may You need to adjust the origin of the rotation for both
you can use a render ShapeRenderer to see if the result is as expected:
add for test in variable class
private ShapeRenderer sRDebugRectangel = new ShapeRenderer();
add for test in update or draw
sRDebugRectangel.begin(ShapeType.Filled);
sRDebugRectangel.identity();
sRDebugRectangel.rect(yourRectangle.getX(),
yourRectangle.getY(),
yourRectangle.getWidth(),
yourRectangle.getHeight());
sRDebugRectangel.end();
can look at my answer to this question to use a shaperrender otherwise known as:
Libgdx, how can I create a rectangle from coordinates?
This question already has an answer here:
Draw five transparent circumcircles on the Google Map v2
(1 answer)
Closed 9 years ago.
I need to draw five circle-circle on the Google Maps v2 by taking the current location as the center of a circle. Meaning that each of the five circles have the same center but with different radii: the first circle will have radius of 10m, second circle radius of 20m, third circle radius of 30m, fourth circle radius of 40m, and fifth circle radius of 50m. I am using Google Maps v2.
And I need to show a marker on the center of the circle as well.
I am trying something like this to draw the circle on the Google Map v2 but it draws only one circle and not the five circum-circle
CircleOptions circleOptions = new CircleOptions()
.center(latLng) //set center
.radius(500) //set radius in meters
.fillColor(Color.TRANSPARENT) //default
.strokeColor(0x10000000)
.strokeWidth(5);
myCircle = googleMap.addCircle(circleOptions);
I need to draw circum-circle exactly like this-
Can anybody help me with this? I am having problem in making this circle in Google Map v2. Any help will be appreciated.
for(int rad=100;rad<=500;rad+100)
{
CircleOptions circleOptions = new CircleOptions()
.center(latLng) //set center
.radius(rad) //set radius in meters
.fillColor(Color.TRANSPARENT) //default
.strokeColor(0x10000000)
.strokeWidth(5);
myCircle = googleMap.addCircle(circleOptions);
}