This question already has answers here:
Google Maps API v3 Polygon closing
(2 answers)
Closed 8 years ago.
When working with polygons on google maps, there can be cases where a polygon will cross the map overlap. For example, when going to the right edge of the map, and placing points to create a box, with the left points on the right edge of the map, and the right points on the left edge of the repetition of the map (as the map can pan infinitely horizontally).
That said, I would see points close to the +180 and -180 extremes such as:
[50, 178] top left
[50, -178] top right
[-50, -178] bottom right
[-50, 178] bottom left
This is the data provided when pulled out from looping the polygon.getPath() points.
google.maps.event.addListener(drawingManager, 'polygoncomplete', function(polygon) {
polySelected = true;
coords = new Array();
polygon.getPath().forEach(function (l, index) {
coords[index]="("+l.lat()+","+l.lng()+")"
});
}
With this data, it is impossible to tell if the points actually cross the 180 meridian line. If I took out the 'upper left', 'upper right', you would have no way of discerning if the users second point was to the right (crossing 180) or left (making a very large box).
Is there any way around this? Such as not having 'wrapped' coordinates [50, 182] like that? What is the standard way of dealing with crossing the boundaries and having data representing that?
Thanks!
Edit - This is not a case of closing the polygon and having coordinates snap to a very large area. This case is for a normal polygon manual closure, creating a small square polygon, but one that crosses the map horizontal repeat and having the resultant coordinates be indistinguishable from a polygon that crosses the repeat 180 meridian line, or one that is very wide (but no different vertically) and covering the opposite area. I wish I could demonstrate with pictures to help explain but I do not have this functionality yet.
Answer
I do not see a way to answer my own question, however I have figured it out and want to share. I realized that you cannot draw an edge of a polygon that extends more than 180 degrees longitudinally. Knowing that, if you have two points that have a different of greater than 180, it crosses the +/-180 meridian.
Thanks to all who were offering support!
You can do this:
var longitude = l.lng();
while (longitude > 180)
longitude -= 360; // e.g. 182 and 542 become -178, which is the equivalent longitude
while (longitude < -180)
longitude += 360;
coords[index]="("+l.lat()+","+longitude+")";
As long as your latitude is between 90 and -90. If it isn't you need to fix that too... let me know if you need help with that.
Related
I am creating list of circles on my map using a loop. The circle radius may differ for each loop.
The logic should not overlap two circles. For this I need the following :-
Start with lat lng (circle center) and draw a circle.
Move radius*2 kilometer ahead and get the center point for second circle and so on.
Problem :
I am facing problem in the second step given above. How should I calculate the next point(Circle center) in row ? The input that I have is center point of first circle and radius.
Any suggestions?
The approximate conversions are:
Latitude: 1 deg = 110.574 km
Longitude: 1 deg = 111.320*cos(latitude) km
This doesn't fully correct for the Earth's polar flattening -- for that you'd probably want a more complicated formula using the WGS84 reference ellipsoid (the model used for GPS). But the error is probably negligible for your purposes.
Source: http://en.wikipedia.org/wiki/Latitude
So you can calculate the distance you need to move on the map and the latitude, Longitude for the next point on the map.
However there is a library (I have never used it, nut hope it helps)
http://www.jstott.me.uk/jcoord/
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How to find area of given coordinates?
Requirements:
I have a polygon coordinates on a map then we want to find area of polygon in hectares and acres
Coordinates are in decimal degrees area should be in decimal degrees
we can convert hectares and acres kindly replay me
Coordinates are
(10.072642780669105,76.3268655538559),(10.078051232146612,76.32986962795258]),(10.073530110959743,76.33390367031097)
Code
arr=[ [10.075854059674523, 76.32832467556],
[10.079825860518895, 76.33338868618011],
[10.076234340596953, 76.33806645870209],
[10.07065684212598, 76.33806645870209],
[10.068924417668397, 76.33175790309906] ];
var sum=0;
for(var i=0,l=arr.length-1;i<l;i++){ sum+=(arr[i][0]*arr[i+1][1]-arr[i+1][0]*arr[i][1]); }
alert('The Area of Ploygon is:'+(sum/2));
It's quite complex and is not exactly math.
If you want to get real world result not some math theory you have to notice that earth is not sphere - look for WGS 84 in Google.
One of algorithms that can be implemented quite fast is Montecarlo:
Find the minimum bounding box of polygon (for small areas you can handle earth as flat surface)
Calculate area of MBB as for rectangle S = a*b
Get N random points inside the rectangle
Find M = points inside the polygon
Surface of your polygon is S*M/N
Increasing N gives you higher accuracy.
You should easy find algorithms for resolving each step (finding distance from one geo point to other geo point, checking if point x/y is inside of figure)
The polygon area code compares favorably with another math site where the problem is explained under 2-D polygons.
The problem is you have squared degrees.
Well, for small patches of land the Earth can be considered flat, and for converting squared degrees to hectares or acres or square meters you need to know how long a degree is.
At the Equator, a degree on Earth is 1/360th of the circumference of Earth which is 2*Pi*Rearth or ~40000km.
1 degree is approximately 1/360th of 40000 km = approximately 111 km per degree.
1km is 1000m. 1 km^2 = 10^6 m^2. But 1 hectrare = 10000 m^2, so 1km^2 is 100 hectares.
A squared degree will be approximately (111^2 km^2) (100 hectares/km^2) ~= 1232100 hectares
Ah, but I cheated, this only works near the Equator.
The Earth is a little bit fatter than 40000 km around, and also a degree of longitude becomes shorter as you get near the pole. Degrees of latitude N or S are always 1/360th of the way around the Earth from pole to pole, but degrees of longitude are widest at the equator and become narrower as cos(latitude).
Ideally you need a library to do the conversion for you.
It actually depends on where you are in the world and how precise you want to be. Bear in mind that land is neither flat nor necessarily aligned with the underlying sphereoid.
The company I work for sprays bracken from the air and produces maps of where the spraying occurred plotted on Ordnance survey maps accurate to a metre or two.
In the UK you may find these useful, they detail the complexity of these conversions which I'm not even going to try to summarise here.
http://www.ordnancesurvey.co.uk/business-and-government/help-and-support/navigation-technology/os-net/surveying.html
http://www.ordnancesurvey.co.uk/docs/support/guide-coordinate-systems-great-britain.pdf
It appears the Ordnance Survey don't have the old conversion paper that I learned from on their site but I found a copy here.
http://www.fgg.uni-lj.si/~/mkuhar/Zalozba/TM_projection.pdf
My map uses EPSG:900013 projection. As a result I get values in meters in the range of -20037508.342789244 to 20037508.342789244 when getting my mouse position.
I used the .transform() method of the LonLat class, using EPSG:900913 as the source projection, and (without thinking) used EPSG:4329 as the destination projection.
My question is, why is the EPSG:4329 giving me ranges from -180, 180, -80.05, 85.05 (which i wanted) instead of -180, 180, -90, 90 (which it should have given me, since those are the correct bounds http://spatialreference.org/ref/epsg/wgs-84/)?
I'm relatively sure your source projection (900913) is setting those constraints, so that when you move your mouse, you're limited to travelling so many meters away from 0,0, which corresponds to the 85.05 and -80.05 in your transformations.
Said differently, EPSG 900913 doesn't cover the complete globe. So when you move your mouse to the furthest north/south, respectively, it would transform not to +/- 90, but to 85.05 and -80.05, as you've discovered.
If you go and check this page in the OpenLayers docs, they explain it as follows:
Specifically, most spherical mercator maps use an extent of the world
from -180 to 180 longitude, and from -85.0511 to 85.0511 latitude.
Because the mercator projection stretches to infinity as you approach
the poles, a cutoff in the north-south direction is required, and this
particular cutoff results in a perfect square of projected meters.
Hello everybody o/
I know that this is more a math question than gmap, but I suppose that someone already pass through this =)
In my map, I have circle (actually I have several of them, but this not change the question), like this: http://code.google.com/intl/pt-BR/apis/maps/articles/mvcfun/step6.html
How do I know if a marker (with latitude X and longitude Y) is inside this circle?
Sorry for the bad english, I'm brazillian =p
In Google Maps JavaScript API v3 you can use geometry library. To enable it you have to slightly change the script URL:
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>
The library contains utility functions for the computation of geometric data on sphere. You can utilize it to compute the distance of two points given by their latLngs this way:
var distanceInMetres = google.maps.geometry.spherical.computeDistanceBetween(latLngCircleCenter, latLngPoint);
Now you can easily check if the point is inside the circle (suppose R is in metres):
if(distanceInMetres < R)
alert("in the circle");
If (lat1, lon1) and (lat2, lon2) are your two points and R is the radius of the circle around your first point, then the distance between the points is given by the haversine formula (or the Great-circle distance). But I believe that for your problem, the angles are small enough to use this approximation:
and then check whether d^2 is less than the radius R^2.
But if your latitude and longitude differences are larger than a few degrees, you'll want to use the full haversine formula.
I Recommend you read http://www.movable-type.co.uk/scripts/latlong.html. It provides a number of algorithms for computations of this kind. It includes JavaScript code for the computations.
Basically if you have the coords of circle center (cX,cY) and radius R, and some marker at X,Y you can do the following calculations:
var distanceQuad = (X-cX)*(X-cX)+(Y-cY)*(Y-cY)
if (distanceQuad<=(R*R))
{
alert("Marker inside circle!");
}
This is from trigonometry. You calculate distance as sqrt(sqr(deltaX)+sqr(deltaY)) and compare it with circle Radius. Given code is a bit optimized to get rid of calculating square root.
It's much easier than you'd expect. Read this answer including a working jsFiddle example.
I can do this to convert a lat/lng to pixel coordinates in Google maps:
var xy = map.getProjection().fromLatLngToDivPixel(new google.maps.LatLng(lat, lng));
Now I'm wondering, how do I get a distance in pixels from that point? Say I want to draw a circle with a radius of 5 miles around that. How do I do that?
In v3 there is the circle object
The best way to draw a circle around a given point is to draw a GPolygon with many points, simulating a circle.
You can find a good example of this here. Look for the drawCircle function in the source (you will need to download the attached circle.html to check it out.