2amigos / yii2-google-maps-library

Google Maps API library for Yii2
https://2amigos.us
BSD 3-Clause "New" or "Revised" License
106 stars 79 forks source link

Handle Polygon to google from Postgis. #110

Open maurocrispin21 opened 6 years ago

maurocrispin21 commented 6 years ago

I need to render polygons on google maps using this library. I store my cordinateds as geomtry data type on postgis postgres [https://postgis.net/docs/geometry.html]. So my coordinates are not a human readable latlong array. Geometry data type cannot be handle due this library recieves arrays to define polygons

`$coords = [ new LatLng(['lat' => 25.774252, 'lng' => -80.190262]), new LatLng(['lat' => 18.466465, 'lng' => -66.118292]), new LatLng(['lat' => 32.321384, 'lng' => -64.75737]), ];

    $polygon = new Polygon([
      'paths' => $coords
    ]);`

Geometry data type has different data type outputs https://postgis.net/docs/reference.html#Geometry_Outputs. One of that outputs is GeoJSON https://postgis.net/docs/ST_AsGeoJSON.html. That actually google maps api can manage according with google's api doc https://developers.google.com/maps/documentation/javascript/datalayer#load_geojson

How can I handle this type of data to yii2-google-maps-library???

maurocrispin21 commented 6 years ago

I could not find a proper way to do it. So I dumped geom into points then I used ST_X ST_Y to get points' lat/lon

namgee commented 3 years ago

how do you do this? please help me

I solved a similar issue when generating polylines.

Model:

public function getLine($rp_zone)
    {
        return Map::findBySQL("
            SELECT 
                ST_Y((dp).geom) AS lat,
                ST_X((dp).geom) AS lng,
                ST_Y(centre) AS centre_lat,
                ST_X(centre) AS centre_lng
            FROM (
                SELECT ST_DumpPoints(geom) AS dp,
                ST_Centroid(geom) AS centre
                FROM traffic.map
                WHERE rp_zone = :rp_zone
            )x
        ",[':rp_zone' => $rp_zone])->asArray()->all();
    }

Controller:

public function actionView($id)
    {
        return $this->render('view', [
            'model' => $model = $this->findModel($id),
        ]);
    }

View:

    $coord = new LatLng([
        'lat' => $model->map->getLine($model->zone_id)[0]['centre_lat'],
        'lng' => $model->map->getLine($model->zone_id)[0]['centre_lng']
        ]);

    $map = new Map([
        'center' => $coord,
        'zoom' => 20,
    ]);

    foreach ($model->map->getLine($model->zone_id) as $coord) {
        $coords[] = new LatLng(['lat' => $coord['lat'], 'lng' => $coord['lng']]);
    }

    $polyline = new Polyline([
        'path' => $coords,
    ]);