doublesecretagency / craft-googlemaps

Google Maps plugin for Craft CMS - Maps in minutes. Powered by the Google Maps API.
https://plugins.doublesecretagency.com/google-maps/
Other
10 stars 8 forks source link

Geolocation via google API service #67

Open splendidrob opened 1 year ago

splendidrob commented 1 year ago

Just wandering why geolocation requires a third party service when google provides this directly as part of its API? https://developers.google.com/maps/documentation/geolocation/overview

The 'Free' service the docs suggest (https://ipstack.com/product) only allow 100 requests a month before charging.

Many thanks!

splendidrob commented 1 year ago

Also just to add to this the current geolocation is fairly inaccurate (as noted in the docs) whilst using google's geolocation is pretty spot on. Is it not possible to utilise this in the plugin?

lindseydiloreto commented 1 year ago

Hi @splendidrob, thanks for raising this issue. It's a great question, one that requires serious consideration.

The Backstory

The ipstack API has been the preferred geolocation service since the dawn of Smart Map, around 8 years ago. It wasn't always called "ipstack"... it was originally called "FreeGeoIP", and was completely free (but sometimes unstable). Then the company got bought, changed names, and has slowly changed their policies.

I was not aware that ipstack now has a limit of only 100 free requests per month, I have no idea when that limitation was introduced. Clearly the company has changed a lot over the past 8 years, and it's extremely fair to now consider our other options.

The Plan

I've tinkered with the idea of switching over to the Google Geolocation API, although I still don't know much about it. I'll need to do a bit of research, of course, to figure out the best way to introduce that API. Once the API is supported, we may drop support for the ipstack and MaxMind APIs.

In terms of a timeline, I'm not certain as to when this will be prioritized. I can say, however, that it will almost certainly be tied to a major release. It will be considered a breaking change if we drop support for ipstack and MaxMind.


Thanks again for raising this issue! We'll post any updates back on this thread. 👍

splendidrob commented 1 year ago

Here's how I achieved it in the end:

var map = function() {

  var $map = $('[data-map]');

  if (!$map.length) { return; }

  console.log('map.init');

  var $mapId = $map.children('div').attr('id');
  var $mapTrigger = $('[data-map-trigger]');

  $mapTrigger.on('click', function(e) {

    const options = {
      enableHighAccuracy: true,
      timeout: 5000,
      maximumAge: 0
    };

    function success(pos) {
      const crd = pos.coords;
      var user = {'lat': crd.latitude, 'lng': crd.longitude}

      console.log('Your current position is:');
      console.log(`Latitude : ${crd.latitude}`);
      console.log(`Longitude: ${crd.longitude}`);
      console.log(`More or less ${crd.accuracy} meters.`);

      googleMaps.getMap($mapId)
        .center(user)
        .zoom(15)
        .markers(user,{'icon': '/assets/img/icon-location.png'})
    }

    function error(err) {
      console.warn(`ERROR(${err.code}): ${err.message}`);
    }

    navigator.geolocation.getCurrentPosition(success, error, options);
  });

}
lindseydiloreto commented 1 year ago

That's a great front-end solution, thanks for sharing @splendidrob!

Just to be clear to future readers... that solution doesn't use the Google Geolocation API. It is relying on native JavaScript geolocation in the browser. While there are some drawbacks to this approach (the user may decline to be geolocated), it will also give you the most accurate possible results.

Thanks again for the snippet, I'm sure some folks will find it helpful! 👍