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\System;
16 use Friendica\Util\Network;
18 const OSM_TMS = 'https://www.openstreetmap.org';
19 const OSM_NOM = 'https://nominatim.openstreetmap.org/search.php';
23 function openstreetmap_install()
25 Addon::registerHook('load_config', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_load_config');
26 Addon::registerHook('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
27 Addon::registerHook('generate_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_map');
28 Addon::registerHook('generate_named_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_named_map');
29 Addon::registerHook('Map::getCoordinates', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_get_coordinates');
30 Addon::registerHook('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');
32 logger("installed openstreetmap");
35 function openstreetmap_uninstall()
37 Addon::unregisterHook('load_config', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_load_config');
38 Addon::unregisterHook('render_location', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_location');
39 Addon::unregisterHook('generate_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_map');
40 Addon::unregisterHook('generate_named_map', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_generate_named_map');
41 Addon::unregisterHook('Map::getCoordinates', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_get_coordinates');
42 Addon::unregisterHook('page_header', 'addon/openstreetmap/openstreetmap.php', 'openstreetmap_alterheader');
44 logger("removed openstreetmap");
47 function openstreetmap_load_config(\Friendica\App $a)
49 $a->loadConfigFile(__DIR__. '/config/openstreetmap.ini.php');
52 function openstreetmap_alterheader($a, &$navHtml)
54 $addScriptTag = '<script type="text/javascript" src="' . $a->getBaseURL() . '/addon/openstreetmap/openstreetmap.js"></script>' . "\r\n";
55 $a->page['htmlhead'] .= $addScriptTag;
59 * @brief Add link to a map for an item's set location/coordinates.
61 * If an item has coordinates add link to a tile map server, e.g. openstreetmap.org.
62 * If an item has a location open it with the help of OSM's Nominatim reverse geocode search.
67 function openstreetmap_location($a, &$item)
69 if (!(strlen($item['location']) || strlen($item['coord']))) {
74 * Get the configuration variables from the config.
75 * @todo Separate the tile map server from the text-string to map tile server
76 * since they apparently use different URL conventions.
77 * We use OSM's current convention of "#map=zoom/lat/lon" and optional
78 * ?mlat=lat&mlon=lon for markers.
81 $tmsserver = Config::get('openstreetmap', 'tmsserver', OSM_TMS);
82 $nomserver = Config::get('openstreetmap', 'nomserver', OSM_NOM);
83 $zoom = Config::get('openstreetmap', 'zoom', OSM_ZOOM);
84 $marker = Config::get('openstreetmap', 'marker', OSM_MARKER);
86 // This is needed since we stored an empty string in the config in previous versions
87 if (empty($nomserver)) {
91 if ($item['coord'] != "") {
92 $coords = explode(' ', $item['coord']);
93 if (count($coords) > 1) {
94 $lat = urlencode(round($coords[0], 5));
95 $lon = urlencode(round($coords[1], 5));
98 $target .= '?mlat=' . $lat . '&mlon=' . $lon;
100 $target .= '#map='.intval($zoom).'/'.$lat.'/'.$lon;
104 if (empty($target)) {
105 $target = $nomserver.'?q='.urlencode($item['location']);
108 if ($item['location'] != "") {
109 $title = $item['location'];
111 $title = $item['coord'];
114 $item['html'] = '<a target="map" title="'.$title.'" href= "'.$target.'">'.$title.'</a>';
117 function openstreetmap_get_coordinates($a, &$b)
119 $nomserver = Config::get('openstreetmap', 'nomserver', OSM_NOM);
121 // This is needed since we stored an empty string in the config in previous versions
122 if (empty($nomserver)) {
123 $nomserver = OSM_NOM;
126 $args = '?q=' . urlencode($b['location']) . '&format=json';
128 $cachekey = "openstreetmap:" . $b['location'];
129 $j = Cache::get($cachekey);
132 $curlResult = Network::curl($nomserver . $args);
133 if ($curlResult->isSuccess()) {
134 $j = json_decode($curlResult->getBody(), true);
135 Cache::set($cachekey, $j, Cache::MONTH);
139 if (!empty($j[0]['lat']) && !empty($j[0]['lon'])) {
140 $b['lat'] = $j[0]['lat'];
141 $b['lon'] = $j[0]['lon'];
145 function openstreetmap_generate_named_map(&$a, &$b)
147 openstreetmap_get_coordinates($a, $b);
149 if (!empty($b['lat']) && !empty($b['lon'])) {
150 openstreetmap_generate_map($a, $b);
154 function openstreetmap_generate_map(&$a, &$b)
156 $tmsserver = Config::get('openstreetmap', 'tmsserver', OSM_TMS);
158 if (strpos(z_root(), 'https:') !== false) {
159 $tmsserver = str_replace('http:','https:',$tmsserver);
162 $zoom = Config::get('openstreetmap', 'zoom', OSM_ZOOM);
163 $marker = Config::get('openstreetmap', 'marker', OSM_MARKER);
165 $lat = $b['lat']; // round($b['lat'], 5);
166 $lon = $b['lon']; // round($b['lon'], 5);
168 logger('lat: ' . $lat, LOGGER_DATA);
169 logger('lon: ' . $lon, LOGGER_DATA);
171 $cardlink = '<a href="' . $tmsserver;
174 $cardlink .= '?mlat=' . $lat . '&mlon=' . $lon;
177 $cardlink .= '#map=' . $zoom . '/' . $lat . '/' . $lon . '">' . ($b['location'] ? escape_tags($b['location']) : L10n::t('View Larger')) . '</a>';
178 if (empty($b['mode'])) {
179 $b['html'] = '<iframe style="width:100%; height:300px; border:1px solid #ccc" src="' . $tmsserver .
180 '/export/embed.html?bbox=' . ($lon - 0.01) . '%2C' . ($lat - 0.01) . '%2C' . ($lon + 0.01) . '%2C' . ($lat + 0.01) .
181 '&layer=mapnik&marker=' . $lat . '%2C' . $lon . '" style="border: 1px solid black"></iframe>' .
182 '<br/><small>' . $cardlink . '</small>';
184 $b['html'] .= '<br/>' . $cardlink;
187 logger('generate_map: ' . $b['html'], LOGGER_DATA);
190 function openstreetmap_addon_admin(&$a, &$o)
192 $t = get_markup_template("admin.tpl", "addon/openstreetmap/");
193 $tmsserver = Config::get('openstreetmap', 'tmsserver', OSM_TMS);
194 $nomserver = Config::get('openstreetmap', 'nomserver', OSM_NOM);
195 $zoom = Config::get('openstreetmap', 'zoom', OSM_ZOOM);
196 $marker = Config::get('openstreetmap', 'marker', OSM_MARKER);
198 // This is needed since we stored an empty string in the config in previous versions
199 if (empty($nomserver)) {
200 $nomserver = OSM_NOM;
203 $o = replace_macros($t, [
204 '$submit' => L10n::t('Submit'),
205 '$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>')],
206 '$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>')],
207 '$zoom' => ['zoom', L10n::t('Default zoom'), $zoom, L10n::t('The default zoom level. (1:world, 18:highest, also depends on tile server)')],
208 '$marker' => ['marker', L10n::t('Include marker on map'), $marker, L10n::t('Include a marker on the map.')],
212 function openstreetmap_addon_admin_post(&$a)
214 $urltms = defaults($_POST, 'tmsserver', OSM_TMS);
215 $urlnom = defaults($_POST, 'nomserver', OSM_NOM);
216 $zoom = defaults($_POST, 'zoom', OSM_ZOOM);
217 $marker = defaults($_POST, 'marker', OSM_MARKER);
219 Config::set('openstreetmap', 'tmsserver', $urltms);
220 Config::set('openstreetmap', 'nomserver', $urlnom);
221 Config::set('openstreetmap', 'zoom', $zoom);
222 Config::set('openstreetmap', 'marker', $marker);
224 info(L10n::t('Settings updated.') . EOL);