使用yii2-google-map-library从数据库显示多边形

时间:2015-04-17 02:26:11

标签: php google-maps-api-3 yii2-advanced-app


当我尝试使用yii2-google-maps-library从数据库显示多边形时,我遇到了问题。我使用字段tb_wilayah来表(id, label, lat, lng),然后尝试使用此代码在地图上显示多边形:

$data = AreaWilayah::find()->all();
    $paths = [];
    foreach ($data as $value){
        $koord = new LatLng([
            'lat' => $value->lat,
            'lng' => $value->lng
        ]);
        array_push($paths, $koord);
    }
    $coords = [$paths];
    $polygon = new Polygon([
        'paths' => $coords
    ]);
    $map->addOverlay($polygon);

不幸的是,我发送了错误消息Argument 1 passed to dosamigos\google\maps\overlays\PolygonOptions::addCoord() must be an instance of dosamigos\google\maps\LatLng, array given, called in C:\xampp\htdocs\project\vendor\2amigos\yii2-google-maps-library\overlays\PolygonOptions.php on line 120 and defined

有人能帮助我吗?或者,如果有人可以提供教程,那将是一个真正的帮助。

1 个答案:

答案 0 :(得分:0)

上述代码的

Thx。那为我指明了正确的方向。这对我有用:

foreach ($ismalls as $mall) {
    $poly_json = json_decode($mall->polygonpoints, true);
    $coords = [];
    //echo print_r($poly_json);
    foreach ($poly_json as $points) {
        $koord = new LatLng([
            'lat' => $points[1],
            'lng' => $points[0]
        ]);
        array_push($coords, $koord);
    }
   $polygon = new Polygon([
        'paths' => $coords
    ]);

    // Add a shared info window
    $polygon->attachInfoWindow(new InfoWindow([
        'content' => '<p>'.$mall->display_name.'</p>'
        ]));

    // Add it now to the map
    $map->addOverlay($polygon);
}