-
Notifications
You must be signed in to change notification settings - Fork 2
OpenStreetMap
Pair\Helpers\OpenStreetMap is the browser-side entrypoint for lightweight OpenStreetMap rendering in Pair views.
Use it when you want Pair to:
- load
/assets/pair.css - load
/assets/PairOpenStreetMap.js - render a dependency-free marker map from server-provided latitude and longitude points
- avoid a browser API key when you only need map tiles and markers
- keep OpenStreetMap markup and asset loading consistent across views
This helper does not perform geocoding or address autocomplete. Store or calculate coordinates separately, then pass them to the browser-side map payload.
Registers Pair's bundled OpenStreetMap stylesheet and JavaScript asset.
Parameters:
-
$assetsPathpoints to the public directory that containspair.cssandPairOpenStreetMap.js
Important runtime behavior:
- Pair registers
pair.cssbecause the map canvas, tile layer, marker layer, status panel, and attribution panel rely on bundled styles - Pair registers
PairOpenStreetMap.jswithdefer - if
$assetsPathis passed asassets,/assets, or another slash-variant, Pair normalizes it - no Google key or remote loader is required
- by default, tiles are loaded from
https://tile.openstreetmap.org/{z}/{x}/{y}.png
use Pair\Helpers\OpenStreetMap;
OpenStreetMap::load();
$points = [
[
'marker' => 1,
'title' => 'Main office',
'location' => 'Milan',
'latitude' => 45.4642,
'longitude' => 9.1900,
'url' => '/offices/1',
],
];<?php
$pointsPayload = json_encode(
['points' => $points],
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT
);
?>
<div class="pair-osm-map" data-pair-osm-map>
<div class="pair-osm-map__canvas" data-pair-osm-canvas>
<div class="pair-osm-map__tiles" data-pair-osm-tiles></div>
<div class="pair-osm-map__markers" data-pair-osm-markers></div>
<div class="pair-osm-map__status" data-pair-osm-status hidden></div>
<div class="pair-osm-map__attribution">
© <a href="https://www.openstreetmap.org/copyright" rel="noopener" target="_blank">OpenStreetMap</a> contributors
</div>
</div>
<script type="application/json" data-pair-osm-points><?= $pointsPayload ?></script>
</div>PairOpenStreetMap.js reads a JSON script inside the map root:
{
"points": [
{
"marker": 1,
"title": "Main office",
"location": "Milan",
"latitude": 45.4642,
"longitude": 9.19,
"url": "/offices/1"
}
]
}Supported point fields:
-
latitudeandlongitudeare required and must be valid numeric coordinates -
markeris the visible marker label -
titleandlocationare combined for marker accessibility labels and browser tooltips -
urlordetailUrlcontrols the marker link; unsafe protocols are ignored -
categoryandstatusare normalized for project code that wants to extend marker styling later
Invalid points are discarded before rendering. If no valid points remain, the map shows the empty message from data-pair-osm-empty-message, or No points to show. by default.
Options can be passed as data-* attributes on the map root:
<div
class="pair-osm-map"
data-pair-osm-map
data-pair-osm-min-zoom="11"
data-pair-osm-max-zoom="17"
data-pair-osm-single-point-zoom="15"
data-pair-osm-padding="64"
data-pair-osm-tile-url-template="https://tile.openstreetmap.org/{z}/{x}/{y}.png">
</div>The same values can be provided when initializing a map manually:
window.PairOpenStreetMap.initMap(mapElement, {
points,
minZoom: 11,
maxZoom: 17,
singlePointZoom: 15,
padding: 64,
tileUrlTemplate: "https://tile.openstreetmap.org/{z}/{x}/{y}.png",
});Zoom values are clamped to Pair's supported tile range, and marker links are normalized before they are assigned to anchors.
PairOpenStreetMap.js exposes window.PairOpenStreetMap.
Public methods:
-
initAll(root = document)scans a document or fragment for[data-pair-osm-map] -
initMap(root, options = {})initializes one map root -
scheduleRenderAll()schedules a resize redraw -
renderAll()redraws all initialized maps immediately
Use initAll(fragment) after injecting map markup through AJAX or a modal body.
The framework asset copy script copies PairOpenStreetMap.js because it recursively copies every bundled file under assets/.
For a normal deployment, copy all assets:
php vendor/viames/pair/scripts/copy-assets.php public/assetsIf you use the js filter, remember that only JavaScript files are copied. Copy all assets, or copy CSS separately, when the map needs pair.css.
- Keep the OpenStreetMap attribution visible when using OpenStreetMap tiles.
- For production traffic beyond light usage, consider a project-owned tile provider and pass its URL through
data-pair-osm-tile-url-templateortileUrlTemplate. -
OpenStreetMap::load()is additive: it does not change GoogleAddress, GoogleMaps, GoogleGeocoder, or GooglePlacesService.
See also: Integrations, GoogleMaps, GoogleAddress.