2amigos / yii2-google-maps-library

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

Show rectangles on the map? #83

Open pedroriverove opened 7 years ago

pedroriverove commented 7 years ago

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.

tonydspaniard commented 7 years ago

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.

pedroriverove commented 7 years ago

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();
tonydspaniard commented 7 years ago

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

pedroriverove commented 7 years ago

Throw the following error:

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

tonydspaniard commented 7 years ago

@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.

pedroriverove commented 7 years ago

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

tonydspaniard commented 7 years ago

Saludos y fuerza @Pedro25