$49

Smart Map

Documentation  •  Changelog

Documentation

Visitor Geolocation

Occasionally, you'll need to know where your site visitors are located. Fortunately, Smart Map provides an easy way to geolocate visitors based on their IP address.

On the plugin's Settings page, select which geolocation service you'd like to use...

Accessing visitor geolocation data via Twig:

craft.smartMap.visitor

Accessing visitor geolocation data via PHP:

craft()->smartMap->visitor

Both of these methods will return an array containing various data of the visitor's approximate whereabouts.

The "visitor" array will contain the following keys:

  • ip
  • city
  • state
  • zipcode
  • country
  • latitude
  • longitude
  • coords
{# Visitor's IP address #}
{{ craft.smartMap.visitor.ip }}

{# Visitor's country #}
{{ craft.smartMap.visitor.country }}

Accuracy

These techniques rely on calculating location from the user's IP address. Please be aware, this will rarely be 100% accurate. Generally speaking, you will end up with geolocation results which are within a few miles of the visitor's actual location, although occasionally they will be detected as much farther away.

A more precise method of visitor geolocation can be done using the HTML 5 geolocation feature. However, this will prompt the user to give your site permission to know their location, and it's possible (and common) for them to decline.