Jump to content

Module: Leaflet Map


Mats

Recommended Posts

I installed the version 3.0.3 (PW3) and tested the geocoding and It can't find many places and even street numbers. Please correct me if I'm wrong but Google Geocoding can't work without an API key and the module doesn't ask for a key so it's probably using Leaflet's default geocoding cause when I search for example "Terra Vibe Park" I'm getting Nothing found but if I go on this sample that uses Leaflet with Esri Geocoding and search for "Terra Vibe Park" it will find it. Shouldn't be better to update the module to use Esri Geocoding? According to that sample code it doesn't seem to require any API key.

Link to comment
Share on other sites

I started working on a module that will use exclusively the Esri Geocoding.

Since you're using the Leaflet Control Geocoder on your module that supports various geocoders, you should update it to let the user select which one they want to use by supplying their API key if the selected geocoder requires it.

EDIT: Unfortunately Esri isn't free.

  • Like 1
Link to comment
Share on other sites

Is it possible to use Leaflet's layer groups with this module? I have three pages, each containing a map, each one showing a different kind of data. I'd like to be able to have one map that shows all three types of data with the ability to turn layers on and off, each type being a different layer.

I'm thinking that the best way to do this is going to be by using the module to handle the backend, geocoding the data in the relevant pages/child pages, but using plain Leaflet.js to output the data on the front-end. Any other thoughts/suggestions would be appreciated though.

Link to comment
Share on other sites

Dear Mats,

I'm trying to get your Leaflet Map Module with PJAX to work. I'm using the MoXo https://github.com/MoOx/pjax library but seem to have some troubles with it: I managed to configure PJAX that the inline JavaScript is executed after every page fetch, but I get Error: Map container is already initialized.

Any ideas how to approach this? 

EDIT:

I decided to use my own code with the standard Leaflet library and getting the marker with data attributes:

.map(data-lat=$map->lat data-lng=$map->lng data-zoom=$map->zoom data-address=$map->address)

initializing the map with 

import L from "leaflet";
document.addEventListener("DOMContentLoaded", function(event) {
  let container = document.querySelector(".map");
  let mymap = L.map(container).setView([container.dataset.lat, container.dataset.lng], container.dataset.zoom);
  L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {
  maxZoom: 18,
  id: 'mapbox.outdoors'}).addTo(mymap);
  let marker = L.marker([container.dataset.lat, container.dataset.lng])
  }).addTo(mymap);
});

and triggering DOMContentLoaded in pjax:

document.addEventListener("pjax:success", function(event){
  window.document.dispatchEvent(new Event("DOMContentLoaded", {
    bubbles: true,
    cancelable: true
  }));
});

 

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

I tried using this module on a new website and it looks like it's no longer functioning, probably due to the Google Api issues.

When I add a new Field I immediately get the error "Error geocoding address" and cannot update the address without getting this error, and it no longer returns the lat/lng points when I change the address.

Link to comment
Share on other sites

  • 4 weeks later...

This module is great, essential. Unfortunately I can't yet get it to work on my front end. The map doesn't show up due to a javascript/jquery conflict.

The module outputs regular jquery script starting with $(function() { ... If I manually add the following alternative to the footer it works fine, the map shows up in all its CartoDB glory:

Spoiler

var jMap = jQuery.noConflict(); 

jMap(function() {
var mleafletmap1 = new jsMarkupLeafletMap();
mleafletmap1.setOption('zoom', 18);
mleafletmap1.setOption('scrollWheelZoom', 1);

mleafletmap1.init('mleafletmap1', 43.660072, -79.354134, 'CartoDB.Positron');
var default_marker_icon = L.AwesomeMarkers.icon({ icon: 'home', iconColor: 'white', prefix: 'fa', markerColor: 'darkblue' });
mleafletmap1.addMarkerIcon(default_marker_icon, 43.660072, -79.354134, '/events/myeventtitle/', 'My Event Title', '');
});

 

How can I get the module to do the same? Or get around this problem some other way?

Edit:

I have replaced the following line in MarkupLeafletMap.module:

$inlineScript = "<script type='text/javascript'>\n$(function() {\n" .

with this:

$inlineScript = "<script type='text/javascript'>\nvar jMap = jQuery.noConflict();\njMap(function() {\n" .

Now the module works as expected. 

Could you make that part of the module for next updates? With jLeaflet instead of the more generic jMap. Or is there a good reason not to do it like this or a more cleverer way to avoid jquery conflicts?

 

 

 

Link to comment
Share on other sites

  • 2 weeks later...

The 'address' field is supposed to output the address you entered.

echo $page->map->address;	// outputs the address you entered

But it actually outputs whatever the map thinks is at that lat-long, with a lot of unnecessary details added, something like this:

Quote

WhatThisPlaceUsedToBeCalled, 12, Street Name, Corktown, Old Toronto, Toronto, Ontario, M4M 1L9, Canada

The address search also insists Water Street is in lower Manhattan when I mean Water Street in Brooklyn. Brooklyn becomes Kings County in map->address; nobody calls it that!

You can force the map to select the right location by dragging the pointer. Now the address is:

Quote

DUMBO Historic District Proposal, Manhattan Bridge lower level, Two Bridges, Manhattan, Manhattan Community Board 3, New York County, New York City, New York, 10002, USA

Which is hilarious and completely useless.

Is there a way to keep/store the actual "address you entered"? Or else a way to format that map->address into something usable?

Link to comment
Share on other sites

Thank you for this module! 
It works great but I have some problems with the options not getting recognised.
(Iam using the dev version, because of the geocoding issues with google maps)

Since I use the map as a fullscreen background I need to disable the zoom on scroll feature for usability.
The option for the marker are working but the leaflet options scrollWheelZoom and dragging are ignored.
This is my code:

$map = $modules->get('MarkupLeafletMap'); 

$options = array('markerIcon' => 'flag', 'markerColour' => 'red', 'scrollWheelZoom' => false, 'dragging' => false);
echo $map->render($page, 'map', $options);

another option would be to disable this after init with map.scrollWheelZoom.disable()
But this is also not working, because map is not defined. Whats the name of the map instance in js?

  • Thanks 2
Link to comment
Share on other sites

  • 2 weeks later...

Hi everybody,

I'm using the dev-version, sans google api with ProcessWire 3.0.123 but unfortunately my map doesn't appear (blank div on the page): http://processwire.marcoangeli.net/about/

here are my settings:

 

_init.php

$map = $modules->get('MarkupLeafletMap');


_head.php

<?php echo $map->getLeafletMapHeaderLines(); ?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>


basic-page.php

echo $map->render($page, 'map');

 

Do I miss some basic setting?

 

Thanks!

 

 

 

Link to comment
Share on other sites

There's a problem with your JavaScript and CSS Files, many 404 Errors (Console output of google chrome):

font-awesome.min.css:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
MarkerCluster.css:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
MarkerCluster.Default.css:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
leaflet-providers.js:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
leaflet.awesome-markers.css:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
leaflet.markercluster.js:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
MarkupLeafletMap.js:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
leaflet.awesome-markers.min.js:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
leaflet-providers.js:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
MarkupLeafletMap.js:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
leaflet.awesome-markers.min.js:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
leaflet.awesome-markers.css:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
(index):67 Uncaught ReferenceError: jsMarkupLeafletMap is not defined
    at HTMLDocument.<anonymous> ((index):67)
    at j (jquery.min.js:2)
    at Object.fireWith [as resolveWith] (jquery.min.js:2)
    at Function.ready (jquery.min.js:2)
    at HTMLDocument.K (jquery.min.js:2)
/favicon.ico:1 Failed to load resource: the server responded with a status of 404 (Not Found)
font-awesome.min.css:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
leaflet.awesome-markers.css:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
MarkerCluster.css:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)
MarkerCluster.Default.css:1 Failed to load resource: the server responded with a status of 404 (Page Not Found)

And the links:

		<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>

 

  • Like 1
Link to comment
Share on other sites

hey zoeck,

thanks for the suggestions.

On my html I've got this code:

<link rel="stylesheet" type="text/css" href="/site/modules/FieldtypeLeafletMapMarker/assets/font-awesome-4.6.3/css/font-awesome.min.css">

but there's no FieldtypeLeafletMapMarker folder inside my module directory.

Instead I have MarkupLeafletMap folder with all the assets inside...

very strange.

I'll investigate further...

Link to comment
Share on other sites

hey zoeck, thank you for the feedback.

I finally resolved the issue: I installed the module by uploading the zip file: that didn't work. Then I uninstalled everything and followed the instructions:

I created a folder named "FieldtypeLeafleftMapMarker" with everything inside and that worked.

Thanks again.

Link to comment
Share on other sites

  • 3 weeks later...

Hey @Mats,
great module! I was playing around with pages->find today and was getting matches for pages that didn't have map markers stored to them, or at least I thought. On closer inspection in MySQL I found that there were DB rows for all pages that had the template with my map marker field, not just the ones with actual markers stored.

Most of the empty records look like this

data: EMPTY lat: 0.000000 lng: 0.000000 status: -1 zoom: 0

Some like

data: EMPTY lat: 0.000000 lng: 0.000000 status: -100 zoom: 0

Which I'm assuming would be the ones that once had a marker which was later removed through "Clear"

And some like this:

data: EMPTY lat: 0.000000 lng: 0.000000 status: -100 zoom: 16

Which I guess are pages that someone just fumbled around with the zoom on in the backend ?

So in order to find pages with actual map markers right now I'd need to set up my selector like

 template=mytemplate, map_marker.lat!=0, map_marker.lng!=0 

Instead of just

map_marker!=""

This might also explain why the marker input field keeps popping up in page edit mode, even though I've set the visibilty to "closed when blank & open when populated".

Am I missing something obvious or is this unintentional behaviour?

Best regards!

Link to comment
Share on other sites

  • 3 months later...

Great module.

I had some problems upgrading from 2.8.1 to 3.0.3. Using processwire upgrade doesn't work after several attempts on different sites I had multiple instances of MarkupAddInlineScript different versions and different paths. I finally made a backup of the database table for the field and the field definition (via export) removed the inputfield, then the complete module(s), reinstalled 3.0.3 and restored the field definition and the content to the database.

In this version in the frontend the map with several markers from a pagearray didn't show, Script console said arg.split is not a function.

Took me some time to realize that the init-function was wrong.  The default lat, lng options came with a comma instead of a point

$karte = $map->render($regLocations, 'geolocation', array('width' => '50%', 'height' => '350px', 'id' => 'locMap'));

results in:

locMap.init('locMap', 53,016109, 14,005830, 'OpenStreetMap.Mapnik');

I have to grab the lat, lng values of the first item and put them in the initialization

$karte = $map->render($regLocations, 'geolocation', array('width' => '50%', 'height' => '350px', 'id' => 'locMap', 'lat'=>$firstLoc->lat, 'lng'=>$firstLoc->lng));

to get the right output:

locMap.init('locMap', 53.016109, 14.005830, 'OpenStreetMap.Mapnik');

This might be a localization problem, since the sytem works with german settings.

Link to comment
Share on other sites

  • 1 month later...

Hello, 
i have imported locations as seperate variables 
longtitude and latitude 

How to use the module with this templatevars ? 
Can anybody give me an example from scratch ? I have to add the css and js files manuelly ? And the how to call the map ? 

Thanks 

Link to comment
Share on other sites

  • 2 weeks later...

Hello

I used this module and all work very well. Thanks for this ?

I have just 2 questions :

  1. Is there a way/option/hook to not appy the clustering in a multi-markers map ? OR define a threshold (10 markers for example or 100 kilometers between markers or 100 pixels between markers) ?
  2. Is there a way/option/hook to add margin or padding when fitToMarkers is true ? Because some edge markers are cut...

1658934703_pwtommedesavoielocalhost_acteurs_affineurs_.thumb.png.0a31db5eaef7c82ea3a78b5d099527d2.png

Thanks a lot for your help

Link to comment
Share on other sites

  • 1 month later...

I'm trying to automatically add pages containing a field with this fieldtype. while all other fields are populated as expected the lat/lng part of my geolocation field keeps its default values. Is there a special syntax to address these

$newPoi = $pages->add("tmbPoi", "$parentPageId->url", [
                'name' => $poi['id'],
                'title' => $poi['name'],
                'PLZ' => $PLZ,
                'Adresse' => $Adresse,
                'body' => $descriptionHtml,
                'geolocation->lat' => $lat,
                'geolocation->lng' => $lng,
                'canonical' => $poi->canonical
            ]);
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...