Skip to content

Commit

Permalink
Add infowindow for polylines with midpoint calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
barhom committed Apr 19, 2022
1 parent 2cbac9c commit 065228d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
15 changes: 15 additions & 0 deletions resources/views/polyline.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,21 @@
'polyline_{!! $id !!}': polyline_{!! $id !!}
});

google.maps.event.addListener(polyline_{!! $id !!}, 'click', function(event) {

@if (isset($options['autoClose']) && $options['autoClose'])

for (var i = 0; i < infowindows.length; i++) {
infowindows[i].close();
}

@endif

infowindow_{!! $id+3 !!}.open({!! $options['map'] !!}, marker_{!! $id+3 !!});
infowindow_{!! $id+3 !!}.setPosition(event.latLng);

});

@foreach (['eventClick', 'eventDblClick', 'eventRightClick', 'eventMouseOver', 'eventMouseDown', 'eventMouseUp', 'eventMouseOut', 'eventDrag', 'eventDragStart', 'eventDragEnd', 'eventDomReady'] as $event)

@if (isset($options[$event]))
Expand Down
2 changes: 1 addition & 1 deletion src/Facades/MapperFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @method static Mapper streetview($latitude, $longitude, $heading, $pitch, array $options = [])
* @method static Mapper marker($latitude, $longitude, array $options = [])
* @method static Mapper informationWindow($latitude, $longitude, $content = '', array $options = [])
* @method static Mapper polyline(array $coordinates = [], array $options = [])
* @method static Mapper polyline(array $coordinates = [], $content = '', array $options = [])
* @method static Mapper polygon(array $coordinates = [], array $options = [])
* @method static Mapper rectangle(array $coordinates = [], array $options = [])
* @method static Mapper circle(array $coordinates = [], array $options = [])
Expand Down
37 changes: 36 additions & 1 deletion src/Mapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public function informationWindow($latitude, $longitude, $content = '', array $o
*
* @return self
*/
public function polyline(array $coordinates = [], array $options = [])
public function polyline(array $coordinates = [], $content = '', array $options = [])
{
$items = $this->getItems();

Expand All @@ -360,9 +360,44 @@ public function polyline(array $coordinates = [], array $options = [])

$item->shape('polyline', $coordinates, $options);

// Info window for a polyline if content is not empty
if(!empty($content)) {
$parameters = $this->getOptions();
$options = array_replace_recursive(
['markers' => $parameters['markers']],
$item->getOptions()['markers'],
$options,
($content !== '' ? ['markers' => ['content' => $content]] : [])
);
$options['markers']['visible'] = false;
$coordinates = $this->midpoint($coordinates[0]['latitude'], $coordinates[0]['longitude'], $coordinates[1]['latitude'], $coordinates[1]['longitude']);
$latitude = $coordinates[0];
$longitude = $coordinates[1];
$item->marker($latitude, $longitude, $options);
}

return $this;
}

public function midpoint ($lat1, $lng1, $lat2, $lng2) {

$lat1= deg2rad($lat1);
$lng1= deg2rad($lng1);
$lat2= deg2rad($lat2);
$lng2= deg2rad($lng2);

$dlng = $lng2 - $lng1;
$Bx = cos($lat2) * cos($dlng);
$By = cos($lat2) * sin($dlng);
$lat3 = atan2( sin($lat1)+sin($lat2),
sqrt((cos($lat1)+$Bx)*(cos($lat1)+$Bx) + $By*$By ));
$lng3 = $lng1 + atan2($By, (cos($lat1) + $Bx));
$pi = pi();
$coords[] = ($lat3*180)/$pi;
$coords[] = ($lng3*180)/$pi;
return $coords;
}

/**
* Add a new map polygon.
*
Expand Down

0 comments on commit 065228d

Please sign in to comment.