Example #1
0
$waypoints = [new DirectionsWayPoint(['location' => $santo_domingo])];
$directionsRequest = new DirectionsRequest(['origin' => $home, 'destination' => $school, 'waypoints' => $waypoints, 'travelMode' => TravelMode::DRIVING]);
// Lets configure the polyline that renders the direction
$polylineOptions = new PolylineOptions(['strokeColor' => '#FFAA00', 'draggable' => true]);
// Now the renderer
$directionsRenderer = new DirectionsRenderer(['map' => $map->getName(), 'polylineOptions' => $polylineOptions]);
// Finally the directions service
$directionsService = new DirectionsService(['directionsRenderer' => $directionsRenderer, 'directionsRequest' => $directionsRequest]);
// Thats it, append the resulting script to the map
$map->appendScript($directionsService->getJs());
// Lets add a marker now
$marker = new Marker(['position' => $coord, 'title' => 'My Home Town']);
// Provide a shared InfoWindow to the marker
$marker->attachInfoWindow(new InfoWindow(['content' => '<p>ทดสอบ</p>']));
// Add marker to the map
$map->addOverlay($marker);
// Now lets write a polygon
$coords = [new LatLng(['lat' => 25.774252, 'lng' => -80.190262]), new LatLng(['lat' => 18.466465, 'lng' => -66.118292]), new LatLng(['lat' => 32.321384, 'lng' => -64.75736999999999]), new LatLng(['lat' => 25.774252, 'lng' => -80.190262])];
$polygon = new Polygon(['paths' => $coords]);
// Add a shared info window
$polygon->attachInfoWindow(new InfoWindow(['content' => '<p>This is my super cool Polygon</p>']));
// Add it now to the map
$map->addOverlay($polygon);
// Lets show the BicyclingLayer :)
$bikeLayer = new BicyclingLayer(['map' => $map->getName()]);
// Append its resulting script
$map->appendScript($bikeLayer->getJs());
$map->height = 450;
$map->width = 800;
// Display the map -finally :)
echo $map->display();
 /**
  * Returns the boundaries of an array of Polygon objects
  * @param Polygon[] $polygons array of Polygons
  * @param float $margin margin factor for the bounds
  * @return LatLngBounds
  * @throws \yii\base\InvalidParamException
  */
 public static function getBoundsOfPolygons($polygons, $margin = 0.0)
 {
     $coords = [];
     /** @var Polygon $polygon */
     foreach ($polygons as $polygon) {
         if (!$polygon instanceof Polygon) {
             throw new InvalidParamException('"$polygons" must be an array of "' . Polygon::className() . '" objects');
         }
         // merge LatLng arrays
         $coords = ArrayHelper::merge($coords, $polygon->paths);
     }
     return LatLngBounds::getBoundsOfCoordinates($coords, $margin);
 }