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_load_config(\Friendica\App $a, ConfigFileLoader $loader)
39 $a->getConfigCache()->load($loader->loadAddonConfig('openstreetmap'));
42 function openstreetmap_alterheader($a, &$navHtml)
44 $addScriptTag = '<script type="text/javascript" src="' . DI::baseUrl()->get() . '/addon/openstreetmap/openstreetmap.js"></script>' . "\r\n";
45 DI::page()['htmlhead'] .= $addScriptTag;
49 * @brief Add link to a map for an item's set location/coordinates.
51 * If an item has coordinates add link to a tile map server, e.g. openstreetmap.org.
52 * If an item has a location open it with the help of OSM's Nominatim reverse geocode search.
57 function openstreetmap_location($a, &$item)
59 if (!(strlen($item['location']) || strlen($item['coord']))) {
64 * Get the configuration variables from the config.
65 * @todo Separate the tile map server from the text-string to map tile server
66 * since they apparently use different URL conventions.
67 * We use OSM's current convention of "#map=zoom/lat/lon" and optional
68 * ?mlat=lat&mlon=lon for markers.
71 $tmsserver = DI::config()->get('openstreetmap', 'tmsserver', OSM_TMS);
72 $nomserver = DI::config()->get('openstreetmap', 'nomserver', OSM_NOM);
73 $zoom = DI::config()->get('openstreetmap', 'zoom', OSM_ZOOM);
74 $marker = DI::config()->get('openstreetmap', 'marker', OSM_MARKER);
76 // This is needed since we stored an empty string in the config in previous versions
77 if (empty($nomserver)) {
81 if ($item['coord'] != "") {
82 $coords = explode(' ', $item['coord']);
83 if (count($coords) > 1) {
84 $lat = urlencode(round($coords[0], 5));
85 $lon = urlencode(round($coords[1], 5));
88 $target .= '?mlat=' . $lat . '&mlon=' . $lon;
90 $target .= '#map='.intval($zoom).'/'.$lat.'/'.$lon;
95 $target = $nomserver.'?q='.urlencode($item['location']);
98 if ($item['location'] != "") {
99 $title = $item['location'];
101 $title = $item['coord'];
104 $item['html'] = '<a target="map" title="'.$title.'" href= "'.$target.'">'.$title.'</a>';
107 function openstreetmap_get_coordinates($a, &$b)
109 $nomserver = DI::config()->get('openstreetmap', 'nomserver', OSM_NOM);
111 // This is needed since we stored an empty string in the config in previous versions
112 if (empty($nomserver)) {
113 $nomserver = OSM_NOM;
116 $args = '?q=' . urlencode($b['location']) . '&format=json';
118 $cachekey = "openstreetmap:" . $b['location'];
119 $j = DI::cache()->get($cachekey);
122 $curlResult = DI::httpRequest()->get($nomserver . $args);
123 if ($curlResult->isSuccess()) {
124 $j = json_decode($curlResult->getBody(), true);
125 DI::cache()->set($cachekey, $j, Duration::MONTH);
129 if (!empty($j[0]['lat']) && !empty($j[0]['lon'])) {
130 $b['lat'] = $j[0]['lat'];
131 $b['lon'] = $j[0]['lon'];
135 function openstreetmap_generate_named_map(&$a, &$b)
137 openstreetmap_get_coordinates($a, $b);
139 if (!empty($b['lat']) && !empty($b['lon'])) {
140 openstreetmap_generate_map($a, $b);
144 function openstreetmap_generate_map(&$a, &$b)
146 $tmsserver = DI::config()->get('openstreetmap', 'tmsserver', OSM_TMS);
148 if (strpos(DI::baseUrl()->get(true), 'https:') !== false) {
149 $tmsserver = str_replace('http:','https:',$tmsserver);
152 $zoom = DI::config()->get('openstreetmap', 'zoom', OSM_ZOOM);
153 $marker = DI::config()->get('openstreetmap', 'marker', OSM_MARKER);
155 $lat = $b['lat']; // round($b['lat'], 5);
156 $lon = $b['lon']; // round($b['lon'], 5);
158 Logger::log('lat: ' . $lat, Logger::DATA);
159 Logger::log('lon: ' . $lon, Logger::DATA);
161 $cardlink = '<a href="' . $tmsserver;
164 $cardlink .= '?mlat=' . $lat . '&mlon=' . $lon;
167 $cardlink .= '#map=' . $zoom . '/' . $lat . '/' . $lon . '">' . ($b['location'] ? Strings::escapeHtml($b['location']) : DI::l10n()->t('View Larger')) . '</a>';
168 if (empty($b['mode'])) {
169 $b['html'] = '<iframe style="width:100%; height:300px; border:1px solid #ccc" src="' . $tmsserver .
170 '/export/embed.html?bbox=' . ($lon - 0.01) . '%2C' . ($lat - 0.01) . '%2C' . ($lon + 0.01) . '%2C' . ($lat + 0.01) .
171 '&layer=mapnik&marker=' . $lat . '%2C' . $lon . '" style="border: 1px solid black"></iframe>' .
172 '<br/><small>' . $cardlink . '</small>';
174 $b['html'] .= '<br/>' . $cardlink;
177 Logger::log('generate_map: ' . $b['html'], Logger::DATA);
180 function openstreetmap_addon_admin(&$a, &$o)
182 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/openstreetmap/");
183 $tmsserver = DI::config()->get('openstreetmap', 'tmsserver', OSM_TMS);
184 $nomserver = DI::config()->get('openstreetmap', 'nomserver', OSM_NOM);
185 $zoom = DI::config()->get('openstreetmap', 'zoom', OSM_ZOOM);
186 $marker = DI::config()->get('openstreetmap', 'marker', OSM_MARKER);
188 // This is needed since we stored an empty string in the config in previous versions
189 if (empty($nomserver)) {
190 $nomserver = OSM_NOM;
193 $o = Renderer::replaceMacros($t, [
194 '$submit' => DI::l10n()->t('Submit'),
195 '$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>')],
196 '$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>')],
197 '$zoom' => ['zoom', DI::l10n()->t('Default zoom'), $zoom, DI::l10n()->t('The default zoom level. (1:world, 18:highest, also depends on tile server)')],
198 '$marker' => ['marker', DI::l10n()->t('Include marker on map'), $marker, DI::l10n()->t('Include a marker on the map.')],
202 function openstreetmap_addon_admin_post(&$a)
204 $urltms = ($_POST['tmsserver'] ?? '') ?: OSM_TMS;
205 $urlnom = ($_POST['nomserver'] ?? '') ?: OSM_NOM;
206 $zoom = ($_POST['zoom'] ?? '') ?: OSM_ZOOM;
207 $marker = ($_POST['marker'] ?? '') ?: OSM_MARKER;
209 DI::config()->set('openstreetmap', 'tmsserver', $urltms);
210 DI::config()->set('openstreetmap', 'nomserver', $urlnom);
211 DI::config()->set('openstreetmap', 'zoom', $zoom);
212 DI::config()->set('openstreetmap', 'marker', $marker);