Detect your location by IP address using Salesforce and external geolocation provider REST API

Usually I feel curious about who visits my blog, from which country and from which city.

I found some interesting post about showing location of customer on google map using Salesforce and as external geolocation detection provider.

However, this provider doesn’t allow you to use it without registration and revealing your actual location. So I felt engaged in building my own custom similar solution and finally I have built it.

I use in my solution three different geolocation detector providers, since looks like all of them give wrong information at least about my own IP address.
Anyway they have different accuracy, and I believe their accuracy may vary upon IP address, so each user can select different providers and find which one shows more exact information in his or her particular case.

There is my version of code

<apex:page controller="GL" cache="false" sidebar="false" showHeader="false" title="Geolocation Detection">
<apex:includeScript value=""/>
<script type="text/javascript">
window.onload = function(){

//Javascript function that loads the map. Notice I can simply use the normal Visualforce merge fields for lat and long.
function loadMap() {
var latlng = new google.maps.LatLng({!latitude}, {!longitude});
var myOptions = {
zoom: 11,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

var marker = new google.maps.Marker({
position: latlng,
title:"IP Location"

// To add the marker to the map, call setMap();
<span style="font-size: 1.4em">
<strong>Related Blog Post:</strong>
<apex:form >
<b>Select geolocation detection provider: </b>
<apex:selectList size="1" value="{!way}">
<apex:selectOption itemLabel="" itemValue="1"/>
<apex:selectOption itemLabel="telize" itemValue="2"/>
<apex:selectOption itemLabel="ipinfo" itemValue="3"/>
<apex:actionSupport event="onchange" action="{!refresh}"/>
<div id="map_canvas" style="height:500px"></div><br/>


public class GL {

public void refresh() {

HttpRequest req2 = new HttpRequest();
req2.setEndpoint(wayMap.get(way) + ip + (way == '3'? '/json' : '') );
Http http = new Http();
HTTPResponse res2 = http.send(req2);
if(res2.getStatusCode()==200) {

Object o = json.deserialize(res2.getBody(),clsMap.get(way));
if ( way == '1' || way == '2' ) {
iwan i = (iwan) o;
longitude = i.longitude;
latitude = i.latitude;
} else {
ipinfo ii = (ipinfo) o;
String[] ss = ii.loc.split(',');
longitude = ss[1];
latitude = ss[0];


public String longitude { get; set; }

public String latitude { get; set; }

public String ip { get {
ip = ApexPages.currentPage().getParameters().get('ip');
if (ip == null) {
ip = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP');
return ip;
} set;}

public String way { get {
if ( way == null ) {
way = ApexPages.currentPage().getParameters().get('way');
if ( way == null ) {
way = '1';
return way;
} set;}

Map<String, String> wayMap = new Map<String, String>{'1'=>'','2'=>'','3'=>''};
Map<String, System.Type> clsMap = new Map<String, System.Type>{'1'=>iwan.class,'2'=>iwan.class,'3'=>ipinfo.class};
public GL() {


public class iwan{
String country_name;
String country_code;
String region_name;
String region_code;
String city;
String zipcode;
String latitude;
String longitude;
String ip;
String metro_code;
String areacode;

public class ipinfo{
String ip;
String city;
String hostname;
String region;
String country;
String loc;
String org;



This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s