Draw polygon and detect if a marker is inside - google-maps-api-3

I have these coordinates stored in a field of a database for drawing a polygon on a google map. I want to detect if a marker is inside this polygon:
(37.15730677081955, -3.7892532348632812),
(37.13486648362684, -3.7593841552734375),
(37.16059015678727, -3.7027359008789062)
Does anyone know how to get it?

You can use google map geometry library containsLocation() method with something like this:
for (var j=0; j < allMarkers.length; j++){
for (var i=0; i < createdShapes.length; i++) {
var latlong = allMarkers[j].getPosition();;
if(google.maps.geometry.poly.containsLocation(latlong, createdShapes[i]) == true) {
allMarkers[j].setOptions({
icon : "http://labs.google.com/ridefinder/images/mm_20_white.png",
});
}
}

Here is some code written in Java to detect if a GPS Coordinate is inside a lat/long bounding box, it is mathematical, so it could probably be translated to other languages.
Detecting whether a GPS coordinate falls within a polygon on a map

Related

How to create circle or buffer of 1 KM around specified waypoint in arcGIS JAVA SDK?

I'm using this code snippet to draw circle, Circle shape is fine but circle covers even thousand km during zoom-out. I need to fix this circle around specified radius.
private void addPointGraphic(double lat, double lng, float radius) {
if (graphicsOverlay != null) {
Viewpoint viewpoint = new Viewpoint(latitude, longitude, 12);
final ListenableFuture<Boolean> viewpointSetFuture = mapView.setViewpointAsync(viewpoint, 5);
SimpleMarkerSymbol pointSymbol = new SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, color, radius);
pointSymbol.setOutline(new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, hexGreen, 1.5f));
Point point = new Point(lng, lat, SpatialReferences.getWgs84());
Graphic pointGraphic = new Graphic(point, pointSymbol);
graphicsOverlay.getGraphics().add(pointGraphic);
}
}
The ArcGIS Runtime SDK for Java has many samples available for different workflows.
Perhaps the Buffer sample suits what you are trying to do:
https://github.com/Esri/arcgis-runtime-samples-java/tree/master/geometry/buffer
Esri's GeoNet forums also have dedicated message boards for all the SDKs, so any perticular questions regarding the Java SDK might get more visibility from that community there:
https://community.esri.com/t5/arcgis-runtime-sdk-for-java/bd-p/arcgis-runtime-sdk-for-java-questions

How do i draw a polyline in android studio for array of coordinates?

http://107.180.68.111:84/ZIGAPICleveland/api/Trip/GetAllShapes?Page=0
this is the api i got and how do i draw a polyline for this?
here is the code, first get an ArrayList of coordinates from the above link using JSON parser then use this code
PolylineOptions polylineOptions = new PolylineOptions().
geodesic(true).
color(Color.BLUE).
width(10);
for (int i = 0; i < coordinates.size(); i++)
polylineOptions.add(coordinates.get(i));
mMap.addPolyline(polylineOptions);
here coordinates is the arraylist of location coordinates, mMap is GoogleMap object

How to use a QSGSimpleTextureNode?

I'm trying to understand how do I use a QSGSimpleTextureNode but Qt documentation is very vague. I want to render text on the scene graph, so basically what I want is to draw a texture with all the glyphs and then set that texture on a QSGSimpleTextureNode. My idea was to create the texture using standard OpenGL code and set the texture data to the data I have just created. I can't find an example to show me how to achieve this.
I would use QSGGeometryNode instead of QSGSimpleTextureNode. If I am not wrong it is not possible to set the texture coordinates in a QSGSimpleTextureNode. You could write a custom QQuickItem for the SpriteText and override the updatePaintNode:
QSGNode* SpriteText::updatePaintNode(QSGNode *old, UpdatePaintNodeData *data)
{
QSGGeometryNode* node = static_cast<QSGGeometryNode*>(old);
if (!node){
node = new QSGGeometryNode();
}
QSGGeometry *geometry = NULL;
if (!old){
geometry = new QSGGeometry(QSGGeometry::defaultAttributes_TexturedPoint2D()
,vertexCount);
node->setFlag(QSGNode::OwnsGeometry);
node->setMaterial(material); // <-- Texture with your glyphs
node->setFlag(QSGNode::OwnsMaterial);
geometry->setDrawingMode(GL_TRIANGLES);
node->setGeometry(geometry);
} else {
geometry = node->geometry();
geometry->allocate(vertexCount);
}
if (textChanged){
//For every Glyph in Text:
//Calc x + y position for glyph in texture (between 0-1)
//Create vertexes with calculated texture coordinates and calculated x coordinate
geometry->vertexDataAsTexturedPoint2D()[index].set(...);
...
node->markDirty(QSGNode::DirtyGeometry);
}
//you could start timer here which call update() methode
return node;
}

Google Maps Directions API - Marking a point x km from starting point [duplicate]

This question already has an answer here:
Draw Route X Kilometers from Origin
(1 answer)
Closed 5 years ago.
I am trying to build a web application to show the position of users in a map, who are participating in a virtual trek. These treks are long distance one, typically >500km, on highways. The information I know is the trek route (start, end along with way points) and distance he has covered with respect to the starting point.
How in GMaps V3, I can plot the marker, which is x km from the starting point, along the drawn direction by Google maps.
Figured out this. The technique involves getting the individual lat long steps and incrementally calculate the distance. The individual steps are close enough to do a straight line approximation between two consecutive points.
A reverse traverse is done for the last overshooting point.
The error rate is 0.1%, irrespective of the distance. ie a distance of 2000 km will be off by 2 km on map.
//Returns the Google LatLng object corresponding to distanceInKM for the given route.
//Params : directionResult : The google.maps.DirectionsResult obtained from DirectionService.route(request:DirectionsRequest, callback:function(DirectionsResult, DirectionsStatus))
//distanceInKM : The distance offset with respect to the starting point.
function getLatLngOnRoute(directionResult, distanceInKM) {
var lastPos=directionResult.routes[0].legs[0].steps[0].path[0]; var currPos;
var distance=0.0;
distanceInKM*=1000;
//Will not consider alternate routes.
for (var j=0; j<directionResult.routes[0].legs.length; j++) {
//There may be multiple legs, each corresponding to one way point. If there are no way points specified, there will be a single leg
for (var k=0; k<directionResult.routes[0].legs[j].steps.length; k++) {
//There will be multiple sub legs or steps
for (var l=0; l<directionResult.routes[0].legs[j].steps[k].path.length; l++) {
currPos=directionResult.routes[0].legs[j].steps[k].path[l];
//Calculate the distance between two lat lng sets.
distance+=google.maps.geometry.spherical.computeDistanceBetween(lastPos, currPos);
if (distance>distanceInKM) {
//If the exact point comes between two points, use distance to lat-lng conversion
var heading=google.maps.geometry.spherical.computeHeading(currPos, lastPos);
var l= google.maps.geometry.spherical.computeOffset(currPos, distance-distanceInKM, heading);
return l;
}
lastPos=currPos;
}
}
}
}
This topic set me in the correct direction.

CSG operations on implicit surfaces with marching cubes

I render isosurfaces with marching cubes, (or perhaps marching squares as this is 2D) and I want to do set operations like set difference, intersection and union. I thought this was easy to implement, by simply choosing between two vertex scalars from two different implicit surfaces, but it is not.
For my initial testing, I tried with two spheres circles, and the set operation difference. i.e A - B. One circle is moving and the other one is stationary. Here's the approach I tried when picking vertex scalars and when classifying corner vertices as inside or outside. The code is written in C++. OpenGL is used for rendering, but that's not important. Normal rendering without any CSG operations does give the expected result.
void march(const vec2& cmin, //min x and y for the grid cell
const vec2& cmax, //max x and y for the grid cell
std::vector<vec2>& tri,
float iso,
float (*cmp1)(const vec2&), //distance from stationary circle
float (*cmp2)(const vec2&) //distance from moving circle
)
{
unsigned int squareindex = 0;
float scalar[4];
vec2 verts[8];
/* initial setup of the grid cell */
verts[0] = vec2(cmax.x, cmax.y);
verts[2] = vec2(cmin.x, cmax.y);
verts[4] = vec2(cmin.x, cmin.y);
verts[6] = vec2(cmax.x, cmin.y);
float s1,s2;
/**********************************
********For-loop of interest******
*******Set difference between ****
*******two implicit surfaces******
**********************************/
for(int i=0,j=0; i<4; ++i, j+=2){
s1 = cmp1(verts[j]);
s2 = cmp2(verts[j]);
if((s1 < iso)){ //if inside circle1
if((s2 < iso)){ //if inside circle2
scalar[i] = s2; //then set the scalar to the moving circle
} else {
scalar[i] = s1; //only inside circle1
squareindex |= (1<<i); //mark as inside
}
}
else {
scalar[i] = s1; //inside neither circle
}
}
if(squareindex == 0)
return;
/* Usual interpolation between edge points to compute
the new intersection points */
verts[1] = mix(iso, verts[0], verts[2], scalar[0], scalar[1]);
verts[3] = mix(iso, verts[2], verts[4], scalar[1], scalar[2]);
verts[5] = mix(iso, verts[4], verts[6], scalar[2], scalar[3]);
verts[7] = mix(iso, verts[6], verts[0], scalar[3], scalar[0]);
for(int i=0; i<10; ++i){ //10 = maxmimum 3 triangles, + one end token
int index = triTable[squareindex][i]; //look up our indices for triangulation
if(index == -1)
break;
tri.push_back(verts[index]);
}
}
This gives me weird jaggies:
(source: mechcore.net)
It looks like the CSG operation is done without interpolation. It just "discards" the whole triangle. Do I need to interpolate in some other way, or combine the vertex scalar values? I'd love some help with this.
A full testcase can be downloaded HERE
EDIT: Basically, my implementation of marching squares works fine. It is my scalar field which is broken, and I wonder what the correct way would look like. Preferably I'm looking for a general approach to implement the three set operations I discussed above, for the usual primitives (circle, rectangle/square, plane)
EDIT 2: Here are some new images after implementing the answerer's whitepaper:
1.Difference
2.Intersection
3.Union
EDIT 3: I implemented this in 3D too, with proper shading/lighting:
1.Difference between a greater sphere and a smaller sphere
2.Difference between a greater sphere and a smaller sphere in the center, clipped by two planes on both sides, and then union with a sphere in the center.
3.Union between two cylinders.
This is not how you mix the scalar fields. Your scalars say one thing, but your flags whether you are inside or not say another. First merge the fields, then render as if you were doing a single compound object:
for(int i=0,j=0; i<4; ++i, j+=2){
s1 = cmp1(verts[j]);
s2 = cmp2(verts[j]);
s = max(s1, iso-s2); // This is the secret sauce
if(s < iso) { // inside circle1, but not inside circle2
squareindex |= (1<<i);
}
scalar[i] = s;
}
This article might be helpful: Combining CSG modeling with soft blending using
Lipschitz-based implicit surfaces.

Resources