Skip to content

Commit

Permalink
Added Custom InfoWindow and Icon on Google Maps (#24)
Browse files Browse the repository at this point in the history
* Added Custom InfoWindow and Icon on Google Maps

* Added Unit Test for Icon and InfoWindow on Google Maps

* leaflet + tests

Co-authored-by: kikojover
Co-authored-by: Lars Wiegers <larswiegers@live.nl>
  • Loading branch information
kikojover and LarsWiegers committed Mar 10, 2022
1 parent 6c78a80 commit 1826c27
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
23 changes: 21 additions & 2 deletions resources/views/components/google.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,34 @@ function initMap{{$mapId}}() {
zoom: {{$zoomLevel}},
});
function addInfoWindow(marker, message) {
var infoWindow = new google.maps.InfoWindow({
content: message
});
google.maps.event.addListener(marker, 'click', function () {
infoWindow.open(map{{$mapId}}, marker);
});
}
@foreach($markers as $marker)
new google.maps.Marker({
var marker{{ $loop->iteration }} = new google.maps.Marker({
position: {
lat: {{$marker['lat'] ?? $marker[0]}},
lng: {{$marker['long'] ?? $marker[1]}}
},
map: map{{$mapId}},
title: "{{ $marker['title'] ?? 'Hello World!' }}",
@if(isset($marker['title']))
title: "{{ $marker['title'] }}",
@endif
icon: @if(isset($marker['icon']))"{{ $marker['icon']}}" @else null @endif
});
@if(isset($marker['info']))
addInfoWindow(marker{{ $loop->iteration }}, @json($marker['info']));
@endif
@endforeach
}
</script>
6 changes: 5 additions & 1 deletion resources/views/components/leaflet.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ class='{{ $attributes["class"] }}'
iconSize: [{{$marker['iconSizeX'] ?? 32}} , {{ $marker['iconSizeY'] ?? 32 }}],
});
@endif
var marker = L.marker([{{$marker['lat'] ?? $marker[0]}}, {{$marker['long'] ?? $marker[1]}}]).addTo(mymap);
var marker = L.marker([{{$marker['lat'] ?? $marker[0]}}, {{$marker['long'] ?? $marker[1]}}]);
@if(isset($marker['info']))
marker.bindPopup(@json($marker['info']));
@endif
marker.addTo(mymap);
@endforeach
@if($tileHost === 'mapbox')
Expand Down
12 changes: 12 additions & 0 deletions tests/Unit/GoogleMapsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ public function test_it_can_take_classes_as_attribute()
$content = $this->getComponentRenderedContent("<x-maps-google class='h-16'></x-maps-google>");
$this->assertStringContainsString("class='h-16'", $content);
}

public function test_it_can_take_custom_icon_on_marker()
{
$content = $this->getComponentRenderedContent("<x-maps-google :markers=\"[['lat' => 38.716450, 'long' => 0.055684, 'icon' => 'icon.png']]\"></x-maps-google>");
$this->assertStringContainsString('icon: "icon.png"', $content);
}

public function test_it_can_take_custom_infowindow_on_marker()
{
$content = $this->getComponentRenderedContent("<x-maps-google :markers=\"[['lat' => 38.716450, 'long' => 0.055684, 'info' => 'MarkerInfo']]\"></x-maps-google>");
$this->assertStringContainsString('addInfoWindow(marker1, "MarkerInfo");', $content);
}
}
6 changes: 6 additions & 0 deletions tests/Unit/LeafletTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ public function test_it_can_take_classes_as_attribute()
$content = $this->getComponentRenderedContent("<x-maps-leaflet class='h-16'></x-maps-leaflet>");
$this->assertStringContainsString("class='h-16'", $content);
}

public function test_it_can_take_custom_infowindow_on_marker()
{
$content = $this->getComponentRenderedContent("<x-maps-leaflet :markers=\"[['lat' => 38.716450, 'long' => 0.055684, 'info' => 'MarkerInfo']]\"></x-maps-leaflet>");
$this->assertStringContainsString('marker.bindPopup("MarkerInfo");', $content);
}
}

0 comments on commit 1826c27

Please sign in to comment.