Click here to Skip to main content
12,767,637 members (52,156 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# Javascript SQL-Server , +
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 3:10am
Rate this: bad
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:[^].
You can also calculate a vector mean, by calculating a mean for longitude and latitude values respectively.
Wonde Tadesse 13-Jun-12 22:45pm
Zoltán Zörgő 14-Jun-12 2:21am
Thank you.
Rate this: bad
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 getCenterOfPolygon(polygon){
	var PI=22/7
	var X=0;
	var Y=0;
	var Z=0;
	polygon.getPath().forEach(function (vertex, inex) {;
		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);

var pt=getCenterOfPolygon(mapPolygon);
kimberly ping 24-Jan-15 0:06am
What is this algorithm called?
Rate this: bad
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[^].

Also this[^] might help you to generate source code for your formulas.

Cheers and best of luck!

Rushikesh Joshi
Wonde Tadesse 13-Jun-12 22:45pm
Joshi, Rushikesh 13-Jun-12 23:50pm
Thanks Wonde :)
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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170217.1 | Last Updated 14 Jun 2012
Copyright © CodeProject, 1999-2017
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