0

I want to get latitude and longitude without using android Location Service and Google Api. So Is there any way to get latitude and longitude from connected wifi router ?

Android dev
  • 273
  • 2
  • 5
  • 23

2 Answers2

2

First of all get the IP Address using the following code:

 public String getIpAddr() {
   WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
   WifiInfo wifiInfo = wifiManager.getConnectionInfo();
   int ip = wifiInfo.getIpAddress();

   String ipString = String.format("%d.%d.%d.%d", (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff));
   return ipString;
}

Please Note: You need to add android.permission.INTERNET and android.permission.ACCESS_WIFI_STATE in your AndroidManifest.xml as to access the code.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

After this use some 3rd party web service which gives you langitude and latitude by providing IP. Example : http://www.geoipservice.asmx/GetGeoIP?IPAddress= (PASS IP AS STRING)

Hope you got your answer!!!

prashant0205
  • 269
  • 1
  • 3
  • 17
  • Thanks for reply .Let me check this – Android dev Jun 14 '16 at 05:26
  • getting below error when trying to use above web service `java.net.UnknownHostException: Unable to resolve host "www.geoipservice.asmx": No address associated with hostname` – Android dev Jun 14 '16 at 05:36
  • Try this webservice http://ip-api.com/json/172.217.4.227 .. Use your IP instead of Google's .. ;) .. – prashant0205 Jun 14 '16 at 05:43
  • Thanks. I have tried above webservice but in response am getting below response `{"message":"private range","query":"My IP Address","status":"fail"} ` .What does mean by **private range** – Android dev Jun 14 '16 at 05:51
  • See this works for me ... [link](https://drive.google.com/open?id=0BxnXj5S9SPTGX1AyWnFoMjZ2YlE) – prashant0205 Jun 14 '16 at 06:06
  • Yes Its working with google IP ,but when i used mine IP address instead of google then its saying that **private range** .I think its the problem of wifi. Can you elaborate more the meaning of **private range** that when this comes – Android dev Jun 14 '16 at 06:13
  • @Android Do vote up and mark this as correct answer if it worked for you .. :) – prashant0205 Jun 14 '16 at 06:15
  • These are Private IP ranges 192.168.0.0 - 192.168.255.255 172.16.0.0 - 172.31.255.255 10.0.0.0 - 10.255.255.255 Visit [wikipedia](https://en.wikipedia.org/wiki/Private_network) for more info on Private IPs – prashant0205 Jun 14 '16 at 06:18
  • Create a free account on [Hostinger](http://www.hostinger.in/) or any similar and deploy your code there... And use the IP provided by Hostinger .. :) IF USING A WEBSERVICE ... or else use [Appetize](https://appetize.io/) to debug you Android App ... :) – prashant0205 Jun 14 '16 at 06:33
  • I am developing an android app .And phone is connected with office wifi , Finaaly i'm calling web service provided by you – Android dev Jun 14 '16 at 06:44
  • Go to Wi-Fi Setting in your phone... Open the Menu and Go to Advanced and check the IP Address there .. or if any trouble doing this download an app to find your current IP ... – prashant0205 Jun 14 '16 at 06:53
  • Hey i am able to find IP address ,IP address is not the problem ,I think Problem is that my wifi is not allowing to find the location.Or the problem may be the web service `ip-api.com/json/172.217.4.227` is not able to access my wifi IP address – Android dev Jun 14 '16 at 07:03
0

Try this code

code

This will give you longitude and latitude

Community
  • 1
  • 1
Vishwesh Jainkuniya
  • 2,821
  • 3
  • 18
  • 35