Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more: , +
hi to all
Please help me, i want to find the middle lattitude and longitude of a polygon in gmap using sql server or C# or javascripts please help

thanks in advance
Posted

First of all define the "middle point" in terms of mathematical concepts.
If you mean the center of a polygon:
Regular Polygon case:
In the case of regular polygons the center is the point that is equidistant from each vertex or corner. It is also the center of the polygon's incircle and circumcircle.
Irregular Polygon case
Irregular polygons are not considered as having a center, since there is unlikely to be any one point equally distant form each vertex. However, an irregular polygon can have a centroid, or center of gravity.
If you mean the centroid of a polygon see this article: http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/[^].
You can also calculate a vector mean, by calculating a mean for longitude and latitude values respectively.
 
Share this answer
 
v2
Comments
Wonde Tadesse 13-Jun-12 22:45pm    
5+
Zoltán Zörgő 14-Jun-12 2:21am    
Thank you.
this is a JavaScript function who get a Polygon and return a Point. you can use this function to calculate center of a Polygon.

Function:
JavaScript
function getCenterOfPolygon(polygon){
	var PI=22/7
	var X=0;
	var Y=0;
	var Z=0;
	polygon.getPath().forEach(function (vertex, inex) {
		lat1=vertex.lat();
		lon1=vertex.lng();
		lat1 = lat1 * PI/180
		lon1 = lon1 * PI/180
		X += Math.cos(lat1) * Math.cos(lon1)
		Y += Math.cos(lat1) * Math.sin(lon1)
		Z += Math.sin(lat1)
	})
	Lon = Math.atan2(Y, X)
	Hyp = Math.sqrt(X * X + Y * Y)
	Lat = Math.atan2(Z, Hyp)
	Lat = Lat * 180/PI
	Lon = Lon * 180/PI 
	return new google.maps.LatLng(Lat,Lon);
}


Use:
JavaScript
var pt=getCenterOfPolygon(mapPolygon);
alert(pt.lat()+","+pt.lng());
 
Share this answer
 
v2
Comments
kimberly ping 24-Jan-15 0:06am    
What is this algorithm called?
Solution 1 is well detailed and thank you Zoltan

Below is simplified version

Consider polygon (Pentagon) with co-ordinates (x1,y1); (x2,y2); (x3,y3); (x4,y4); (x5,y5) then centroid should be

Centroid X coord = (x1+x2+x3+x4+x5) / 5
Centroid Y coord = (y1+y2+y3+y4+y5) / 5

You can use X as latitude and Y as longitude and can point Centroid.

If you are using any mapping tool/library then that library itself should have this functionality to create a polygon with centroid.

If you are creating any functionality on a DigitalMap then attached formulas might help you in future http://williams.best.vwh.net/avform.htm[^].


Also this http://www.geog.ubc.ca/courses/klink/gis.notes/ncgia/u33.html#SEC33.4.1[^] might help you to generate source code for your formulas.

Cheers and best of luck!

Thanks
Rushikesh Joshi
 
Share this answer
 
v2
Comments
Wonde Tadesse 13-Jun-12 22:45pm    
5+
Joshi, Rushikesh 13-Jun-12 23:50pm    
Thanks Wonde :)
[no name] 14-Jun-12 3:21am    
thanx for give me solution

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900