'google maps api convert polygon to circle

I have a geoJson for a polygon which represents a circle and I need to draw a circle from the polygon geometry.

I get the geoJson from a DB and I can draw the polygon but if the geoJson was made from a circle I need to draw a circle instead of a polygon.

Update: I found a way to compute the radius, now I need just a way to compute the coordinate for center.

var geometry = JSON.parse(retZones["GeoJSON"]);
var data = {
    type: "Feature",
    geometry
};

var geoJsonLayer = new google.maps.Data();
var newFeature = geoJsonLayer.addGeoJson(data);
var newGeometry = newFeature[0].getGeometry();

// If the geoJson represents a polygon (I get this info from DB) I draw the polygon
if (retZones["TypeGeometry"] == "Polygon") {
    var shape = [];
    for (var i = 0; i < newGeometry.getLength(); i++) {
        var shapeData = newGeometry.getAt(i).getArray();
        shape.push(shapeData);
    }

    var newShape = new google.maps.Polygon({
        paths: shape,
        fillColor: '#ffff00',
        fillOpacity: 0.4,
        strokeWeight: 2,
        clickable: true,
        zIndex: 1,
        editable: false
    });
}
// If the geoJson represents a circle I have to draw a circle
else if (retZones["TypeGeometry"] == "Circle") {
    var p1 = { lat: newGeometry.getAt(0).getArray()[0].lat(), lng: newGeometry.getAt(0).getArray()[0].lng() };
    var p2 = { lat: newGeometry.getAt(0).getArray()[180].lat(), lng: newGeometry.getAt(0).getArray()[180].lng() };
    var c = ; // How to compute the center?
    var r = google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 2;

    var newShape = google.maps.Circle({
        center: c,
        radius: r,
        fillColor: '#ffff00',
        fillOpacity: 0.4,
        strokeWeight: 2,
        clickable: true,
        zIndex: 1,
        editable: false
    });
}


Solution 1:[1]

I found out this solution and it seems to work. If someone have a better way please let me know.

var ns = newGeometry.getAt(0).getArray().length;
var N = 0;
var E = Math.round(ns / 4);
var S = E * 2;
var W = E * 3;
var p1 = { lat: newGeometry.getAt(0).getArray()[N].lat(), lng: newGeometry.getAt(0).getArray()[N].lng() };
var p2 = { lat: newGeometry.getAt(0).getArray()[S].lat(), lng: newGeometry.getAt(0).getArray()[S].lng() };
var p3 = { lat: newGeometry.getAt(0).getArray()[E].lat(), lng: newGeometry.getAt(0).getArray()[E].lng() };
var p4 = { lat: newGeometry.getAt(0).getArray()[W].lat(), lng: newGeometry.getAt(0).getArray()[W].lng() };
var c = { lat: p2.lat - ((p2.lat - p1.lat) / 2) , lng: p4.lng - ((p4.lng - p3.lng) / 2) };
var r = google.maps.geometry.spherical.computeDistanceBetween(p1, p2) / 2;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1