Click here to Skip to main content
11,704,315 members (49,992 online)
Rate this: bad
good
Please Sign up or sign in to vote.
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 13-Jun-12 2:10am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
v2
Comments
Wonde Tadesse at 13-Jun-12 22:45pm
   
5+
Zoltán Zörgő at 14-Jun-12 2:21am
   
Thank you.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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:
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:
var pt=getCenterOfPolygon(mapPolygon);
alert(pt.lat()+","+pt.lng());
  Permalink  
v2
Comments
kimberly ping at 24-Jan-15 0:06am
   
What is this algorithm called?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v2
Comments
Wonde Tadesse at 13-Jun-12 22:45pm
   
5+
Joshi, Rushikesh at 13-Jun-12 23:50pm
   
Thanks Wonde :)
Pardeep singh from Chandigarh at 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)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 740
1 OriginalGriff 520
2 Maciej Los 355
3 Andy Lanng 284
4 CPallini 275
0 OriginalGriff 8,824
1 Sergey Alexandrovich Kryukov 8,247
2 CPallini 5,197
3 Maciej Los 4,726
4 Mika Wendelius 3,606


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 14 Jun 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100