I need to count and list the markers that are within a figure either a sirculo or a rectangle.
The operation would be that when you insert a figure on the map I list and count how many markers inside it.
The Circle and Rectangle classes have a getBounds() function, which returns a LatLngBounds object. Add your circle/rectangle and get its bounds. Loop over your markers, calling the contains() function on each marker to see if it is contained in that latlng bounds.
You may need to be slightly cleverer with the circle, as its bounds will be for the bounding box that surrounds it, so you might have markers that fall outside the circle but still within that box. In which case you'd need to work something out based on the radius of the circle.
Related
I try to use 2 ellipses to detect a collision if they overlap. I have to rotate the ellipses but I can't figure out how this works. I'm working with the "com.badlogic.gdx.math.Ellipse" class but it seems to have no method for rotating. Any ideas? Thx in advance!!
Unfortunately, LibGDX doesn't have in-built rotating functions for ellipses.
Instead, I'd either be resorting to a circle in which rotation does not matter, or use polygons to check intersection.
Polygons are formed through an array of float values (vertices), where, every even element of the array is the horizontal component (x) and the odd, the vertical component (y).
Polygon polygon1 = new Polygon(vertexSet1);
Polygon polygon2 = new Polygon(vertexSet2);
Then, by using an Intersector, you can then check whether these polygons have intersected. The more vertices, the more accurate your shape will be. Just remember to have 6 or more elements in your vertex array, as the 6 floats will give 3 (x, y) points which is the minimum required for a polygon.
if (intersector.overlapConvexPolygons(polygon1, polygon2) {
//do your intersection code
}
The polygons themselves have commands to translate, scale and rotate, allowing for the rotations you mentioned above.
I am using bing maps and I want to query my database to return all values inside the map bounds, so every time, the map moves, I want to query it again.
In order to make it more efficient, I want to query only the boundary I haven't query before.
So I get the previous bound and the current bound and want to get the square bound of the non shared rectangle between the previous and the current rectangles (The not shared rectangle of the current bounds).
For example, If I move the map right for 5cm and up for 2cm, I will recieve a new LocationRect of the rectangle 5cm 2cm (the not shared).
I have the map bounds:
LocationRect currentBounds = map.Bounds;
When I move the map I get a new bounds, but before I save the previous bounds:
previousBounds = currentBounds;
I want to get the new location I moved to (only the new, not the whole currentBounds).
So I want to do something like this:
LocationRect newMapBounds = currentBounds.NotSharedBounds(previousBounds);
But how can I check this? I saw there is a method of Intersects but it returns bool, and I need to get the new LocationRect...
I will be very thankfull for the helper :)
If i understand you right you have the blue rectangle(ABCD) and when you move the map
you have red rectangle(EHGF) and you know their vertexes coordinates
e
So the not common space creating 3 new rectangles for you: Green Yellow And Black.
And you need coordinates of those 3 rectangles in order to query your data, in other words you need to perform three queries to you DB in order to get the NOT common space of BLUE and RED rectangles.
You will have scenarios of RED and BLUE rectangles that you need to deal before you start the calculations:
The rectangles are coincide.
The scenario in the picture
They have no common space at all.
For example the vertexes coordinates of GREEN rectangle are(The second scenario):
T(x) = E(x), T(y) = D(y)
F is a common of RED and GREEN Rectangles
N(x) = B(x) , N(y) = G(y)
F is a common of BLUE and GREEN Rectangles
Hope it helps.
I want to achieve the following: "Place an array of markers that may be distributed across a large range of lat, lng into a fitbounds method that places the fitbounds inside a custom rectangle."
The image below should clarify what I'm trying to achieve. Basically I have an array of markers that I want to make sure always fits within a small box on the right hand side of my page.
The white box contains some information pertaining to the markers, which will always be present, so I don't want any markers hidden behind the white box, and I'd love if I could define that they live within the black box. (Note the black box is just a visual reference for this question).
You can use the containsLocation to ensure a point is inside of a polygon. See here.
As you go through each coordinate pair in your array, verify the location is within the polygon area, then add to the map accordingly. You can also set an attribute to those points to "define" what extent they are in.
var latlng = new google.maps.latLng(array[n]);
if (google.maps.geometry.poly.containsLocation(latlng, polygon)){
marker = new google.maps.Marker({
position: latlng,
map: map
});
marker.dataset.box = 'blackbox';
} else {
alert('Not inside black box');
}
If you're using HTML5, you can add a dataset attribute to markers that are within the polygon.
marker.dataset.box = 'blackbox';
If not, you can use setAttribute.
marker.setAttribute('data-box', 'blackbox');
I'm answering my own question (hopefully for your benefit), but this is the issue:
I've created a world map with polylines (in Google maps API v3). Some of these lines span the IDL (international date line). I want to place directional arrows on some of my lines, one arrow in the center of each line. On lines that span the IDL, the arrow center calculated by averaging the 2 end points together:
(latlng1.lat()+latlng2.lat()/2), (latlng1.lng()+latlng2.lng()/2)
draws the line the opposite way around the world leaving a floating arrow on my map instead of in the center of the actual line.
I initially sought a formula for getting the reverse of the line and somehow determining which was used. But it's too dependent on the map display. I even went so far as to see if I could get the center from the end points of the line if I first converted them to pixels, but the data conversion had no effect on the outcome.
So I then thought to look for an intersect method on the polyline, passing a point to check - and if it wasn't, find the center point from the reverse line (which I believe would 180-center.lng()) but there is no intersection method on polylines. So I started looking into the bounds object, because there are some intersection/contains methods on bounds.
So here is a very simple solution for a relatively undocumented problem:
(credit for function to retrieve bounds goes to Ben Appleton, ty):
function getBoundsForPoly(poly) {
var bounds = new google.maps.LatLngBounds;
poly.getPath().forEach(function(latLng) {
bounds.extend(latLng);
});
return bounds;
}
var poly_bounds = MapAbstraction.get_poly_bounds(line);
var center_point =poly_bounds.getCenter();
and then I can pass my center_point to my marker creation tool to generate the arrow in the actual center of the line without worrying about which way it was drawn.
I need to do a search for markers that are within a circle using as parameters the radius and position of the center of the circle. Someone can help me on how serious this search.
Concept:
make an array of the markers you want to search
iterate through the array of markers calculating the distance between each marker and the center point using the geometry library computeDistanceBetween()
function
compare that distance to the radius of the circle, if it is less than the radius, the marker is inside the circle, otherwise it is outside the circle.
example (with center point from geocoded address)