Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geolocation via google API service #67

Open
splendidrob opened this issue Oct 6, 2022 · 4 comments
Open

Geolocation via google API service #67

splendidrob opened this issue Oct 6, 2022 · 4 comments
Labels
Feature Request New feature or request

Comments

@splendidrob
Copy link

splendidrob commented Oct 6, 2022

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
Copy link
Author

splendidrob commented Oct 7, 2022

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
Copy link
Collaborator

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

@lindseydiloreto lindseydiloreto added the Feature Request New feature or request label Oct 26, 2022
@splendidrob
Copy link
Author

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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants