yii2-google-maps-library
yii2-google-maps-library copied to clipboard
Handle Polygon to google from Postgis.
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???
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
how do you do this? please help me
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,
]);