-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ajoute une recherche de lieu à la carte
- Loading branch information
1 parent
f3b46ca
commit 9e5dac4
Showing
12 changed files
with
223 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ import './auto_form'; | |
import './modal_trigger'; | ||
import './map'; | ||
import './map_form'; | ||
import './map_search_form'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// @ts-check | ||
|
||
import { getAttributeOrError, querySelectorOrError } from "./util"; | ||
import { MapElement } from './map'; | ||
|
||
customElements.define('d-map-search-form', class extends HTMLElement { | ||
connectedCallback() { | ||
requestAnimationFrame(() => { | ||
/** @type {MapElement} */ | ||
const map = querySelectorOrError(document, `#${getAttributeOrError(this, 'target')}`); | ||
|
||
/** @type {HTMLInputElement} */ | ||
const selectedValueField = querySelectorOrError(document, `#${getAttributeOrError(this, 'selectedValueFieldName')}`); | ||
|
||
selectedValueField.addEventListener('change', () => { | ||
const { coordinates, kind } = JSON.parse(selectedValueField.value); | ||
|
||
// Zoom closer on streets | ||
const zoom = kind === 'street' ? 15 : 12; | ||
|
||
map.flyTo(coordinates, zoom); | ||
}); | ||
}); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Application; | ||
|
||
interface MapGeocoderInterface | ||
{ | ||
public function findPlaces(string $search): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Infrastructure\Adapter; | ||
|
||
use App\Application\MapGeocoderInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
final class IgnMapGeocoder implements MapGeocoderInterface | ||
{ | ||
public function __construct( | ||
private HttpClientInterface $ignGeocoderClient, | ||
) { | ||
} | ||
|
||
public function findPlaces(string $search): array | ||
{ | ||
$response = $this->ignGeocoderClient->request( | ||
'GET', | ||
'/geocodage/completion', | ||
[ | ||
'query' => [ | ||
'text' => $search, | ||
'type' => 'StreetAddress, PositionOfInterest', | ||
'poiType' => 'administratif', | ||
], | ||
], | ||
); | ||
|
||
$data = json_decode($response->getContent(), true); | ||
|
||
$places = []; | ||
|
||
foreach ($data['results'] as $result) { | ||
$places[] = [ | ||
'label' => $result['fulltext'], | ||
'value' => [ | ||
'coordinates' => [$result['x'], $result['y']], | ||
'kind' => $result['kind'], | ||
], | ||
]; | ||
} | ||
|
||
return $places; | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/Infrastructure/Controller/Map/Fragments/MapSearchController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Infrastructure\Controller\Map\Fragments; | ||
|
||
use App\Application\MapGeocoderInterface; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
use Symfony\Component\Routing\Annotation\Route; | ||
|
||
final class MapSearchController | ||
{ | ||
public function __construct( | ||
private \Twig\Environment $twig, | ||
private MapGeocoderInterface $mapGeocoder, | ||
) { | ||
} | ||
|
||
#[Route( | ||
'/carte/search', | ||
name: 'fragment_carto_search', | ||
methods: ['GET'], | ||
)] | ||
public function __invoke(Request $request): Response | ||
{ | ||
$search = $request->query->get('search'); | ||
|
||
if (!$search) { | ||
throw new BadRequestHttpException(); | ||
} | ||
|
||
$results = $this->mapGeocoder->findPlaces($search); | ||
|
||
return new Response( | ||
$this->twig->render( | ||
name: 'map/fragments/search_results.html.twig', | ||
context: [ | ||
'results' => $results, | ||
], | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<div | ||
class="fr-x-autocomplete-wrapper" | ||
data-controller="autocomplete" | ||
data-autocomplete-url-value="{{ path('fragment_carto_search') }}" | ||
data-autocomplete-query-param-value="search" | ||
data-autocomplete-min-length-value="3" | ||
data-autocomplete-delay-value="500" | ||
data-autocomplete-loading-status-value="{{ 'common.autocomplete.status.loading'|trans }}" | ||
data-autocomplete-empty-status-value="{{ 'common.autocomplete.status.min_chars'|trans({ '%minChars%': 3 }) }}" | ||
data-action="autocomplete.change->reset#reset" | ||
> | ||
<div class="fr-search-bar"> | ||
<label for="map_search" class="app-sr-only"> | ||
{{ 'map.search_form.search'|trans }} | ||
</label> | ||
|
||
<input | ||
id="{{ selectedValueFieldName }}" | ||
name="{{ selectedValueFieldName }}" | ||
type="hidden" | ||
data-autocomplete-target="hidden" | ||
> | ||
|
||
<input | ||
id="search" | ||
name="search" | ||
type="text" | ||
class="fr-input" | ||
spellcheck="false" | ||
autocomplete="false" | ||
data-autocomplete-target="input" | ||
placeholder="{{ 'map.search_form.search.placeholder'|trans }}" | ||
> | ||
|
||
<button class="fr-btn" aria-label="{{ 'common.form.search'|trans }}">{{ 'common.form.search'|trans }}</button> | ||
</div> | ||
|
||
<ul | ||
id="map_search-results" | ||
role="listbox" | ||
aria-label="{{ 'map.search_form.results_label'|trans }}" | ||
class="fr-x-autocomplete" | ||
data-autocomplete-target="results" | ||
> | ||
<li role="status" data-autocomplete-target="status"></li> | ||
</ul> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{% for result in results %} | ||
<li role="option" data-autocomplete-value="{{ result.value|json_encode }}">{{ result.label }}</li> | ||
{% endfor %} | ||
|
||
<template id="status"> | ||
{{ 'common.autocomplete.results_count'|trans({ '%count%': results|length }) }} | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters