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
13 use Friendica\Core\Cache;
14 use Friendica\Core\Config;
15 use Friendica\Core\Hook;
16 use Friendica\Core\L10n;
17 use Friendica\Core\Logger;
18 use Friendica\Core\Renderer;
19 use Friendica\Util\ConfigFileLoader;
20 use Friendica\Util\Network;
21 use Friendica\Util\Strings;
23 const OSM_TMS = 'https://www.openstreetmap.org';
24 const OSM_NOM = 'https://nominatim.openstreetmap.org/search.php';
28 function openstreetmap_install()
30 Hook::register('load_config', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_load_config');
31 Hook::register('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
32 Hook::register('generate_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_map');
33 Hook::register('generate_named_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_named_map');
34 Hook::register('Map::getCoordinates', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_get_coordinates');
35 Hook::register('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');
37 Logger::log("installed openstreetmap");
40 function openstreetmap_uninstall()
42 Hook::unregister('load_config', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_load_config');
43 Hook::unregister('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
44 Hook::unregister('generate_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_map');
45 Hook::unregister('generate_named_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_named_map');
46 Hook::unregister('Map::getCoordinates', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_get_coordinates');
47 Hook::unregister('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');
49 Logger::log("removed openstreetmap");
52 function openstreetmap_load_config(\Friendica\App $a, ConfigFileLoader $loader)
54 $a->getConfigCache()->load($loader->loadAddonConfig('openstreetmap'));
57 function openstreetmap_alterheader($a, &$navHtml)
59 $addScriptTag = '<script type="text/javascript" src="' . DI::baseUrl()->get() . '/addon/openstreetmap/openstreetmap.js"></script>' . "\r\n";
60 $a->page['htmlhead'] .= $addScriptTag;
64 * @brief Add link to a map for an item's set location/coordinates.
66 * If an item has coordinates add link to a tile map server, e.g. openstreetmap.org.
67 * If an item has a location open it with the help of OSM's Nominatim reverse geocode search.
72 function openstreetmap_location($a, &$item)
74 if (!(strlen($item['location']) || strlen($item['coord']))) {
79 * Get the configuration variables from the config.
80 * @todo Separate the tile map server from the text-string to map tile server
81 * since they apparently use different URL conventions.
82 * We use OSM's current convention of "#map=zoom/lat/lon" and optional
83 * ?mlat=lat&mlon=lon for markers.
86 $tmsserver = Config::get('openstreetmap', 'tmsserver', OSM_TMS);
87 $nomserver = Config::get('openstreetmap', 'nomserver', OSM_NOM);
88 $zoom = Config::get('openstreetmap', 'zoom', OSM_ZOOM);
89 $marker = Config::get('openstreetmap', 'marker', OSM_MARKER);
91 // This is needed since we stored an empty string in the config in previous versions
92 if (empty($nomserver)) {
96 if ($item['coord'] != "") {
97 $coords = explode(' ', $item['coord']);
98 if (count($coords) > 1) {
99 $lat = urlencode(round($coords[0], 5));
100 $lon = urlencode(round($coords[1], 5));
101 $target = $tmsserver;
103 $target .= '?mlat=' . $lat . '&mlon=' . $lon;
105 $target .= '#map='.intval($zoom).'/'.$lat.'/'.$lon;
109 if (empty($target)) {
110 $target = $nomserver.'?q='.urlencode($item['location']);
113 if ($item['location'] != "") {
114 $title = $item['location'];
116 $title = $item['coord'];
119 $item['html'] = '<a target="map" title="'.$title.'" href= "'.$target.'">'.$title.'</a>';
122 function openstreetmap_get_coordinates($a, &$b)
124 $nomserver = Config::get('openstreetmap', 'nomserver', OSM_NOM);
126 // This is needed since we stored an empty string in the config in previous versions
127 if (empty($nomserver)) {
128 $nomserver = OSM_NOM;
131 $args = '?q=' . urlencode($b['location']) . '&format=json';
133 $cachekey = "openstreetmap:" . $b['location'];
134 $j = Cache::get($cachekey);
137 $curlResult = Network::curl($nomserver . $args);
138 if ($curlResult->isSuccess()) {
139 $j = json_decode($curlResult->getBody(), true);
140 Cache::set($cachekey, $j, Cache::MONTH);
144 if (!empty($j[0]['lat']) && !empty($j[0]['lon'])) {
145 $b['lat'] = $j[0]['lat'];
146 $b['lon'] = $j[0]['lon'];
150 function openstreetmap_generate_named_map(&$a, &$b)
152 openstreetmap_get_coordinates($a, $b);
154 if (!empty($b['lat']) && !empty($b['lon'])) {
155 openstreetmap_generate_map($a, $b);
159 function openstreetmap_generate_map(&$a, &$b)
161 $tmsserver = Config::get('openstreetmap', 'tmsserver', OSM_TMS);
163 if (strpos(DI::baseUrl()->get(true), 'https:') !== false) {
164 $tmsserver = str_replace('http:','https:',$tmsserver);
167 $zoom = Config::get('openstreetmap', 'zoom', OSM_ZOOM);
168 $marker = Config::get('openstreetmap', 'marker', OSM_MARKER);
170 $lat = $b['lat']; // round($b['lat'], 5);
171 $lon = $b['lon']; // round($b['lon'], 5);
173 Logger::log('lat: ' . $lat, Logger::DATA);
174 Logger::log('lon: ' . $lon, Logger::DATA);
176 $cardlink = '<a href="' . $tmsserver;
179 $cardlink .= '?mlat=' . $lat . '&mlon=' . $lon;
182 $cardlink .= '#map=' . $zoom . '/' . $lat . '/' . $lon . '">' . ($b['location'] ? Strings::escapeHtml($b['location']) : L10n::t('View Larger')) . '</a>';
183 if (empty($b['mode'])) {
184 $b['html'] = '<iframe style="width:100%; height:300px; border:1px solid #ccc" src="' . $tmsserver .
185 '/export/embed.html?bbox=' . ($lon - 0.01) . '%2C' . ($lat - 0.01) . '%2C' . ($lon + 0.01) . '%2C' . ($lat + 0.01) .
186 '&layer=mapnik&marker=' . $lat . '%2C' . $lon . '" style="border: 1px solid black"></iframe>' .
187 '<br/><small>' . $cardlink . '</small>';
189 $b['html'] .= '<br/>' . $cardlink;
192 Logger::log('generate_map: ' . $b['html'], Logger::DATA);
195 function openstreetmap_addon_admin(&$a, &$o)
197 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/openstreetmap/");
198 $tmsserver = Config::get('openstreetmap', 'tmsserver', OSM_TMS);
199 $nomserver = Config::get('openstreetmap', 'nomserver', OSM_NOM);
200 $zoom = Config::get('openstreetmap', 'zoom', OSM_ZOOM);
201 $marker = Config::get('openstreetmap', 'marker', OSM_MARKER);
203 // This is needed since we stored an empty string in the config in previous versions
204 if (empty($nomserver)) {
205 $nomserver = OSM_NOM;
208 $o = Renderer::replaceMacros($t, [
209 '$submit' => L10n::t('Submit'),
210 '$tmsserver' => ['tmsserver', L10n::t('Tile Server URL'), $tmsserver, L10n::t('A list of <a href="http://wiki.openstreetmap.org/wiki/TMS" target="_blank">public tile servers</a>')],
211 '$nomserver' => ['nomserver', L10n::t('Nominatim (reverse geocoding) Server URL'), $nomserver, L10n::t('A list of <a href="http://wiki.openstreetmap.org/wiki/Nominatim" target="_blank">Nominatim servers</a>')],
212 '$zoom' => ['zoom', L10n::t('Default zoom'), $zoom, L10n::t('The default zoom level. (1:world, 18:highest, also depends on tile server)')],
213 '$marker' => ['marker', L10n::t('Include marker on map'), $marker, L10n::t('Include a marker on the map.')],
217 function openstreetmap_addon_admin_post(&$a)
219 $urltms = ($_POST['tmsserver'] ?? '') ?: OSM_TMS;
220 $urlnom = ($_POST['nomserver'] ?? '') ?: OSM_NOM;
221 $zoom = ($_POST['zoom'] ?? '') ?: OSM_ZOOM;
222 $marker = ($_POST['marker'] ?? '') ?: OSM_MARKER;
224 Config::set('openstreetmap', 'tmsserver', $urltms);
225 Config::set('openstreetmap', 'nomserver', $urlnom);
226 Config::set('openstreetmap', 'zoom', $zoom);
227 Config::set('openstreetmap', 'marker', $marker);
229 info(L10n::t('Settings updated.') . EOL);