I have an app that adds a 50mi radius around the current/clicked marker (from mapMarkers[]). I know the selected marker's index number and its LatLng position as "myPosition" and the other markers' LatLng position as mapMarkers[i].point.
After the circle is drawn on the map, I call the following simple function to step thru all the mapMarkers[] to compute the distance from the current marker, and then count how many markers actually fall within the 50mi radius. Simple, right?
Problem: The following routine does not produce any result upon first iteration (i.e., first click) for the current marker. But DOES produce the expected result upon the 2nd and subsequent clicks on the current marker.
function inCircle() {
var j = 0;
var myDistance = 0;
for (var i = 0; i < mapMarkers.length; i++) {
myDistance = google.maps.geometry.spherical.computeDistanceBetween(myPosition, mapMarkers[i].point);
myDistance = parseInt(myDistance/1609.34) //meters to miles
if (myDistance <= 50) {
j = j + 1;
//Do more stuff...
}
}
alert("Markers inside circle: " + j);
}
I call this function just after I BIND the circle to the selected marker. "myPosition" is declared as the map loads. Commenting-out the distance calculations produces results on the first iteration for the current marker.
Any thoughts what might be causing this issue?
WRT my comment above: I think, the geometry library is only loading because you made a request. see here developers.google.com/maps/documentation/javascript/libraries. Include the geometry-spherical library when you call the google maps js(during page load).
Eg:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=drawing,geometry,weather"></script>
Related
I am trying to get smooth route for below route path using road API in Roads Inspector but not getting smooth route.Google road api failing to provide smooth route.Some portion of route is not smooth with actual road route e.g at turns, at bridges etc. Please have look for below route path and provide solution to get smooth route/more accurate route along actual road.
Points:
42.04144333333333,-88.060575|42.04123666666667,-88.06014333333333|42.04119166666667,-88.06017166666666|42.040835,-88.05990166666666|42.03982666666667,-88.05242333333334|42.03903666666667,-88.04572333333333|42.03903833333333,-88.04572|42.038495,-88.04141833333334|42.03774666666666,-88.03593833333333|42.037683333333334,-88.03549|42.034861666666664,-88.03204166666667|42.02808,-88.031215|42.02045666666667,-88.03131166666667|42.012881666666665,-88.02988833333333|42.00522333333333,-88.02747666666667|41.997353333333336,-88.02500333333333|41.98961333333333,-88.02349333333333|41.982191666666665,-88.02351333333333|41.97412833333333,-88.02447333333333|41.96599,-88.02588166666666|41.95825833333333,-88.027075|41.952605,-88.03345|41.945281666666666,-88.0377|41.937595,-88.03779333333334|41.92935,-88.037845|41.92084333333333,-88.03799166666667|41.91231,-88.038075|41.90379,-88.038145|41.89564,-88.03784166666667|41.887255,-88.036495|41.87881,-88.03291666666667|41.87096833333333,-88.03694333333334|41.863101666666665,-88.04085166666667|41.85484833333334,-88.04049166666667|41.848978333333335,-88.03315166666667|41.842145,-88.02940833333334|41.83407,-88.02922|41.826135,-88.029025|41.820256666666666,-88.02674333333333|41.813515,-88.02884833333333|41.80965333333333,-88.03722166666667|41.810065,-88.04824833333333|41.8104,-88.06018333333333|41.81016666666667,-88.07216833333334|41.80704166666666,-88.08223833333334|41.80573666666667,-88.09275|41.80591166666667,-88.10409166666666|41.80608,-88.11518333333333|41.80625166666667,-88.12632166666667|41.806415,-88.13737333333333|41.80649666666667,-88.14849166666667|41.80653,-88.15959333333333|41.80652666666667,-88.17042666666667|41.805715,-88.181295|41.80482833333333,-88.19194333333333|41.803918333333336,-88.202765|41.80304666666667,-88.212815|41.802146666666665,-88.22354833333333|41.801383333333334,-88.23485666666667|41.80068833333333,-88.24686666666666|41.8,-88.25845333333334|41.799368333333334,-88.26976833333333|41.798743333333334,-88.28041666666667|41.80003166666667,-88.28312833333334|41.795566666666666,-88.28211666666667|41.79022,-88.28205833333334|41.785465,-88.28198|41.784135,-88.28193833333333|41.782473333333336,-88.283865|41.78230833333333,-88.28874666666667|41.782226666666666,-88.288225|41.781863333333334,-88.287305|41.78176833333333,-88.28751333333334|41.78176833333333,-88.28751333333334
points in the "inspector"
Your points don't look like the full output of a GPS tracker (the use case for which this API is designed), and thus you have stretches that are too far apart. Please increase the resolution of your GPS recording to get better output.
Google maps seems not to snap far points,
(if the distance between is longer than 300m - 400m).
As a solution I added some fake points between points that too far apart.
And now Google Maps snapping my route correctly.
List<LatLon> extendedPointsList = new List<LatLon>();
var lastLatLon = OrigionalRoute[0];
extendedPointsList.Add(lastLatLon);
indexesBeforeAddingPoints.Add(0);
for (int i = 1; i < OrigionalRoute.Count; i++)
{
var currentLatlon = OrigionalRoute[i];
double estimatedDistance =
getEstimatedDistanceBetweenPoints(currentLatlon, lastLatLon);
//estimated 400 meters
if (estimatedDistance > 0.004)
{
//estimated 340 meters
int countOfPoints = (int)Math.Round(estimatedDistance / 0.0034);
if (countOfPoints > 1)
{
var latDiff = (currentLatlon.Lat - lastLatLon.Lat) / countOfPoints;
var lonDiff = (currentLatlon.Lon - lastLatLon.Lon) / countOfPoints;
for (int j = 1; j < countOfPoints; j++)
{
var aveLat = lastLatLon.Lat + (latDiff * j);
var aveLon = lastLatLon.Lon + (lonDiff * j);
indexesBeforeAddingPoints.Add(i - 1);
extendedPointsList.Add(new LatLon(aveLat, aveLon));
}
}
}
indexesBeforeAddingPoints.Add(i);
extendedPointsList.Add(currentLatlon);
lastLatLon = currentLatlon;
}
OrigionalRoute = extendedPointsList;
The method to estimate distance (I ignored earth's sphere and projection).
private static double getEstimatedDistanceBetweenPoints(LatLon pointA, LatLon pointB)
{
return Math.Sqrt(Math.Pow((pointA.Lat - pointB.Lat), 2) + Math.Pow((pointA.Lon - pointB.Lon), 2));
}
I have searched and simply cannot find what I need (if it exists).
A window will have a large picture.
The picture will be divided into zones (such as border lines that separate states on a map).
When a person clicks within a zone, then I will raise the appropriate event.
I've used AS3 with MXML to create a database program. All is working great except for this last step. I cannot figure out how the user is within a particular area of the picture when he clicks or when he touches.
I've read and tried to come up with an approach, and there must be (hopefully so) an easier way than the muddled nonsense I'm coming up with.
Thanks
VL
Are you drawing it in flash professional CS6? If so then why can't you just have the picture as a symbol and then just self divide the lines and make those divided areas into symbols that are children of the picture symbol. You could keep the individual state symbols right where they so that they stay true to the overall picture.
A first thought would be to make an instance of this picture symbol through the code, and then loop though all the children of that picture and add a click event to each one.
var picture:Picture=new Picture();
for(var i:int=0; i<picture.numChildren-1; i++){
picture.getChildAt(i).addEventListener(MouseEvent.CLICK, mouseEventHandler);
}
Please comment if I am missing something, or this does not work.
EDIT
Well, if you know the dimensions of the image, you could divide its width and height by 3(your number of rows and columns) and this is your zone dimensions. You then could take the mouse's click point relative to the top left of your picture and then divide its width by the zone with, and its height by the zone height, and then get its integer floor value, you could get which region it is. Code it below:
//This is all for a constat region list (like a window, or floor tiles, not things irregular)
import flash.display.Sprite;
var regionsX:int = 3; //Your number of windows across the row
var regionsY:int = 3; // across the column
var regions:Array = new Array(); // an array to hold the values that you will get from where the user clicks
// All of this used a 2D array method
for(var x:int = 0; x < regionX; x++) {
regions[regionsX] = new Array();
for(var y:int = 0; y < regionY; y++) {
regions[regionsX][regionsY] = "region(".concat(x).concat(",").concat(y);
// Here you make this equal to anything you want to get a value of,
//once the correct region is found (I just have a string version here for an example)
}
}
... // other stuff..
var picture:Picture = new Picture(); // your window picture
var regionWidth:Number = picture.width / regionsX; // Gets each region's width
var regionHeight:Number = picture.height / regionsY; // Get each regoin's height
...
picture.addEventListener(MouseEvent.CLICK, mouseEventListener); // add a click listener to the picture
function mouseEventListener(event:MouseEvent):void{
var mouseX:Number = picture.globalToLocal(event.stageX); // gets where the user clicked, and then converts it
//to the picture's cordinate space. ( 50,100 acording to the stage, could be (25,200) to the picture)
var mouseY:Number = picture.globalToLocal(event.stageY); // same for the Y
var regionIntX:Number = Math.floor(mouseX / regionWidth); // Dives the point by each region's width, and then
// converts it to a while integer. (For instance, if a region's width is 100 and you click at 288, then if you do the
// math, you clicked in the 3rd region, but it returns 2... why? (becaue the array counter starts at 0, so 0 is the 1st
// region, 1 is the second and so on...
var regionIntY:Number = Math.floor(mouseY / regionHeight); // Same for Y
var yourValue:String = regions[regionIntX][regionIntY]; // This returns that you initialy put into your 2d array
// by using the regionIntX and regionIntY for the array values. You have to decide what is stored in this array...
}
The simplest solution would be to add an event listener for MouseEvent.CLICK to the picture and in the handler check properties mouseX and mouseY of the picture. Define the bounds of each area in an XML or similar and check against current mouseX/Y to see which area has been clicked.
Do anybody know how the google map bound calculation can be done without using any UI element and Javascript.
I have set of points and different zoom level. I can probably add the screen size and I need to calculate the bounds for provided coordinate and zoom level. I am trying to do this in the plain C# code.
Please help.
as for calculating bounds - you can do it easily by walking array of your coordinates and extend bounds rectangle if point falls out. First coord is a start. I don't familiar with C# but there is the algo using pseudocode:
points = Array of coord(lat, lng)
bounds = object {
top: null
left: null
right: null
bottom: null
function extend(coord: (lat, lng))
{
if (this.top == null) // empty
{
this.top = coord.lat; this.bottom = coord.lat;
this.left = coord.lng; this.right = coord.lng;
}
else
{
if (coord.lng < this.left) this.left = coord.lng;
if (coord.lat < this.bottom) this.bottom = coord.lat;
if (coord.lng > this.right) this.right = coord.lng;
if (coord.lat > this.top) this.top = coord.lat;
}
}
}
But of course much simplier way is to use already written google functionality.
Zoom level can be calculated somehow from the size of bounding box (for example you can find a table with rates in km or miles per pixel, or approximate width or map), but most comfortable way is map.fitBounds(bounds)
I've got a grid of sprites.
Now I would like to drag an image on a grid-element.
Therefore I need to know which x/y of the grid-element is the closest point to the mouse-position.
All the grid-elements are stored in an array.
How can I achieve that?
You must loop through all the elements and find the smallest distance to the mouse. Then store the array index of the element. Try something like this:
// Setup variables outside of loop.
var mousePoint:Point = new Point(mouseX, mouseY);
var elementPoint:Point = new Point();
var element:Sprite;
var closestIndex:uint = 0;
var closestDist:Number;
// Loop through elements
for (var i:int = 0; i < gridElements.length; i++)
{
element = gridElements[i] as Sprite;
// Set the elementPoint's x and y rather than creating a new Point object.
elementPoint.x = element.x;
elementPoint.y = element.y;
// Find distance from mouse to element.
var dist:Number = Point.distance(mousePoint, elementPoint);
// Update closestIndex and closestDist if it's the closest.
if (i == 0 || dist < closestDist)
{
closestDist = dist;
closestIndex = i;
}
}
// Can now use closestIndex to get the element from the array.
trace('The closest element is at index', closestIndex, ', with a distance of', closestDist);
Of course, this will only get you the first closest element, so you must decide what you want to happen if there are two elements an equal distance away.
You will also need to allow for the origins of your elements. The will probably have there origins set to there top left, so you need to allow for this in your distance calculation.
You could also check for a minimum distance. So if the user is dragging too far away from any of the elements then do nothing.
If all you need is the x,y of the closest grid then all you have to do is.
var gridX:int = Math.floor(mouseX / NumberOfColumns);
var gridY:int = Math.floor(mouseY / NumberOfRows);
This will convert your mouse coordinates to your grid coordinates.
Now comes the problem. If your storing them in a 2d array then you have your x/y if your storing them in a flat array (1d) you need to look it up just like you created it.
var myObject:Object = my2dArray[gridX, gridY];
var myObject:Object = myFlatArray[(gridX * NumberOfRows) + gridY];
If you have taken care of how you create your array and push the items in it, it should be no problem to retrieve stuff without searching it.
I have divided the my google map display in to numbers of parts, Now I want of find it out if any markers are positioned inside a/any particulate cell.
Any Help ?
Farther Explained :
I have got the map bounds by
map.getBounds();
method and then farther divide it into numbers of sub-bounds.
also I have putted markers as
map.addOverlay(markerObject);
Now , I want find if of the cells (which I got by dividing the map by bounds) is containing any markers or not .
I have divide the entire map bounds into numbers of sub bounds
So keep all markers in array. Each marker has a method called get_position( ). After you have finished division of map bound into small sub bounds, you just need to iterate over the sub bounds and check whenever the marker within it.
PS. Also take a look on it, in some cases could be useful.
Suppose you on sub bound cell:
var sub_bounds = new Array();
// here you've pushed into an array the sub bounds
for ( var i = 0; i<sub_bounds.length; ++i)
{
for ( var j = 0; j < markers.length; ++j)
{
var lat = markers[j].get_position( ).lat;
var lng = markers[j].get_position( ).lng;
if ( sub_bounds[i].sw.lat<lat && lat<sub_bounds[i].ne.lat &&
sub_bounds[i].sw.lng<lng && lng<sub_bounds[i].ne.lng)
// marker within cell, do whatever you need to do
}
}
Here is an alternative to the above solution without re-iteration:
First - how big are your sub_bounds? Say 10 latitude and longitude degrees each.
Second - The position of the marker is (floor(marker.lat / 10), floor(marker.lng / 10))
Third - Each marker is added to the map and dropped in a bucket for that subdomain.
so (40, -78) would lie in bucket (4,7) i.e. bucket["4~7"]
Correction: would lie in bucket (4,-7) i.e. bucket["4~-7"]
which would contain all markers between 40 and 50 lat and -70 and -80 lng.
You can use GLatLngBounds as the object that holds all these markers in each bucket, which would give you a good set of methods to use, such as calculating center of the bucket depending on the markers currently in it.
Probably the best solution is given here: how to find out whether a point is inside a polygone:
How to detect that a point is inside a Polygon using Google Maps?