Tutorial: Javascript + jQuery


Geocoding by address components

$(function() {
  var apiKey = "YOUR-API-KEY-HERE";
  var country = "US";
  var state = "CA";
  var city = "San Francisco";
  var address = "123 Some Street";

  var url = "https://api.geolake.com/geocode?api_key=" + encodeURIComponent(apiKey) +
    "&country=" + encodeURIComponent(country) +
    "&state=" + encodeURIComponent(state) +
    "&city=" + encodeURIComponent(city) +
    "&address=" + encodeURIComponent(address);

  $.get(url, function(data) {
    if(data.success) {
      console.log("Coordinate: " + data.latitude + "; " + data.longitude);
    } else {
      console.log("Could not find the specified address");
    }
  });
});


Geocoding by IP

$(function() {
  var apiKey = "YOUR-API-KEY-HERE";
  var ip = "1.2.3.4";// Specify the IP you want to geocode
  ip = "detect";// Or specify the IP as 'detect' to use the IP address of the client that is placing the API call

  var url = "https://api.geolake.com/geocode?api_key=" + encodeURIComponent(apiKey) +
    "&ip=" + encodeURIComponent(ip);

  $.get(url, function(data) {
    if(data.success) {
      var countryCode;
      var currencyCode;
      var timezone;
      var city;
      if(data.place) {
        countryCode = data.place.countryCode;
        currencyCode = data.place.currencyCode;
        timezone = data.place.timezone;
        city = data.place.city;
      }

      //NOTE: Depending on the IP address, some of the returned values might be undefined
      console.log("Coordinate: " + data.latitude + "; " + data.longitude);
      console.log("City: " + city + ", " + countryCode);
      console.log("Currency: " + currencyCode);
      console.log("Timezone: " + timezone);
    } else {
      console.log("Could not find the specified IP address");
    }
  });
});