Hello Everyone, In this tutorial, I am going provide an example that explains you, how to detect Geolocation(Latitude and Longitude) of an IP address using Visualforce and Apex. In this example, I am using a third party API (http://api.ipstack.com) to achieve this goal. Basically, I do a callout request on the mention API URL and get the response result in the form of a JSON object.
After completing this tutorial, you’ll able to:
- Find Geolocation of IP address.
So let's start,
First of all, To achieve this goal you have to get your free API key by registering yourself on https://ipstack.com/.
After registration, you will get the free API key, that you can use to do callout request in Salesforce to get IP geolocation.
Syntax of Callout URL:
http://api.ipstack.com/'+Your IP Address+'?access_key=Free API Access key';Limitation: With free API Access key you can do only 10,000 requests. For more requests, you have to get paid license of Ipstack.com
Find Geolocation of IP address
Step 1: Navigate to Setup | Security Controls | Remote Site Settings menu, and click on the New Remote Site button and enter the Remote Site Name and Remote Site URL. Make sure Active checkbox is checked then click on Save button.
Note: Remote Site Setting allow Apex to do callout on External URL
Step 2: Open Developer Console, Go to File>New>Apex Class and create an Apex controller called IPGeolocation. Replace the following code.
public class IPGeolocation { public String country {get;set;} public String ip {get;set;} public Decimal latitude {get;set;} public Decimal longitude {get;set;} public String countrycode{get;set;} public void getIPGeolocation() { string ReturnValue = ''; ReturnValue = ApexPages.currentPage().getHeaders().get('True-Client-IP'); if (ReturnValue == '' || ReturnValue == null) { ReturnValue = ApexPages.currentPage().getHeaders().get('X-Salesforce-SIP'); } if (ReturnValue == '' || ReturnValue == null) { ReturnValue = ApexPages.currentPage().getHeaders().get('X-Forwarded-For'); } Http h = new Http(); HttpRequest req = new HttpRequest(); req.setEndpoint('http://api.ipstack.com/'+ReturnValue+'?access_key=84cce4c3f6b699dc43cf4e'); req.setMethod('GET'); HttpResponse res; res = h.send(req); System.debug('Result'+res.getbody()); Map<string,object>m = (Map<string,object>)JSON.deserializeUntyped(res.getbody()); country =(String)m.get('country_name'); countrycode=(String)m.get('country_code'); ip=(String)m.get('ip'); latitude=(Decimal)m.get('latitude'); longitude =(Decimal)m.get('longitude'); } }Step 3: Open Developer Console, Go to File>New>Visualforce Page and create a new Visualforce Page called IPAddressGeolocation and replace the following markup.
<apex:page action="{!getIPGeolocation}" controller="IPGeolocation" showHeader="false" sidebar="false"> <div style="margin-left:30px;margin-top:30px;"> <span style="font-size:18px"> <u>IP Address Geolocation</u> </span><br/><br/> Country:{!country} <br/> Country Code: {!countrycode} <br/> IP: {!ip} <br/> Latitude: {!latitude} <br/> Longitude: {!longitude} </div> </apex:page>
Output:
See also:
- Create Zip of Salesforce Attachment Files in Visualforce
- Set PDF filename in Visualforce Page
- Remote Action in Visualforce Page
Conclusion:
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.
3 Comments
Use a powerful, not simple to guess password and frequently reinstate your passwords. What is My Ip Address
ReplyDeleteThanks for this blog keep sharing your thoughts like this...
ReplyDeleteReact JS Training in Chennai
Reat JS Online Course
Great post, thanks for sharing important information , keep posting more. Spoken English Course In Pune
ReplyDeletePost a Comment