Jump to content

Module: Leaflet Map


Mats

Recommended Posts

@Juergen In leaflet JS there is a method invalidateSize() which checks if the size of the map container has changed and then updates the map.

When you call the UIKit modal, the size of the map container changes and therefore your map needs to be updated manually.

So you need to listen to the show.uk.modal event and attach a handler that updates the leaflet map.

The problem here is that the map object is not easily accessible from the global document scope because the JS that renders the map is executed inside an anonymous function call.

You should find a script like this just before the closing </body> tag:

<script type="text/javascript">
 $(function() {
var mleafletmap1 = new jsMarkupLeafletMap();
mleafletmap1.setOption('zoom', 18);

mleafletmap1.init('mleafletmap1', 49.572056, 10.878165, 'OpenStreetMap.Mapnik');
var default_marker_icon = L.AwesomeMarkers.icon({ icon: 'home', iconColor: 'white', prefix: 'fa', markerColor: 'darkblue' });
mleafletmap1.addMarkerIcon(default_marker_icon, 49.572056, 10.878165, '/maptest/', 'maptest', '');
});
</script>

So if you try and get the map object with $(mleafletmap1), this will not return what you need.

In order to have the map object available in global scope, you need to change some module code. You can find the changes at my fork in MarkupLeafletMap.module. Just copy this over your local file.

Now the script before the closing </body> tag should look something like this:

<script type="text/javascript">
var mymap;
 $(function() {
var mleafletmap1 = new jsMarkupLeafletMap();
mleafletmap1.setOption('zoom', 18);

mleafletmap1.init('mleafletmap1', 49.572056, 10.878165, 'OpenStreetMap.Mapnik');
var default_marker_icon = L.AwesomeMarkers.icon({ icon: 'home', iconColor: 'white', prefix: 'fa', markerColor: 'darkblue' });
mleafletmap1.addMarkerIcon(default_marker_icon, 49.572056, 10.878165, '/maptest/', 'maptest', '');
mymap = mleafletmap1;
});
</script>

And you can now access the map object in your script with mymap.map. Example code:

<script>
	var myModal = $('#yourmodalID');
	mymodal.on('show.uk.modal', function(){
		mymap.map.invalidateSize(true);
	})
</script>

While this is just a workaround it should help to display the map correctly.

@Mats Maybe we should add something like this to the module code so in future versions a map object will be returned by default? Or do you see another way to access the map object with the module as is?

  • Like 2
Link to comment
Share on other sites

Thanks @gebeer

but after replacing the MarkupLeafletMap.module file with your version, the map will be not rendered at all. So its not a big problem. I can view the map without modal too. A code modification is not what I want.  But thousand thanks for your efforts.

Best regards

Link to comment
Share on other sites

@gebeer

I tested it on the V3 version and changed the content of the files as suggested. The map in the screen stays blank and the consoles says:

==> jquery.min.js:2:31515
jQuery.Deferred exception: t is undefined o.Util.trim@https://unpkg.com/leaflet@1.0.3/dist/leaflet.js:5:1149
o.Util.splitWords@https://unpkg.com/leaflet@1.0.3/dist/leaflet.js:5:1230
o.Evented<.on@https://unpkg.com/leaflet@1.0.3/dist/leaflet.js:5:3878
L.MarkerClusterGroup<.initialize@http://durvenleven.be/pw/site/modules/FieldtypeLeafletMapMarker/assets/leaflet-markercluster/leaflet.markercluster.js:6:515
o.Class.extend/e@https://unpkg.com/leaflet@1.0.3/dist/leaflet.js:5:2729
jsMarkupLeafletMap@http://durvenleven.be/pw/site/modules/FieldtypeLeafletMapMarker/MarkupLeafletMap.js:54:19
@http://durvenleven.be/pw/straatnamen/abraham-hansplein/:36:20
g/</j@https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:29946
g/</k<@https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:30262
 undefined

                                                                                
==> TypeError: t is undefined[Meer info]  leaflet.js:5:1149
	o.Util.trim https://unpkg.com/leaflet@1.0.3/dist/leaflet.js:5:1149
	o.Util.splitWords https://unpkg.com/leaflet@1.0.3/dist/leaflet.js:5:1230
	o.Evented<.on https://unpkg.com/leaflet@1.0.3/dist/leaflet.js:5:3878
	L.MarkerClusterGroup<.initialize http://durvenleven.be/pw/site/modules/FieldtypeLeafletMapMarker/assets/leaflet-markercluster/leaflet.markercluster.js:6:515
	o.Class.extend/e https://unpkg.com/leaflet@1.0.3/dist/leaflet.js:5:2729
	jsMarkupLeafletMap http://durvenleven.be/pw/site/modules/FieldtypeLeafletMapMarker/MarkupLeafletMap.js:54:19
	<anoniem> http://durvenleven.be/pw/straatnamen/abraham-hansplein/:36:20
	g/</j https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:29946
	g/</k< https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js:2:30262

In InputfieldLeafletMapMarker.module - Lines 61 and 64 I also bumbed the version to 1.0.3 for the admin side and there the 'map' is working fine. (rendering-georeferring, saving etc) 

Link to comment
Share on other sites

@gebeer

I replaced/added  the content of these files in /pw/site/modules/FieldtypeLeafletMapMarker/assets/leaflet-markercluster

MarkerCluster.Default.css    add oldie styles     3 years ago
MarkerCluster.css    Corrected spiderfy leg anim and leg opacity option     2 years ago
leaflet.markercluster-src.js    Tag v1.0.4     29 days ago
leaflet.markercluster.js    Tag v1.0.4     29 days ago

EDIT: IT IS WORKING !! Thank you very much @gebeer

setup: PW 3.0.58 © 2017  Leaflet 1.0.3 with modified files as above is FieldtypeLeafletMapMarker/assets/leaflet-markercluster

Edited by Pretobrazza
Clarity for future readers having similar problem
Link to comment
Share on other sites

Hello @gebeer

11 hours ago, gebeer said:

@Pretobrazza It might be related to the jQuery version you are using. 1.11.0 is a bit outdated. You might want to try a newer one. My map is rendering fine on 2.2.4

Unless I'm missing something important about the version of jquery on my testing page I was using //ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js in the template file. Now I changed it to  //ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js and everything is fine with the map. The testing page takes a few seconds to load as There are 600+ pw children streets which will eventually be shown in the end ;) (look at the source). 

Next step for me to endeavor into :

I saw in the options of the leaflet-PW module, that one can control the behavior of  the `markerTitleField` (or alternatively markerLinkField) which means that a sliding pane or modal window would be triggered to load the content of a page. What do I put in there? Is it possible?

I'm looking around for a versatile Leaflet script (there are many it seems) to put content in a slider pane OR modal window, so that when someone clicks in the title link  in a popup or on a marker, instead of opening a new page, in the browser, a responsive page gets called up in a sliding pane or modal window inside the map with the possibility to have extra controls around them (eg. close, fullscreen, 'like', post a comment, mailto,  etc etc).  

.... but first finding the versatile sliding window (and/or modal) script to load any type of content into is already a challenge for me as I want to stick to the latest version of PW as well as of leaflet (1.0.3)  ...  Does anyone a suggestion?

Even though there is streetview in this example, I'm convinced that with the Leaflet Map module PW will render a page like this on it's socks, not so? 

(The next step after this will be even more of a challenge.to broaden the scope of this module even further)

Link to comment
Share on other sites

31 minutes ago, Pretobrazza said:

.... but first finding the versatile sliding window (and/or modal) script to load any type of content into is already a challenge for me as I want to stick to the latest version of PW as well as of leaflet (1.0.3)  ...  Does anyone a suggestion?

A dialog box, whether sliding or animated fade, doesn't have anything to do with ProcessWire. You can use Uikit, Bootstrap, or any other css framework to present that information.

 

Link to comment
Share on other sites

Thank you for your quick reply, but in the ### Options of the leaflet-PW module, can I control the behavior of  the `markerTitleField` (or alternatively markerLinkField) which means that the dialog box  would be triggered to load the content of a page. What do I put in the the `markerTitleField` (or alternatively 'markerLinkField')  option to do so? Is it possible? Does the corresponding .js or other file in the module provide for it?

19 minutes ago, rick said:

A dialog box, whether sliding or animated fade, doesn't have anything to do with ProcessWire. You can use Uikit, Bootstrap, or any other css framework to present that information

 

Link to comment
Share on other sites

I can't give specific details as I do not use this module. In theory, however:

You can load page content into a bootstrap (just an example) dialog box via an ajax request based on the user clicking the markerTitleField and then display that dialog box.

OR

Depending on the number of clickable markerTitleFields, you could pre-load hidden containers with the appropriate content and display the corresponding dialog box.

Maybe I'm not understanding the issue, which is likely. :)

Link to comment
Share on other sites

@rick

To call the map in the test page map I use:
 

<?php $options = array('markerIcon' => 'flag', 'markerColour' => 'purple', 'markerTitleField' => 'title',); 
  echo $map->render($page, 'map'); ?>

to zoom into my question in the options I find among many others::

`markerLinkField` | Page field to use for the marker link, or blank to not link (type: string; default: url)
`markerTitleField` | Page field to use for the marker title, or blank not to use a marker title (type: string; default: title)

I'm missing something like markerTitleLinkField whereby I could call a dialog box, am I right? 

Link to comment
Share on other sites

You wouldn't necessarily create another field just to provide a link. You can use jquery to locate the title field(s) and enclose the content within the desired anchor tag. 

But isn't that what the link field is for?

Link to comment
Share on other sites

@Pretobrazza with the options markerLinkField and markerTitleField you can pass field names. The content of those fields on your page will be used as title and url within the marker popup.

To apply the options you have to pass them to the render method

<?php $options = array('markerIcon' => 'flag', 'markerColour' => 'purple', 'markerTitleField' => 'title',); 
  echo $map->render($page, 'map', $options); ?>

If you want to have additional popup content you can use the option 'popupFormatter' which is documented in the Readme.md. You can pull any content from any page and put it in the 'popupFormatter' option's callback function.

 

 

Link to comment
Share on other sites

Hello everyone,
Hello @gebeer

After having tested lots of modal rendering scripts, for 3 solid days, I ended up with Featherlighta very lightweight jQuery lightbox plugin, which seems to harness many possibilities.

  1. From a 'Title' field in a Leaflet map popup, I can now render  child pages with a map in modal windows overlaying each other.  However now I need every map to get a different ID  instead of 'mleafletmap1' otherwise they won't render. I tried to put $page->name but as soon as there are hyphens, it doesn't work. Any idea of which dynamically generated variable I could use as an ID for the maps in the base-template?
  2. In the popover  the title has to be clicked twice to fire a modal page. How (where) can I change this behavior?
  3. Even though everything is working fine inside the map, at the bottom of the modal windows with a map I find this:
$(function() { var percelen = new jsMarkupLeafletMap(); percelen.setOption('zoom', 14); percelen.setOption('scrollWheelZoom', 1); percelen.init('percelen', 51.341845581086325, 3.268561363220215, 'OpenStreetMap.Mapnik'); var default_marker_icon = L.AwesomeMarkers.icon({ icon: 'home', iconColor: 'white', prefix: 'fa', markerColor: 'green' }); });

(in a map with markers this gets much longer)

Is it an error that this appears under each modal map or is it something I will have to look into on the side of Featherlight?

In any case, at least I'm moving on now and as soon as the problems above can be solved (especially the first 2), I will start playing around with basemaps  and wms and see what happens ... 

Link to comment
Share on other sites

Hello,

I managed to render a leaflet map with wms in a barebone template in processwire.

Seen that it works, I added a new provider in leaflet-providers.js and I also updated providers.inc and providers.json files.

In leaflet-providers.js  I added:

		.......
		}, <--- didnt forget the comma ;)
		GRB: {
			url: 'https://geoservices.informatievlaanderen.be/raadpleegdiensten/GRB/wms?',
			options: {
				layers: 'GRB_BSK',
				format: 'image/png',
				transparent: true,
				attribution: 'Realized on Processwire'
			}
		}

As a proof of concept first, in order to render the map I must replace the L.tileLayer by L.tileLayer.wms somewhere in the script so that I can obtain a:

whatever = L.tileLayer.wms("https://geoservices.informatievlaanderen.be/raadpleegdiensten/GRB/wms?", {
            layers: 'GRB_BSK',
            format: 'image/png',
            transparent: true,
            attribution: "Realised on Processwire"
        }).addTo(map);

After a hint from @gebeerI tried to do so  in MarkupLeafletMap.js line 47. but I don't think I'm doing it right.  

Is there a particular syntax I should use?

Link to comment
Share on other sites

  • 2 weeks later...

Could you consider setting scrollWheelZoom: false as option for the backend?

I inadvertently easily change carefully set zoom settings just by scrolling the edit-page with the Trackpad or mousewheel. :-)

Link to comment
Share on other sites

Hello,

Now I have come to the stage whereby I want to import hundreds of pages via CSV with addresses. 

I tried to find a solution in this forum via Google search and tried all the possibilities  that were suggested for Batch child editor  as well as in the code of the ImportPagesCSV module being used/suggested  for Ryans's MapMaker but both of them refuse to work and import the adresses, lat lng zoom in one way on another into the Leaflet mapfield.

Now I have 2 questions:

1. Is anyone succeeding at the moment to upload CSV's to the Leaflet Map addressfield?  How?   My version of PW is 3.0.61 - Leaflet V 1.03 - php 7.0. 

2. If so, here's my second question (to @gebeer,  I think?):     Say, I only upload the address, will it geocode automatically? If not can it be done or will I be to geocode the addresses externally beforehand before I upload the CSV's?

Kind regards,
Bernard   

Link to comment
Share on other sites

@Pretobrazza, I just tested CSV import of FieldtypeLeafletMapMarker fields with the ImportPagesCSV module and it works fine but needs a couple of simple modifications to the ImportPagesCSV module.

Add FieldtypeLeafletMapMarker to the array of allowed fieldtypes:

/**
 * List of Fieldtypes that we support importing to
 *
 */
protected $fieldtypes = array(
    'FieldtypePageTitle',
    'FieldtypeText',
    'FieldtypeTextarea',
    'FieldtypeInteger',
    'FieldtypeFloat',
    'FieldtypeEmail',
    'FieldtypeURL',
    'FieldtypeCheckbox',
    'FieldtypeFile',
    'FieldtypePage',
    'FieldtypeLeafletMapMarker',
    );

Tell the module how to handle the import of the FieldtypeLeafletMapMarker fieldtype:

/**
 * Assign a value to a page field 
 *
 */
protected function importPageValue(Page $page, $name, $value) {

    $field = $this->fields->get($name); 

    if($field->type instanceof FieldtypeFile) {

        $value = trim($value); 
        // split delimeted data to an array
        $value = preg_split('/[\r\n\t|]+/', $value); 
        if($field->maxFiles == 1) $value = array_shift($value);
        $data = $page->ImportPagesCSVData; 
        $data[$name] = $value; 
        $page->ImportPagesCSVData = $data; 

    } elseif($field->type instanceof FieldtypeLeafletMapMarker) {

        $value = trim($value);

        // If importing pipe-separated latitude and longitude values, e.g. "-46.0026999|168.03567"
        // list($lat,$lng) = explode('|', $value);
        // $page->$name->lat = $lat;
        // $page->$name->lng = $lng;

        // If importing address string (automatically geocoded on import)
        $page->$name->address = $value;

    } else {

        $page->set($name, $value); 
        if($name == 'title') $page->name = $this->sanitizer->pageName($value, 2); // Sanitizer::translate
    }
}

You'll see I added two ways to import the map pin location: lat/lng (commented out) or address. If you import an address string the lat and lng are geocoded automatically on import. If you import lat and lng without an address then the pin is placed at that location but no address string is automatically generated. If you were to import an address string and a lat/lng (not shown here and not recommended) then the lat/lng is ignored and the location is geocoded from the address string - i.e. the same as just importing an address string.

You could add a zoom value if you wanted - just separate from the other value(s) with a pipe and adapt the example for lat/lng, i.e. explode() and list().

Edited by Robin S
removed draft text
Link to comment
Share on other sites

Hello @Robin S,

Thank you for your reply!

Since yesterday I tried those combinations but to no avail. Just now I turned the php7 to 5.6 but there too it doesn't work for me.

In one of the existing pages I had already saved an address but after uploading 2 records with an address (with $page->$name->address = $value;) , that one too disappeared. Could it be pw 3.0.61  which causes an issue?

Tonight I tried with BCE too but there too  .. no success :(

Link to comment
Share on other sites

This morning I tried again and only found 1 error in JqueryCore.js:2:40351 ( /wire/modules/Jquery/JqueryCore):  Use of getPreventDefault() isn't supported anymore. Instead use: defaultPrevented.   ->  I did so and the error went away. 

On the other hand, when I try and drop a CSV in the the ImportPagesCSV upload box, it doesn't accept it, so I have to press the upload button. For normal fields an even pictures the the ImportPagesCSV module  is working fine but not for the Leaflet submaps.  Same problem with BCE.  

My version of PW is 3.0.61 - Leaflet V 1.03 - php 7.1  now

Link to comment
Share on other sites

On 4/11/2017 at 0:26 PM, Pretobrazza said:

@gebeer

I replaced/added  the content of these files in /pw/site/modules/FieldtypeLeafletMapMarker/assets/leaflet-markercluster

MarkerCluster.Default.css    add oldie styles     3 years ago
MarkerCluster.css    Corrected spiderfy leg anim and leg opacity option     2 years ago
leaflet.markercluster-src.js    Tag v1.0.4     29 days ago
leaflet.markercluster.js    Tag v1.0.4     29 days ago

EDIT: IT IS WORKING !! Thank you very much @gebeer

setup: PW 3.0.58 © 2017  Leaflet 1.0.3 with modified files as above is FieldtypeLeafletMapMarker/assets/leaflet-markercluster

Hi,

I face a problem that at a first glance looks so as the above ... but is different. Having pw3.0.61, after upgrading LeafletMapMarker to the last version (2.8.1), the module stopped outputting the leaflet <script> tag with its content. The <div id='mleafletmap1' ...> with its options is present, both single and multiple (21) items/pages supplied tested (1st parameter).

Would anyone know where the issue comes from and how to correct it?

The rendered html is this:
 

<!DOCTYPE html>
<html lang="en">
<head>
    <script src="/site/templates/scripts/jquery.js"></script>
    
    <!-- Leaflet --><link rel="stylesheet" type="text/css" href="/site/modules/FieldtypeLeafletMapMarker/assets/font-awesome-4.6.3/css/font-awesome.min.css">        <!-- Styles supporting the use of Leaflet.js -->
        <link rel="stylesheet" type="text/css" href="https://unpkg.com/leaflet@1.0.3/dist/leaflet.css" />
        <link rel="stylesheet" type="text/css" href="/site/modules/FieldtypeLeafletMapMarker/assets/leaflet-markercluster/MarkerCluster.css" />
        <link rel="stylesheet" type="text/css" href="/site/modules/FieldtypeLeafletMapMarker/assets/leaflet-markercluster/MarkerCluster.Default.css" />

        <!-- Scripts supporting the use of Leaflet.js -->
        <script type="text/javascript" src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
        <script type="text/javascript" src="/site/modules/FieldtypeLeafletMapMarker/assets/leaflet-markercluster/leaflet.markercluster.js"></script>
        <script type="text/javascript" src="/site/modules/FieldtypeLeafletMapMarker/assets/leaflet-providers/leaflet-providers.js"></script>
        <script type="text/javascript" src="/site/modules/FieldtypeLeafletMapMarker/MarkupLeafletMap.js"></script>

        <!-- Extend Leaflet with Awesome.Markers -->
        <link rel="stylesheet" type="text/css" href="/site/modules/FieldtypeLeafletMapMarker/assets/leaflet-awesome-markers/leaflet.awesome-markers.css" />
        <script type="text/javascript" src="/site/modules/FieldtypeLeafletMapMarker/assets/leaflet-awesome-markers/leaflet.awesome-markers.min.js"></script>
</head>
<body>
<div id='mleafletmap1' class='MarkupLeafletMap' style='width: 100%; height: 700px;'></div>
</body>
</html>

 

Edited by loukote
clarification
Link to comment
Share on other sites

On 5/10/2017 at 1:25 AM, loukote said:

[...] Would anyone know where the issue comes from and how to correct it? [...]

Hm, funny: the map appears when a user is logged in the back-end, does not appear otherwise. In MarkupLeafletMap.module (version 3.0.2) on line 231 the map <div> is output and the <script> is sent to the be hooked before the </body> end-tag just after that, lines 233 to 283. @gebeer, would there be anything suspicious in the MarkupAddInlineScript.module?

Link to comment
Share on other sites

@loukote I just checked on a 3.0.61 install with leaflet 2.8.1 and 3.0.2 and here it is working as expected, even if not logged in to the backend at the same time.

This could be a permission related issue. Do you have any access restriction set on the template that renders the map?

You can check the value of $page->inlineScript on the page that you output the map after the code that renders the map. If it has a value, then the script code should be prepended to the closing </body> tag.

Link to comment
Share on other sites

On 11/5/2017 at 10:15 PM, loukote said:

Hm, funny: the map appears when a user is logged in the back-end, does not appear otherwise.

For me the problem with the map either or not appearing on the front-end lied with the " Front-End Page Editor " module. When uninstalling that module the problem dissapears as well.

Link to comment
Share on other sites

In my case I also had the problem that the map was not rendered.

I found out that a javascript (in my case fullcalendar) for initialization was responsible for this behavior. So I decided to put it inline only on that page where I needed it and not in an external javascript file which is included in every page load.

Another point was that if I turn template cache on the map will not be rendered too, so I disable the cache on the template where the map is included.

So if you have problems, please check this 2 points. Maybe this solves your problem.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...