Coder Social home page Coder Social logo

gsarig / ootb-openstreetmap Goto Github PK

View Code? Open in Web Editor NEW
29.0 2.0 3.0 41.51 MB

A map block for WordPress' Gutenberg Editor which uses OpenStreetMap and Leaflet.js. It needs no API keys and works out of the box. Benefiting from Gutenberg’s potential, the plugin tries a different take on how to add your locations on the map and rethinks a few things, UX-wise. Install it from https://wordpress.org/plugins/ootb-openstreetmap/

JavaScript 61.14% PHP 33.78% CSS 5.08%
gutenberg wordpress openstreetmap multiple-markers markers leafletjs

ootb-openstreetmap's Introduction

Download from WordPress.org

Out of the Block: OpenStreetMap

A map block for WordPress' Gutenberg Editor which uses OpenStreetMap and Leaflet.js. It needs no API keys and works out of the box (or, out of the Block, if you prefer). Benefiting from Gutenberg's potential, the plugin tries a different take on how to add your locations on the map and rethinks a few things, UX-wise.

Instead of manually adding coordinates for each one of your markers, just click-and-drop them directly on the map. You want to adjust their position? Just drag them wherever you want. And instead of filling-in custom fields to set each marker's popup content, just open that popup and start writing in it, the Gutenberg way (it supports WYSIWYG editing, with links, images, and all). It even stores the map's zoom level as you use it so that you don't have to set it by hand.


👇 Jump to the available Hooks🪝

👇 Jump to the available Shortcodes


👉 Read more about the overall UX challenges

👉 The challenges of building a user-friendly place search for OpenStreetMap

👉 Lessons learned from integrating OpenAI to a WordPress plugin

Demos

The main functionality, with drag and drop pins and WYSIWYG editing

Demo GIF

Using the location search

Demo GIF

Drawing a polygon

Demo GIF

Drawing a polyline

Demo GIF

OpenAI integration in action:

Demo GIF

Features

  • No need for API keys. Just install and use it.
  • Support for multiple markers.
  • Support for a different icon per marker.
  • Support for polygons and polylines.
  • Dead-simple interface. Don't search for coordinates and don't get overwhelmed by too many fields when using multiple markers. Just point and click on the map to add your marker where you want it and edit it's popup content directly from there.
  • Place search. Find locations by typing keywords.
  • Remembers the zoom that you set when adding the markers and stores it so that you don't set it by hand (which you can do anyway if you prefer).
  • OpenAI integration which allows you to add markers by using commands in natural language. Just say "please" to activate (e.g. "Please, show me where GOT was filmed"). Requires an OpenAI API key. Read more.
  • Query Maps: Supports creating a map out of maps added on other posts or post types. This can be quite powerful when, for example, you have a custom post type for "Places" with each place having its own map, and you want to dynamically gather-up all the places on a single map.
  • Shortcode support: You can use the shortcode [ootb_query] as an alternative way to use the aforementioned Query Maps feature (see the FAQ for more info).
  • Support for a location custom field, which can be used to store a post's or post type's location, following the Geodata guidelines. Read more in the v.2.8.0 release notes.
  • Adjust the map height.
  • Change the default marker icon with a custom one.
  • Enable or disable map dragging.
  • Enable or disable touch zoom.
  • Enable or disable double-click zoom.
  • Enable or disable scroll wheel zoom.
  • Set a minimum and maximum limit that the user can zoom on the frontend. Setting the same value to both fields will lock the zoom at that level.
  • Support for other Layer Providers: MapBox (using your own API key) and Stamen.
  • Option to export locations in a JSON file
  • Option to import locations from a JSON file

Installation

  1. Upload the plugin to your WordPress plugins directory and activate it.
  2. That's it. You can go to a post/page that supports the Gutenberg editor and start using the block called "Out of the Block: OpenStreetMap"

Frequently Asked Questions

Do I need an API key, like with Google Maps?

No.

That's the point, actually. Just install the plugin and start adding maps. Keep in mind, though, that as stated on the OpenStreetMap Tile Usage Policy, OSM’s own servers are run entirely on donated resources and they have strictly limited capacity. Using them on a site with low traffic will probably be fine. Nevertheless, you are advised to create an account to MapBox and get a free API Key.

How can I add a custom Mapbox style?

You can find the style URL on Mapbox Studio. There, use the "Share" button, and under "Developer resources", copy the "Style URL". It should look like that: mapbox://styles/username/style-id. You can declare a global style on the plugin's settings, to be used as a default for all the maps, or you can set a custom style for each map, by using the block's settings panel.

How do I add a new location?

To add a location, left-click on the map for a while, until you see the prompt saying "Release to drop a marker here". On browsers that support it, the cursor transforms from hand to crosshair, to make it even more apparent. As long as the prompt is visible, it means that releasing the click will drop the marker at that spot. That slight delay has been added to prevent you from accidentally adding markers all over the place with every click.

How do I remove a location?

Click on the marker to open up its popup. There, you will see the "Remove" button.

I can't find some of the options like disable dragging, setting zoom levels, etc

Check under the "Map behavior" section, at the blocks' settings at the sidebar on the right. It's toggled off by default, that's probably why you missed it.

How does the OpenAI integration work?

First of all, you will need to create an account to OpenAI and get an API key. Then, go to the plugin's settings page and paste your key there. After that, you can start adding markers by using commands in natural language. Just say "please" to activate (e.g. "Please, show me where GOT was filmed"). Please keep in mind, though, that it's like asking ChatGPT: the answers you get might not always be 100% reliable, and you should always double-check to confirm their accuracy. Read more.

How can I query maps from other posts or post types?

On the block's side panel, Select the "Map data" panel and click on the "Fetch locations" button. This will automatically retrieve on the frontend all the markers from your posts (you can also select a specific post type from the dropdown). The block will be locked from editing, as the markers will be dynamically retrieved from the selected posts. If you don't want that, there is a "Stop syncing" button that will unlock the block, drop the markers on the map and allow you to edit.

Shortcodes

[ootb_query]

The shortcode [ootb_query] allows you to display a dynamic map, which retrieves markers from other posts or post types. Just add it to a post or page and you're good to go. By default, it will fetch the markers from the 100 most recent posts. The shortcode supports the following attributes:

  • source: (Optional) The source of the data. Can be either geodata, if you want to retrieve the posts based on their Location custom meta field, or block, to retrieve posts containing map blocks in their content. The default option, which will be used if the attribute is omitted, is block.
  • post_type: (Optional) The type of post to query. By default, it is set to post.
  • posts_per_page: (Optional) The number of posts to be displayed on page. Default value is 100.
  • post_ids: (Optional) Comma-separated IDs of the posts to include in the query.
  • height: (Optional) The desired height for the map. Default value is empty, which falls back to 400px.
  • provider: (Optional) Specifies the map provider. Options are: openstreetmap, mapbox and stamen. The default value is an empty string which falls back to openstreetmap.
  • maptype: (Optional) Specifies the type of map. Options are: markers, polygon and polyline. The default value is an empty string, which will fall back to markers.
  • touchzoom: (Optional) If set, touch zoom will be enabled on the map. It can be either true or false. The default value is an empty string, which falls back to true.
  • scrollwheelzoom: (Optional) If set, enables zooming on the map with mouse scroll wheel. It can be either true or false. The default value is an empty string, which falls back to true.
  • dragging: (Optional) If set, dragging is enabled on the map. It can be either true or false. The default value is an empty string, which falls back to true.
  • doubleclickzoom: (Optional) If set, allows zooming in on the map with a double click. It can be either true or false. The default value is an empty string, which falls back to true.
  • marker: (Optional) Specifies the marker for the map. This should correspond to the URL of the image that you want to use as the marker's icon (example: https://www.example.com/my-custom-icon.png). The default value is an empty string, which retrieves the default marker.

Here's an example of how you can use it:

[ootb_query post_type="post" post_ids="1,2,3,4" height="400px" provider="mapbox" maptype="polygon" touchzoom="true" scrollwheelzoom="true" dragging="true" doubleclickzoom="true" marker="https://www.example.com/my-custom-icon.png"]

Hooks

There are a few hooks that you can use to further customize the plugin's behavior. Here they are:

ootb_query_post_type

Allows you to change the post type that the plugin will query for markers. By default, it is set to post. You can pass multiple post types as an array. Example:

add_filter( 'ootb_query_post_type', function() { return array( 'post', 'page' ); } );

ootb_query_posts_per_page

Allows you to change the number of posts that the plugin will query for markers. By default, it is set to 100. Example:

add_filter( 'ootb_query_posts_per_page', function() { return 500; } );

ootb_query_extra_args

Allows you to add extra arguments to the query that the plugin will use to retrieve markers. By default, it is set to an empty array. Example:

  add_filter(
     'ootb_query_extra_args',
     function() {
        return [
           'tax_query' => [
           [
              'taxonomy' => 'people',
              'field' => 'slug',
              'terms' => 'bob'
           ]
        ];
     }
  );

Keep in mind that the extra args will be merged with the default ones, so you don't have to worry about overriding them. In fact, the args that are required for the query to work, cannot be overridden.

ootb_cf_modal_content

Allows you to change the content of the modal that appears when you query posts based on their "Location" custom fields. By default, it will display the value set in the Address field. For example, the following code will display the post's title, thumbnail, excerpt and a link to the post:

add_filter( 'ootb_cf_modal_content', 'my_modal_content', 10, 2 );

function my_modal_content( $address, $post_id ) {

	return sprintf(
		'<div>
			<h3>%1$s</h3>
			<figure>%2$s</figure>
			<p>%3$s</p>
			<p><a href="%4$s">View post</a></p>
		</div>',
		get_the_title( $post_id ),
		get_the_post_thumbnail( $post_id, 'thumbnail' ),
		has_excerpt( $post_id ) ? get_the_excerpt( $post_id ) : $address,
		get_the_permalink( $post_id )
	);
}

ootb_cf_marker_icon

Allows you to change the marker icon for posts that have a "Location" custom field. By default, it will use the default marker. For example, the following code will use a custom marker for a post with ID 123:

add_filter( 'ootb_cf_marker_icon', 'my_marker_icon', 10, 2 );

function my_marker_icon( $icon_url, $post_id ){
	if( 123 === $post_id ) {
		$icon_url = 'https://example.com/my-marker.jpg';
	}
	return $icon_url;
}

Screenshots

The map editing screen The map editing screen

Map behavior options Map behavior options

Adding a marker Adding a marker

Using custom markers Using custom markers

Place search Place search

ootb-openstreetmap's People

Contributors

gsarig avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

ootb-openstreetmap's Issues

Dynamic map marker contents

Is there a way of having the marker popup content in a dynamic map, be filled by the corresponding post title, excerpt and permalink?

Adding map style

Great plugin so far!

It would be great to be able to add map style to a block. Maybe by adding a Mapbox style url? In our case it would even be preferable to add this by a filter in functions.php. So all maps would have the same style.

Saving geo data the WordPress way

Hello!

I'm excited to use your plugin from the very first beginning. I really, really like your approach of staying close to WordPress core and your leverage of what's already there, instead of reinventing the wheel.

Now for a new use-case I have to work with a rigid data-architecture, which incorporates the WordPress Geodata Standard. I would love to allow the users of the system, to enter the relevant geo data using your block, but - you know already - your saving the geo data within the block itself.

The codex says:

WordPress does not include any geographic functionality or functions to deal with Geodata itself [...].
But there is a standard for how to store geodata, so that plugins can interoperate.

Maybe because this is a very old standard within the WordPress world, there are some nice plugins following it, using geodata in a standardized way and helping devs like me combining such tools in new, creative ways.

Can you think of a way to store the relevant bits of the Geodata Standard into post_meta instead?

This would be amazing and will increase the interoperability of your plugin btw. You even found a very nice way to query multiple posts on a map, but this would unfortunately not for work for me out of the block.

Front end click on locator not working on Safari

Excellent plugin!! I love this! I am finding one weird issue when I happened to be in Safari -- if you place a pin on the map, and click on it on the front end the pop-up does not appear. It seems to be working fine on Firefox and Chrome. Can you reproduce this?

Fix for CloudFlare Rocket Loader

Currently, the plugin does not work with the Rocket Loader from CloudFlare.
As far as this is active, the plugin is no longer loaded on the page.

As long as the page is not yet published and only a logged in person with sufficient rights is editing the page, the map will load without any problems.
At this point CloudFlare does not intervene yet.

Maybe this info page will help:
CloudFlare - Ignore JavaScripts in Rocket Loader

Import / Export locations

  • Adds option to export locations to a JSON file
  • Adds option to import locations from a previously exported JSON file

ootb-import-export

Scheduled to be deployed with v. 2.2.0: #14

Poly line

I saw your roadmap on the blog post about this project (https://www.gsarigiannidis.gr/wordpress-gutenberg-map-block-openstreetmap/)

Another possible future feature (because of a specific need for a project I'm working on :) would be the ability to draw a route (polyline) on a map, as opposed to just pins.

I was thinking of actually playing around with this to see what I can do. If you are interested in having another person contribute to this and maybe someday leave you a pull request, would you share your package.json/package-lock.json just to speed up tooling.

This is a fantastic project. You are doing this right. Thank you!

Complianz with Out of the Block: OpenStreetMap

Hi,

I recently open an issue on Complianz repo.
We detected an error in ootb-openstreetmap when OpenStreetMap is blocked by Complianz.

Is it possible to support Complianz on your side as recommended by Complianz developers ?

WordPress: 5.9.7
Complianz Privacy Suite (GDPR/CCPA) premium multisite: 6.4.3
Out of the Block: OpenStreetMap: 2.4.0

Best regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.