yii2-google-maps-library icon indicating copy to clipboard operation
yii2-google-maps-library copied to clipboard

Show rectangles on the map?

Open pedroriverove opened this issue 7 years ago • 7 comments

Provide the correct way to display a RECTANGLES on the map using the extension yii2-google-maps-library, I can not find any reference on the web.

I can not associate php arrays with their relative in javascript.

Please, I need examples to guide me.

pedroriverove avatar Aug 09 '17 18:08 pedroriverove

The only thing you need to do is to create a LatLngBounds which is compound of two LatLng. Then create a Rectangle providing the newly created LatLngBounds instance to its bounds option on its constructor (as an option).

Then add the rectangle to the map exactly as you do with a marker.

tonydspaniard avatar Aug 10 '17 15:08 tonydspaniard

Ok thank you very much for the explanation Could you see my code to see what I'm failing? Thank you very much.

$coord = new LatLng(['lat' => -1.831239, 'lng' => -78.18340599999999]);
$map = new Map([
    'center' => $coord,
    'zoom' => 7,
]);

$waypoints = [
    new LatLngBounds([
        new LatLng(['lat' => -1.743391143288001, 'lng' => -79.98516381249999]),
        new LatLng(['lat' => -1.1672520205625432, 'lng' => -76.98516381249999]),
    ])
];

$rectangleOptions = new RectangleOptions([
    'fillColor' => '#00FF00',
    'strokeColor' => 'red',
    'draggable' => true,
    'editable' => true,
]);

$map->appendScript($rectangleOptions->getJs());

$marker = new Marker([
    'position' => $coord,
    'title' => 'My Home Town',
]);

$marker->attachInfoWindow(
    new InfoWindow([
        'content' => '<p>This is my super cool content</p>'
    ])
);

$rectangle = new Rectangle([
    'paths' => $waypoints
]);

$map->addOverlay($rectangle);

$bikeLayer = new BicyclingLayer(['map' => $map->getName()]);

$map->appendScript($bikeLayer->getJs());

echo $map->display();

pedroriverove avatar Aug 10 '17 16:08 pedroriverove

Rectangle extends from RectangleOptions: https://github.com/2amigos/yii2-google-maps-library/blob/master/overlays/Rectangle.php#L25

$coord = new LatLng(['lat' => -1.831239, 'lng' => -78.18340599999999]);

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

$marker = new Marker([
    'position' => $coord,
    'title' => 'My Home Town',
]);
$marker->attachInfoWindow(
    new InfoWindow([
        'content' => '<p>This is my super cool content</p>'
    ])
);

// add marker
$map->addOverlay($marker); 

// create rectangle
$bounds = new LatLngBounds([
        new LatLng(['lat' => -1.743391143288001, 'lng' => -79.98516381249999]),
        new LatLng(['lat' => -1.1672520205625432, 'lng' => -76.98516381249999]),
    ]);

$rectangle = new Rectangle([
    'bounds' => $bounds, // here my bounds!
     'fillColor' => '#00FF00',
    'strokeColor' => 'red',
    'draggable' => true,
    'editable' => true,
]);

$map->addOverlay($rectangle);

$bikeLayer = new BicyclingLayer(['map' => $map->getName()]);
$map->appendScript($bikeLayer->getJs());

echo $map->display();

Totally untested

tonydspaniard avatar Aug 10 '17 16:08 tonydspaniard

Throw the following error:

Setting unknown property: dosamigos\google\maps\LatLngBounds::0

pedroriverove avatar Aug 10 '17 17:08 pedroriverove

@Pedro25 because you need to set the actual property:

$bounds = new LatLngBounds([
        'northEast' => new LatLng(['lat' => -1.743391143288001, 'lng' => -79.98516381249999]),
        'southWest' => new LatLng(['lat' => -1.1672520205625432, 'lng' => -76.98516381249999]),
    ]);

Please, review how the objects get instantiated... Unfortunately I cannot right now "debug" nor "test" even the code I write as sample. Apologies.

tonydspaniard avatar Aug 10 '17 17:08 tonydspaniard

Greetings from Venezuela and thank you very much for the collaboration.

pedroriverove avatar Aug 10 '17 18:08 pedroriverove

Saludos y fuerza @Pedro25

tonydspaniard avatar Aug 10 '17 19:08 tonydspaniard