{"id":557,"date":"2016-09-19T09:31:37","date_gmt":"2016-09-19T09:31:37","guid":{"rendered":"http:\/\/smartfoxserver.com\/blog\/?p=557"},"modified":"2016-11-11T10:10:17","modified_gmt":"2016-11-11T10:10:17","slug":"using-the-geolocation-api","status":"publish","type":"post","link":"https:\/\/smartfoxserver.com\/blog\/using-the-geolocation-api\/","title":{"rendered":"Using the geolocation API"},"content":{"rendered":"<div class=\"dottedYellowBox\">The following instructions are valid for al <b>SFS2X prior to version 2.12<\/b>. See the last section at the bottom for more recent releases.<\/div>\n<p>SmartFoxServer 2X comes with a geolocation API and database that are used mainly\u00a0in\u00a0the AdminTool&#8217;s <a href=\"\/products\/analytics#p=intro\">Analytics<\/a> module to aggregate and show statistics based on geographical positions. The API is not directly exposed to\u00a0developers but since it&#8217;s been requested several times here is a quick recipe on how to use geolocation in your Extension code.<\/p>\n<p>For starters we need to add <strong>lib\/logs-analysis-app.jar\u00a0<\/strong>to our project&#8217;s dependencies and import a couple of classes:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport com.maxmind.geoip.Country;\r\nimport com.smartfoxserver.v2.admin.utils.IpGeoLocator;\r\n<\/pre>\n<p style=\"padding-left: 30px;\"><em>NOTE: starting from SmartFoxServer 2X\u00a0v2.12, the com.maxmind.geoip.Country class has been replaced by\u00a0com.maxmind.geoip2.record.Country; change the above import accordingly.<\/em><\/p>\n<p>Next we instantiate and initialize the IpGeoLocator class:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nIpGeoLocator geo = new IpGeoLocator();\r\ngeo.init(&quot;data\/&quot;, false);\r\n<\/pre>\n<p>This tells the geolocation service to load the relative database, located under SFS2X\/data\/. Finally we can test the service\u00a0with an IP address:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nString ipAdr = &quot;208.80.154.224&quot;; \/\/ wikipedia's IP\r\n\r\n\/\/ Locate address\r\nCountry country = geo.locateIP(adr);\r\n\r\nif (country == null)\r\n   System.out.println(&quot;Not found for address: &quot; + ipAdr);\r\nelse\r\n   System.out.println(ipAdr + &quot;: &quot; + countrt.getName() + &quot;, &quot; + country.getCode();\r\n<\/pre>\n<p>Running the code results in this output:<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n208.80.154.224: United States, US\r\n<\/pre>\n<p><strong>NOTE:<\/strong>\u00a0it is important to always check that the returned Country object is not null. If it is, the geolocation service was not able to resolve the IP to a specific country.<\/p>\n<h3>\u00bb SFS2X 2.12.x and higher<\/h3>\n<p>SFS2X 2.12 introduced automatic geolocation for every client. The setting is configurable per-Zone from the AdminTool &gt; Zone Configurator &gt; Enable Geolocation.<\/p>\n<p>When the service is active we can access the geolocation data with a few lines of server side code:<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport com.smartfoxserver.v2.util.Country;\r\n\r\nclass MyHandler extends BaseClientRequestHandler\r\n{\r\n\tvoid handleClientRequest(User sender, ISFSObject params)\r\n\t{\r\n\t\tCountry ctry = sender.getCountry();\r\n\t\t\r\n\t\tif (ctry != null)\r\n\t\t{\r\n\t\t\ttrace(&quot;User's country: &quot; + ctry.getName());\r\n\t\t\ttrace(&quot;Country ISO code: &quot; + ctry.getIsoCode());\r\n\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The following instructions are valid for al SFS2X prior to version 2.12. See the last section at the bottom for more recent releases. SmartFoxServer 2X comes with a geolocation API and database that are used mainly\u00a0in\u00a0the AdminTool&#8217;s Analytics module to aggregate and show statistics based on geographical positions. The API is not directly exposed to\u00a0developers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[75,7],"_links":{"self":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/557"}],"collection":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/comments?post=557"}],"version-history":[{"count":9,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/557\/revisions"}],"predecessor-version":[{"id":581,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/557\/revisions\/581"}],"wp:attachment":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/media?parent=557"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/categories?post=557"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/tags?post=557"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}