title: Calculate the center of latitude and longitude coordinates
body: <p>I'm looking for a <strong>elegant solution</strong> that calculates the <code>center</code> between several <a href="/questions/tagged/latitude-longitude" class="post-tag" title="show questions tagged &#39;latitude-longitude&#39;" rel="tag">latitude-longitude</a> coordinates (for example, to simply center a map to the center of a <a href="/questions/tagged/google-maps" class="post-tag" title="show questions tagged &#39;google-maps&#39;" rel="tag"><img src="https://i.stack.imgur.com/uE37r.png" height="16" width="18" alt="" class="sponsor-tag-img">google-maps</a> polygon).</p>
  
  <p>Table: <code>locations</code>:</p>
  
  <pre><code>id |    city    |   latitude   |  longitude
  -----------------------------------------------
  1  |   Berlin   |   52.524268  |   13.406290
  -----------------------------------------------
  2  |   London   |   51.508129  |  -0.1280050    
  -----------------------------------------------
  3  |   Hamburg  |   53.551084  |   9.9936817
  -----------------------------------------------
  4  |  Amsterdam |   52.370215  |   4.8951678
  -----------------------------------------------
  </code></pre>
  
  <p>The current calculation:</p>
  
  <pre><code>function calculateCenter($array_locations) {
  
      $minlat = false;
      $minlng = false;
      $maxlat = false;
      $maxlng = false;
  
      foreach ($array_locations as $geolocation) {
  
           if ($minlat === false) { $minlat = $geolocation['lat']; } else { $minlat = ($geolocation['lat'] &lt; $minlat) ? $geolocation['lat'] : $minlat; }
           if ($maxlat === false) { $maxlat = $geolocation['lat']; } else { $maxlat = ($geolocation['lat'] &gt; $maxlat) ? $geolocation['lat'] : $maxlat; }
           if ($minlng === false) { $minlng = $geolocation['lon']; } else { $minlng = ($geolocation['lon'] &lt; $minlng) ? $geolocation['lon'] : $minlng; }
           if ($maxlng === false) { $maxlng = $geolocation['lon']; } else { $maxlng = ($geolocation['lon'] &gt; $maxlng) ? $geolocation['lon'] : $maxlng; }
      }
  
      // Calculate the center
      $lat = $maxlat - (($maxlat - $minlat) / 2);
      $lon = $maxlng - (($maxlng - $minlng) / 2);
  
      return array($lat, $lon);
  }
  </code></pre>