angulargoogle-maps-api-3cartodb

Is there any way to center a map (Google Map) based where's located a layer from carto


I'm getting a layer from carto using carto.js v4 and showing it on a map (Google Map) and I wanna center the map where is the layer located


Solution

  • You need to get that info using SQL API and PostGIS functions, in general the easy approach is to center the map in the bounding box of your data so that would be sending this query

    select st_asgeojson(
               st_boundingdiagonal(
                   st_collect(
                       the_geom)))
      from populated_places
    

    That query will give you the coordinates of the corners of the bounding box as a GeoJSON object, very easy to parse and translate into a Google Maps API map change.