d
Amit DhamuSoftware Engineer
 

Client Location From Google

1 minute read 00000 views

When using the Google API, data about from the Client is automatically sent to Google. This information includes latitude, longtitude, city and location. You can access this data if you have a Google API Key which you can obtain for free.

Firstly, create a JavaScript code block to load the Google API.

<script
  type="text/javascript"
  src="http://www.google.com/jsapi?key=YOUR_API_KEY"
></script>

Then to access the data, create a new JavaScript code block and do the following.

if (google.loader.ClientLocation) {
  var latitude = google.loader.ClientLocation.latitude
  var longtitude = google.loader.ClientLocation.longitude
  var city = google.loader.ClientLocation.address.city
  var region = google.loader.ClientLocation.address.region
  var country = google.loader.ClientLocation.address.country
  var countrycode = google.loader.ClientLocation.address.country_code
} else {
  // ClientLocation not found or not populated
  // so perform error handling
}

Credits