4 * Description: Use OpenStreetMap for displaying locations. After activation the post location just beneath your avatar in your posts will link to OpenStreetMap.
6 * Author: Fabio <http://kirgroup.com/~fabrixxm>
7 * Author: Mike Macgirvin <http://macgirvin.com/profile/mike>
8 * Author: Klaus Weidenbach
12 use Friendica\Core\Cache\Duration;
13 use Friendica\Core\Hook;
14 use Friendica\Core\Logger;
15 use Friendica\Core\Renderer;
17 use Friendica\Util\ConfigFileLoader;
18 use Friendica\Util\Strings;
20 const OSM_TMS = 'https://www.openstreetmap.org';
21 const OSM_NOM = 'https://nominatim.openstreetmap.org/search.php';
25 function openstreetmap_install()
27 Hook::register('load_config', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_load_config');
28 Hook::register('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
29 Hook::register('generate_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_map');
30 Hook::register('generate_named_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_named_map');
31 Hook::register('Map::getCoordinates', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_get_coordinates');
32 Hook::register('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');
34 Logger::log("installed openstreetmap");
37 function openstreetmap_uninstall()
39 Hook::unregister('load_config', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_load_config');
40 Hook::unregister('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
41 Hook::unregister('generate_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_map');
42 Hook::unregister('generate_named_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_named_map');
43 Hook::unregister('Map::getCoordinates', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_get_coordinates');
44 Hook::unregister('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');
46 Logger::log("removed openstreetmap");
49 function openstreetmap_load_config(\Friendica\App $a, ConfigFileLoader $loader)
51 $a->getConfigCache()->load($loader->loadAddonConfig('openstreetmap'));
54 function openstreetmap_alterheader($a, &$navHtml)
56 $addScriptTag = '<script type="text/javascript" src="' . DI::baseUrl()->get() . '/addon/openstreetmap/openstreetmap.js"></script>' . "\r\n";
57 DI::page()['htmlhead'] .= $addScriptTag;
61 * @brief Add link to a map for an item's set location/coordinates.
63 * If an item has coordinates add link to a tile map server, e.g. openstreetmap.org.
64 * If an item has a location open it with the help of OSM's Nominatim reverse geocode search.
69 function openstreetmap_location($a, &$item)
71 if (!(strlen($item['location']) || strlen($item['coord']))) {
76 * Get the configuration variables from the config.
77 * @todo Separate the tile map server from the text-string to map tile server
78 * since they apparently use different URL conventions.
79 * We use OSM's current convention of "#map=zoom/lat/lon" and optional
80 * ?mlat=lat&mlon=lon for markers.
83 $tmsserver = DI::config()->get('openstreetmap', 'tmsserver', OSM_TMS);
84 $nomserver = DI::config()->get('openstreetmap', 'nomserver', OSM_NOM);
85 $zoom = DI::config()->get('openstreetmap', 'zoom', OSM_ZOOM);
86 $marker = DI::config()->get('openstreetmap', 'marker', OSM_MARKER);
88 // This is needed since we stored an empty string in the config in previous versions
89 if (empty($nomserver)) {
93 if ($item['coord'] != "") {
94 $coords = explode(' ', $item['coord']);
95 if (count($coords) > 1) {
96 $lat = urlencode(round($coords[0], 5));
97 $lon = urlencode(round($coords[1], 5));
100 $target .= '?mlat=' . $lat . '&mlon=' . $lon;
102 $target .= '#map='.intval($zoom).'/'.$lat.'/'.$lon;
106 if (empty($target)) {
107 $target = $nomserver.'?q='.urlencode($item['location']);
110 if ($item['location'] != "") {
111 $title = $item['location'];
113 $title = $item['coord'];
116 $item['html'] = '<a target="map" title="'.$title.'" href= "'.$target.'">'.$title.'</a>';
119 function openstreetmap_get_coordinates($a, &$b)
121 $nomserver = DI::config()->get('openstreetmap', 'nomserver', OSM_NOM);
123 // This is needed since we stored an empty string in the config in previous versions
124 if (empty($nomserver)) {
125 $nomserver = OSM_NOM;
128 $args = '?q=' . urlencode($b['location']) . '&format=json';
130 $cachekey = "openstreetmap:" . $b['location'];
131 $j = DI::cache()->get($cachekey);
134 $curlResult = DI::httpRequest()->get($nomserver . $args);
135 if ($curlResult->isSuccess()) {
136 $j = json_decode($curlResult->getBody(), true);
137 DI::cache()->set($cachekey, $j, Duration::MONTH);
141 if (!empty($j[0]['lat']) && !empty($j[0]['lon'])) {
142 $b['lat'] = $j[0]['lat'];
143 $b['lon'] = $j[0]['lon'];
147 function openstreetmap_generate_named_map(&$a, &$b)
149 openstreetmap_get_coordinates($a, $b);
151 if (!empty($b['lat']) && !empty($b['lon'])) {
152 openstreetmap_generate_map($a, $b);
156 function openstreetmap_generate_map(&$a, &$b)
158 $tmsserver = DI::config()->get('openstreetmap', 'tmsserver', OSM_TMS);
160 if (strpos(DI::baseUrl()->get(true), 'https:') !== false) {
161 $tmsserver = str_replace('http:','https:',$tmsserver);
164 $zoom = DI::config()->get('openstreetmap', 'zoom', OSM_ZOOM);
165 $marker = DI::config()->get('openstreetmap', 'marker', OSM_MARKER);
167 $lat = $b['lat']; // round($b['lat'], 5);
168 $lon = $b['lon']; // round($b['lon'], 5);
170 Logger::log('lat: ' . $lat, Logger::DATA);
171 Logger::log('lon: ' . $lon, Logger::DATA);
173 $cardlink = '<a href="' . $tmsserver;
176 $cardlink .= '?mlat=' . $lat . '&mlon=' . $lon;
179 $cardlink .= '#map=' . $zoom . '/' . $lat . '/' . $lon . '">' . ($b['location'] ? Strings::escapeHtml($b['location']) : DI::l10n()->t('View Larger')) . '</a>';
180 if (empty($b['mode'])) {
181 $b['html'] = '<iframe style="width:100%; height:300px; border:1px solid #ccc" src="' . $tmsserver .
182 '/export/embed.html?bbox=' . ($lon - 0.01) . '%2C' . ($lat - 0.01) . '%2C' . ($lon + 0.01) . '%2C' . ($lat + 0.01) .
183 '&layer=mapnik&marker=' . $lat . '%2C' . $lon . '" style="border: 1px solid black"></iframe>' .
184 '<br/><small>' . $cardlink . '</small>';
186 $b['html'] .= '<br/>' . $cardlink;
189 Logger::log('generate_map: ' . $b['html'], Logger::DATA);
192 function openstreetmap_addon_admin(&$a, &$o)
194 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/openstreetmap/");
195 $tmsserver = DI::config()->get('openstreetmap', 'tmsserver', OSM_TMS);
196 $nomserver = DI::config()->get('openstreetmap', 'nomserver', OSM_NOM);
197 $zoom = DI::config()->get('openstreetmap', 'zoom', OSM_ZOOM);
198 $marker = DI::config()->get('openstreetmap', 'marker', OSM_MARKER);
200 // This is needed since we stored an empty string in the config in previous versions
201 if (empty($nomserver)) {
202 $nomserver = OSM_NOM;
205 $o = Renderer::replaceMacros($t, [
206 '$submit' => DI::l10n()->t('Submit'),
207 '$tmsserver' => ['tmsserver', DI::l10n()->t('Tile Server URL'), $tmsserver, DI::l10n()->t('A list of <a href="http://wiki.openstreetmap.org/wiki/TMS" target="_blank" rel="noopener noreferrer">public tile servers</a>')],
208 '$nomserver' => ['nomserver', DI::l10n()->t('Nominatim (reverse geocoding) Server URL'), $nomserver, DI::l10n()->t('A list of <a href="http://wiki.openstreetmap.org/wiki/Nominatim" target="_blank" rel="noopener noreferrer">Nominatim servers</a>')],
209 '$zoom' => ['zoom', DI::l10n()->t('Default zoom'), $zoom, DI::l10n()->t('The default zoom level. (1:world, 18:highest, also depends on tile server)')],
210 '$marker' => ['marker', DI::l10n()->t('Include marker on map'), $marker, DI::l10n()->t('Include a marker on the map.')],
214 function openstreetmap_addon_admin_post(&$a)
216 $urltms = ($_POST['tmsserver'] ?? '') ?: OSM_TMS;
217 $urlnom = ($_POST['nomserver'] ?? '') ?: OSM_NOM;
218 $zoom = ($_POST['zoom'] ?? '') ?: OSM_ZOOM;
219 $marker = ($_POST['marker'] ?? '') ?: OSM_MARKER;
221 DI::config()->set('openstreetmap', 'tmsserver', $urltms);
222 DI::config()->set('openstreetmap', 'nomserver', $urlnom);
223 DI::config()->set('openstreetmap', 'zoom', $zoom);
224 DI::config()->set('openstreetmap', 'marker', $marker);