Easty
Easty

Reputation: 387

Getting a central point from an esri multipoint with javascript

I have created a webpage displaying markers on an ersi map using javasvipt.

Data: 
MapNorth    MapEast
439624  504743
439622  504736
439722  504775
439738  504739
439715  504774
439734  504739

The javascript code:

var points = data.map(function(x){
                    return [x.MapEast, x.MapNorth];
                });

                var myMultiPoint = {"geometry":{"points":points,"spatialReference":27700},"symbol":{"color":[255,255,255,64],
                "size":6,"angle":0,"xoffset":0,"yoffset":0,"type":"esriSMS","style":"esriSMSCircle",
                "outline":{"color":[0,0,0,255],"width":6,"type":"esriSLS","style":"esriSLSSolid"}}};  

                var gra = new esri.Graphic(myMultiPoint);

                myMap.graphics.add(gra);

                var graExtent = esri.graphicsExtent(myMap.graphics.graphics); 
                myMap.setExtent(graExtent); 

What the above code does is plot markers on the map and then zooms into the extent. What my employers want now is for me to find the central point of all of those points and display one marker in the center.

Can this be done? If so and you tell me how?

Thanks Paul

Upvotes: 3

Views: 1693

Answers (3)

Zev
Zev

Reputation: 1

One thing to point out to those trying to using .getCentriod() , make sure your polygon is closed. Your 1st point and Last Point need to be in the same spot. Otherwise it wont work right. ( I ran into this a year ago, not sure if they changed this)

Upvotes: 0

Clayton Donahue
Clayton Donahue

Reputation: 151

I think what you want to be doing here is instead of creating a Multipoint, create a Polygon from your array of points. Once you have a polygon defined, you can do something like

var myPolygon = new Polygon(points);
var centroid = myPolygon.getCentroid();

This should get you the centroid of the points making up the Polygon.

https://developers.arcgis.com/javascript/jsapi/polygon-amd.html

Note that this requires at least version 3.7 of the JS API, though.

Upvotes: 0

Dave Briand
Dave Briand

Reputation: 1734

Couple of things.

  1. Did you know about gis.stackexchange.com? They might better solve your problem.
  2. What you're trying to do is find the centre of a polygon assuming those points aren't all in a line.
  3. Here's a link with an answer to the question I think you're asking https://gis.stackexchange.com/questions/7998/how-can-i-calculate-the-center-point-inside-a-polygon-in-arcgis-9-3

The solution posted there uses getExtent().getCenter() as seen here

var myPolygonCenterLatLon = myPolygon.getExtent().getCenter();

Upvotes: 1

Related Questions