Would be great to auto center the location on page load to the user location.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

robertwb’s picture

I am proposing a basic patch here using the Leaflet modules internal locate method. This does geolocate (works on mobile and desktop). I think that it is not the cleanest way to do it, though the assumption probably works 99.99% of the time. Basically, I patched the js file:

  • Check to see the center of the map using "map.getCenter()"
  • if the map is at 0,0 that it assumes that this is a new feature, or one with NULL geometry, then Zoom to center, otherwise allow the feature to dictate center.
robertwb’s picture

Status: Active » Needs review
abdolmaleki’s picture

How to add a marker

sano’s picture

hmm, this did not work for me. After patching/clearing cache and refreshing the node/add form where the map is used I would expect the browser to ask me for permission to allow geolocate me, but this is not happening and map is not centered to may location. The settings for the field are shown in the attached screenshot.

Joel MMCC’s picture

@sano #4, first, remove the default latitude and longitude, or set them to 0, 0. As stated in the comment with the patch, it works by checking for 0, 0, and if it’s not set there, it assumes that there is existing geometry and doesn’t attempt to geolocate since that would erase the existing data.

Also be sure that your website is being served via SSL (https protocol). As part of the drive to increase privacy, all modern browsers on all platforms now silently reject attempts to geolocate the user that come over unencrypted http — without an error message displayed to the user or even asking for their permission. You’d only see the error in the Javascript Console of your browser’s Developer Tools.

For development purposes you can use a self-signed SSL certificate, but for production sites use a real one. Let’s Encrypt now issues free certificates suitable for non-e-commerce uses, and many hosting providers now integrate that so that installing one can be done easily from the hosting control panel. They even auto-renew every 90 days for extra security.

I recommend all sites do this anyway, even if you don’t need HTML5 Geolocation. Google and other search engines are starting to mark all unencrypted http sites as unsafe, same as if your site had gotten hacked and was distributing malware! Also, of course, if your site isn’t encrypted, then every time you log in, you’re sending your admin login credentials over the Internet in a highly insecure manner.