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
11 use Friendica\Core\Addon;
12 use Friendica\Core\Cache;
13 use Friendica\Core\Config;
14 use Friendica\Core\L10n;
15 use Friendica\Core\Logger;
16 use Friendica\Core\Renderer;
17 use Friendica\Core\System;
18 use Friendica\Util\Network;
19 use Friendica\Util\Strings;
21 const OSM_TMS = 'https://www.openstreetmap.org';
22 const OSM_NOM = 'https://nominatim.openstreetmap.org/search.php';
26 function openstreetmap_install()
28 Addon::registerHook('load_config', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_load_config');
29 Addon::registerHook('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
30 Addon::registerHook('generate_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_map');
31 Addon::registerHook('generate_named_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_named_map');
32 Addon::registerHook('Map::getCoordinates', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_get_coordinates');
33 Addon::registerHook('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');
35 Logger::log("installed openstreetmap");
38 function openstreetmap_uninstall()
40 Addon::unregisterHook('load_config', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_load_config');
41 Addon::unregisterHook('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
42 Addon::unregisterHook('generate_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_map');
43 Addon::unregisterHook('generate_named_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_named_map');
44 Addon::unregisterHook('Map::getCoordinates', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_get_coordinates');
45 Addon::unregisterHook('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');
47 Logger::log("removed openstreetmap");
50 function openstreetmap_load_config(\Friendica\App $a)
52 $a->loadConfigFile(__DIR__ . '/config/openstreetmap.config.php');
55 function openstreetmap_alterheader($a, &$navHtml)
57 $addScriptTag = '<script type="text/javascript" src="' . $a->getBaseURL() . '/addon/openstreetmap/openstreetmap.js"></script>' . "\r\n";
58 $a->page['htmlhead'] .= $addScriptTag;
62 * @brief Add link to a map for an item's set location/coordinates.
64 * If an item has coordinates add link to a tile map server, e.g. openstreetmap.org.
65 * If an item has a location open it with the help of OSM's Nominatim reverse geocode search.
70 function openstreetmap_location($a, &$item)
72 if (!(strlen($item['location']) || strlen($item['coord']))) {
77 * Get the configuration variables from the config.
78 * @todo Separate the tile map server from the text-string to map tile server
79 * since they apparently use different URL conventions.
80 * We use OSM's current convention of "#map=zoom/lat/lon" and optional
81 * ?mlat=lat&mlon=lon for markers.
84 $tmsserver = Config::get('openstreetmap', 'tmsserver', OSM_TMS);
85 $nomserver = Config::get('openstreetmap', 'nomserver', OSM_NOM);
86 $zoom = Config::get('openstreetmap', 'zoom', OSM_ZOOM);
87 $marker = Config::get('openstreetmap', 'marker', OSM_MARKER);
89 // This is needed since we stored an empty string in the config in previous versions
90 if (empty($nomserver)) {
94 if ($item['coord'] != "") {
95 $coords = explode(' ', $item['coord']);
96 if (count($coords) > 1) {
97 $lat = urlencode(round($coords[0], 5));
98 $lon = urlencode(round($coords[1], 5));
101 $target .= '?mlat=' . $lat . '&mlon=' . $lon;
103 $target .= '#map='.intval($zoom).'/'.$lat.'/'.$lon;
107 if (empty($target)) {
108 $target = $nomserver.'?q='.urlencode($item['location']);
111 if ($item['location'] != "") {
112 $title = $item['location'];
114 $title = $item['coord'];
117 $item['html'] = '<a target="map" title="'.$title.'" href= "'.$target.'">'.$title.'</a>';
120 function openstreetmap_get_coordinates($a, &$b)
122 $nomserver = Config::get('openstreetmap', 'nomserver', OSM_NOM);
124 // This is needed since we stored an empty string in the config in previous versions
125 if (empty($nomserver)) {
126 $nomserver = OSM_NOM;
129 $args = '?q=' . urlencode($b['location']) . '&format=json';
131 $cachekey = "openstreetmap:" . $b['location'];
132 $j = Cache::get($cachekey);
135 $curlResult = Network::curl($nomserver . $args);
136 if ($curlResult->isSuccess()) {
137 $j = json_decode($curlResult->getBody(), true);
138 Cache::set($cachekey, $j, Cache::MONTH);
142 if (!empty($j[0]['lat']) && !empty($j[0]['lon'])) {
143 $b['lat'] = $j[0]['lat'];
144 $b['lon'] = $j[0]['lon'];
148 function openstreetmap_generate_named_map(&$a, &$b)
150 openstreetmap_get_coordinates($a, $b);
152 if (!empty($b['lat']) && !empty($b['lon'])) {
153 openstreetmap_generate_map($a, $b);
157 function openstreetmap_generate_map(&$a, &$b)
159 $tmsserver = Config::get('openstreetmap', 'tmsserver', OSM_TMS);
161 if (strpos($a->getBaseURL(true), 'https:') !== false) {
162 $tmsserver = str_replace('http:','https:',$tmsserver);
165 $zoom = Config::get('openstreetmap', 'zoom', OSM_ZOOM);
166 $marker = Config::get('openstreetmap', 'marker', OSM_MARKER);
168 $lat = $b['lat']; // round($b['lat'], 5);
169 $lon = $b['lon']; // round($b['lon'], 5);
171 Logger::log('lat: ' . $lat, Logger::DATA);
172 Logger::log('lon: ' . $lon, Logger::DATA);
174 $cardlink = '<a href="' . $tmsserver;
177 $cardlink .= '?mlat=' . $lat . '&mlon=' . $lon;
180 $cardlink .= '#map=' . $zoom . '/' . $lat . '/' . $lon . '">' . ($b['location'] ? Strings::escapeHtml($b['location']) : L10n::t('View Larger')) . '</a>';
181 if (empty($b['mode'])) {
182 $b['html'] = '<iframe style="width:100%; height:300px; border:1px solid #ccc" src="' . $tmsserver .
183 '/export/embed.html?bbox=' . ($lon - 0.01) . '%2C' . ($lat - 0.01) . '%2C' . ($lon + 0.01) . '%2C' . ($lat + 0.01) .
184 '&layer=mapnik&marker=' . $lat . '%2C' . $lon . '" style="border: 1px solid black"></iframe>' .
185 '<br/><small>' . $cardlink . '</small>';
187 $b['html'] .= '<br/>' . $cardlink;
190 Logger::log('generate_map: ' . $b['html'], Logger::DATA);
193 function openstreetmap_addon_admin(&$a, &$o)
195 $t = Renderer::getMarkupTemplate("admin.tpl", "addon/openstreetmap/");
196 $tmsserver = Config::get('openstreetmap', 'tmsserver', OSM_TMS);
197 $nomserver = Config::get('openstreetmap', 'nomserver', OSM_NOM);
198 $zoom = Config::get('openstreetmap', 'zoom', OSM_ZOOM);
199 $marker = Config::get('openstreetmap', 'marker', OSM_MARKER);
201 // This is needed since we stored an empty string in the config in previous versions
202 if (empty($nomserver)) {
203 $nomserver = OSM_NOM;
206 $o = Renderer::replaceMacros($t, [
207 '$submit' => L10n::t('Submit'),
208 '$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>')],
209 '$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>')],
210 '$zoom' => ['zoom', L10n::t('Default zoom'), $zoom, L10n::t('The default zoom level. (1:world, 18:highest, also depends on tile server)')],
211 '$marker' => ['marker', L10n::t('Include marker on map'), $marker, L10n::t('Include a marker on the map.')],
215 function openstreetmap_addon_admin_post(&$a)
217 $urltms = defaults($_POST, 'tmsserver', OSM_TMS);
218 $urlnom = defaults($_POST, 'nomserver', OSM_NOM);
219 $zoom = defaults($_POST, 'zoom', OSM_ZOOM);
220 $marker = defaults($_POST, 'marker', OSM_MARKER);
222 Config::set('openstreetmap', 'tmsserver', $urltms);
223 Config::set('openstreetmap', 'nomserver', $urlnom);
224 Config::set('openstreetmap', 'zoom', $zoom);
225 Config::set('openstreetmap', 'marker', $marker);
227 info(L10n::t('Settings updated.') . EOL);