Is there any API to obtain a list of vehicle within a certain distance (Eg:10 KM)? I am using the map from geoserver. And need to draw the circle within that distance.
Take a look here to see how you can obtain the distance between 2 GPS points (1 point being the vehicle you want to track and the other being the centre of the circle). Once that you have obtained the distance, check to see if the distance is greater than the radius. If the distance is less than the radius, the vehicle is in the circle. If it is equal to the radius, the vehicle is on the edge of the circle, if it is greater than the radius, then, the vehicle is outside the circle.
If you need more complex calcualtions, you might want to take a look at Spheres:
A Java package for doing spherical trigonometry without all the messy
(and cpu intensive) trigonometry. The spheres package includes classes
for points, spherical polygons, latitude/longitude bounding boxes,
orbits, and scenes.
Related
I'm having trouble computing the intersection point of an ellipse and a line.
Let's say I have an ellipse at point (0/0) with radius 500. Additionaly I'm drawing a line from point (0/0) to (mouseX, mouseY).
First I check if the mouse coordinates are outside the ellipse by doing
if((mouseX*mouseX)+(mouseY*mouseY)) > 500*500){/*...*/}
Now, whenever the mouse coordinates are outside that ellipse, I want to draw the line not until the mouse, but until the 'border' of the ellipse. In order to do so, I must have the intersection point of the line and the ellipse.
Are there any libraries that simplify such trigonometric tasks? Is there any other more or less easy way to compute the intersection?
From what you've said, I'm making the following assumptions:
The ellipse is always circular (same radius all the way round).
The line is always being drawn from the centre of the circle.
If those are true then the problem is actually very simple. All you need to do is truncate the line so that its length is the same as the circle's radius, and that gives you the intersection point.
If the mouse is outside the ellipse:
Store the vector describing the line; in this case (mouseX, mouseY).
Normalize the vector (divide each component by the length of the line).
Multiply the vector by the radius of the circle.
The vector now contains the intersection point, relative to the centre of the circle.
You don't have to use a vector class for this, although it might help.
If your circle and line aren't starting on the origin (0,0) then you'll need to compensate. At step 1, subtract your new origin from (mouseX,mouseY). After step 3, add the origin back in to get the display coordinates.
I have a Google Map fragment on my android application.
I have markers drawn on the map which represent places of interest and I have an extra functionality that draws a circle on the center with a radius defined by the user.
What I want to do is that only the markers which are contained inside this circle shall be displayed on map. I am using the Circle object, from Google maps Shapes, to draw the circle.
The problem is that, although the center of the circle is defined in Latitude/Longitude coordinates, it's radius is defined in meters. So how can I calculate if a position of a place in LatLng is contained within a radius defined in meters?
Thank you
All you need is to calculate if two LatLngs are closer to each other than radius.
For that you can use Location.distanceBetween.
See the Circle.contains function here for an example.
Basically what you are going to have to do is test all points and use ray casting to test the points against sides the circle.
If you find only 1 collision with a side then the point is inside the circle, if you find 2 collisions spots then it is outside the circle.
It sounds intimidating but its not too bad once you understand it
Here is excellent documentation on what is involved. I used a combination of the first and second answer to something similar
My maths isn't that good so I'm having a bit of trouble in one of my applications that I'm trying to do where I want a rectangle to represent a vehicle and I want that vehicle/rectangle to "drive" around in a circle. Imagine a roundabout with only 1 vehicle in it, just circling around forever.
If I can get some help how to do that then I'll be able to build on the example and most importantly learn.
If someone could write up a simple example for me I'd be grateful. No background no images, just a rectangle "driving" around in a circle. I'm using java and Swing.
Sorry, I am not sure if could understand clear you exactly need. If you need to draw rectangle which is moving around inside of circle, you can use sin/cos functions.
Something like that:
double r = 50.0; // radius (it might radius of your circle, but consider dimensions of rectangle to make sure you are drawing inside of circle, e.g. circleRadius - rectangeDimesion / 2.0)
for (int f = 0; f < 360; f++) {
double x = Math.sin(Math.toRadians((double)f)) * r;
double y = Math.cos(Math.toRadians((double)f)) * r;
// draw rectangle on [x, y] coordinates
}
If you know the radius of the round about, all the you would need would be a trigonometric function and the angle which the vehicle makes to the round about. You could take a look at this simple introduction which should get you started in the right direction.
On another hand, another approach would be to use a Transformation Matrix where you start with a matrix containing two points (your X and Y co-ordinates) and you transform them to become the new co-ordinates.
You can then rotate the rectangle to mimic a vehicle turning.
If you have a limited background in Mathematics, the first option might be easier for you to grasp.
This is more an extended comment than an answer.
I would divide the problem up into several easier problems, and work on each of them separately:
Draw your rectangle with a specified center location and long axis orientation.
Determine the center point and long axis orientation for an object orbiting around the origin. Note that to get make the long axis a tangent it needs to be perpendicular to the radius through the center.
Translate the whole system so that it orbits the desired point, rather than the origin.
I'm trying to draw a 5 point star in AWT.
Each point in the 2d grid is 72 degrees apart - so I thought I could draw the polygon using only 5 points by ordering the points 144 degrees apart, so the polygon gets fed the points in order 1,3,5,2,4
Unfortunately, this involves a lot of intersecting lines, and the end result is that there are 5 triangles with my desired colour, circling a pentagon that has not been coloured.
Looking through, it has something to do with an even-odd rule, that intersecting points will not be filled.
I need my star to be drawn dynamically, and using the specific shape described (for scaling and such).
If I manually plot the points where it intersects, I get some human error in my star's shape.
Is there any way to just turn this feature off, or failing that, is there a way to get the polygon to return an array of x[] and y[] where lines intersect so I can just draw another one inside it?
Thanks.
Draw it with ten points, 36 degrees apart, at two alternating radii.
Establish the 10-point Polygon in cartesian coordinates, as suggested by relet and as shown in this example. Note how the coordinate system is centered on the origin for convenience in rotating, scaling and translating. Because Polygon implements the Shape interface, the createTransformedShape() method of AffineTransform may be applied. A more advanced shape library may be found here.
Is there a way to get the polygon to return an array of x[] and y[] where lines intersect?
Although typically unnecessary, you can examine the component coordinates using a Shape's PathIterator. I found it instructive to examine the coordinates before and after invoking createTransformedShape().
I am doing a mashup using Google Maps under Grails where users can create geofences by selecting a point on the map and a radius. This get stored on my database and the application receives constantly a set of coordinates from a GPS device.
I would like to compare the received coordinates with the area stored in the circles. If the point is inside (or outside) the circle the program will fire an action. However, I would like to know how I can find out if the coordinates are located inside/outside the circle. There is a Javascript library which allows doing this but I need to do this on the server.
Is there a Java (or even Groovy) library for this?
How would you implement it?
if distance from point to center of circle is <= radius of circle then it is inside the circle.
if the area is made of more than one circle than compare to all the circles... it won't take that long.
java.awt.geom.Point2D.Double is perfect for this.
Well, if it doesn't need to be "perfect", you don't need to worry about plotting circles or anything like that. You can just take the two locations (the location you want to test, and the center of the circle) and use Pythagorus to find the distance. If that distance is less than the radius of the circle, it's inside.
There is a caveat to take into consideration, however: the reason this wouldn't be perfect is that that for your points, you're probably going to get a latitude and longitude...and the Earth is a sphere. So near the poles of the Earth this will kind of fall apart. But it may well be good enough for what you're doing.
Sadly, most of the responses here won't work for you conveniently, because GPS coordinates are in units of degrees. You will need something to convert from two points in Degrees of latitude and longitude to a great circle distance, which simple Pythagorean theorem falls short of.
If you're using Google maps API, you can probably do everything you need using GLatLng. As other posters have noted, You can determine the distance between two points is less than the radius of the specified circle. Specifically GLatLng.distance(other:GLatLng) returns the meters distance between too GPS locations.
To actually display the circles requires a bit more finesse. You will need to create a GPolygon to draw the circumference of the circle. You can find a number of free JavaScript functions that can do this for you.
Victor and Beska have the correct answer. That is, if the distance between the point and the center is less than the radius, then it's in the circle.
For the great circle distance between two points, you can use GeoTools' GeodeticCalculator. In particular you set the point and radius using setStartingGeographicPoint and setDestinationGeographicPoint followed by calling getOrthodromicDistance which will return the distance.
You want to find the vector that is the distance between the selected coordinate and the center of the circle, then compute the square distance between the selected coordinate and the center of the circle by squaring the components of the vector and adding them together; if that scalar (the squared distance) is less than the square of the radius, the point is within the circle.
This method avoids having to take a square root, and is just as accurate as normal distance comparison.
One possibility is to calculate the distance from the centerpoint and compare it to the radius.
Depending on you application you may be have to take into account that the world is a sphere and not 2Dimensional. To calcualte a distance on earth you can use this formula.
Since you are using Google Maps and for geographical distances spherical geometry holds rather than euclidean geometry. However if it is relativley smaller distance like a parking lot etc. then you can use euclidean distance formula (http://en.wikipedia.org/wiki/Distance) to find out whether the point is inside or outside the circle.
I presume you know the coordinates of the circle's center C(xc, yc) and its radius, R. Then for a given point P(x1, y1) find the euclidean distance, D as
square-root((x1-xc)^2 + (y1-yc)^2)). If D > R, the point lies outside the circle. If D < R, the point lies inside the circle. If D = R, the point lies on the circumference of the circle.
In case you are doing your measurements over larger distances then you should rather look for Geodesics (please check this http://en.wikipedia.org/wiki/Great-circle_distance).
I hope it helps.
cheers