Advertisement
milo2012

Get Geocoordinates from Wifi Access Points

Feb 23rd, 2012
1,018
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.44 KB | None | 0 0
  1. import java.io.*;  
  2. import java.util.*;
  3. import java.net.*;
  4. import java.applet.*;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. public class getGPSLocation extends Applet{  
  9. //public class getGPSLocation{
  10.     //public static void main(String[] args) {
  11.     public void init(){
  12.         String result = null;
  13.         if (isWindows()) {
  14.             System.out.println("\nThis is Windows Machine");
  15.             result=getWindows();
  16.         } else if (isMac()) {
  17.             System.out.println("\nThis is Mac Machine");
  18.             result=getMac();
  19.         } else {
  20.             System.out.println("\nYour OS is not support!!");
  21.         }
  22.         //System.out.println(result);
  23.     }
  24.    
  25.     public static String getWindows(){
  26.             String result = null;
  27.             try {  
  28.            
  29.                 ArrayList ssidList = new ArrayList();
  30.                 ArrayList bssidList = new ArrayList();
  31.                 ArrayList rssiList = new ArrayList();
  32.            
  33.                 Process p = Runtime.getRuntime().exec("netsh wlan show networks mode=bssid");  
  34.                 BufferedReader in = new BufferedReader(  
  35.                                     new InputStreamReader(p.getInputStream()));  
  36.                 String line = null;  
  37.                 String signal = null;
  38.                 String ssidStr = null;
  39.                
  40.                 while ((line = in.readLine()) != null) {  
  41.    
  42.                     Pattern p1 = Pattern.compile("(SSID\\s\\d+\\s:)\\s([\\w\\s]*)");
  43.                     Matcher m1 = p1.matcher(line);
  44.                     if(m1.find()){
  45.                         ssidStr = m1.group(2);
  46.                         ssidStr = ssidStr.replaceAll(" ","%20");
  47.                         ssidList.add(ssidStr);
  48.                     }  
  49.                     Pattern p2 = Pattern.compile("(BSSID\\s1\\s*:)\\s((.?)*)");
  50.                     Matcher m2 = p2.matcher(line);
  51.                     if(m2.find()){
  52.                         bssidList.add(m2.group(2));
  53.                     }  
  54.                     Pattern p3 = Pattern.compile("(Signal\\s*):\\s((.?)*)");
  55.                     Matcher m3 = p3.matcher(line);
  56.                     if(m3.find()){
  57.                         signal = m3.group(2);
  58.                         signal = signal.replaceAll("%","");
  59.                         signal = signal.replaceAll(" ","");
  60.                         signal = "-"+signal;
  61.                         rssiList.add(signal);
  62.                     }                                          
  63.                 }
  64.                 int arraySize=ssidList.size();
  65.                 if(arraySize==0){
  66.                     result="I don't know where the target is";
  67.                 }
  68.                 else{
  69.                     result=googleLookup(bssidList,ssidList,rssiList);
  70.                 }
  71.              } catch (IOException e) {  
  72.                 e.printStackTrace();  
  73.             }
  74.         return result;          
  75.     }
  76.    
  77.     public static String googleLookup(ArrayList bssidList,ArrayList ssidList,ArrayList rssiList){
  78.         String result = null;
  79.         try {  
  80.             int j=0;
  81.             String queryString = "https://maps.googleapis.com/maps/api/browserlocation/json?browser=firefox&sensor=true";
  82.             while(j<ssidList.size()){
  83.                 queryString+="&wifi=mac:";
  84.                 queryString+=bssidList.get(j);
  85.                 queryString+="%7C";
  86.                
  87.                 queryString+="ssid:";
  88.                 queryString+=ssidList.get(j);
  89.            
  90.                 queryString+="%7C";
  91.                 queryString+="ss:";
  92.                 queryString+=rssiList.get(j);
  93.                 j++;
  94.             }
  95.                    
  96.             //Get geocoordinates / Longitude and Latitude
  97.             String geoCoordinates = null;      
  98.  
  99.             //System.out.println("Query string: "+queryString);
  100.  
  101.             URL url = new URL(queryString);
  102.             URLConnection urlc = url.openConnection();
  103.             urlc.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; "+ "Windows NT 5.1; en-US; rv:1.8.0.11) ");
  104.             BufferedReader reader = new BufferedReader(new InputStreamReader(urlc.getInputStream()));      
  105.             for (String output; (output = reader.readLine()) != null;) {
  106.                 if(output.indexOf("18000.0")>0){
  107.                     result+="Location is not accurate\n";
  108.                     System.out.println("Location is not accurate");
  109.                 }
  110.                 else{  
  111.                     if(output.indexOf("lat")>0){
  112.                         output = output.replace("\"lat\" : ","");
  113.                         output = output.replaceAll("^\\s+", "");
  114.                         geoCoordinates = output;
  115.                         result+="Latitude: ";
  116.                         result+=output;
  117.                         System.out.println("Latitude: "+output);
  118.                     }
  119.                     if(output.indexOf("lng")>0){
  120.                         output = output.replace("\"lng\" : ","");
  121.                         output = output.replaceAll("^\\s+", "");
  122.                         geoCoordinates += output;
  123.                         result+="Longitude: ";
  124.                         result+=output;
  125.                         System.out.println("Longitude: "+output);
  126.                     }
  127.                 }
  128.                
  129.             }
  130.            
  131.        
  132.             //Reverse geocoordinates to street address
  133.             String reverseGeo = "https://maps.googleapis.com/maps/geo?q="+geoCoordinates+"&output=json&sensor=true_or_false";
  134.            
  135.             System.out.println(reverseGeo);
  136.  
  137.                 URL url1 = new URL(reverseGeo);
  138.                 URLConnection urlc1 = url1.openConnection();
  139.                 urlc1.setRequestProperty("User-Agent", "Mozilla 5.0 (Windows; U; "+ "Windows NT 5.1; en-US; rv:1.8.0.11) ");
  140.             BufferedReader reader1 = new BufferedReader(new InputStreamReader(urlc1.getInputStream()));    
  141.             for (String output1; (output1 = reader1.readLine()) != null;) {
  142.                 if(output1.indexOf("address")>0){
  143.                     output1 = output1.replace("\"address\": ","");
  144.                     output1 = output1.replace("\",","");
  145.                     output1 = output1.replace("\"","");
  146.                     output1 = output1.replaceAll("^\\s+", "");
  147.                     result+="Address is ";
  148.                     result+=output1;
  149.                     System.out.println("Address is "+output1);
  150.                 }
  151.             }      
  152.             String mapAddress = "http://maps.google.com/maps?q="+geoCoordinates+"+%28You+are+located+here%29&iwloc=A&hl=en";
  153.             result+="\n"+mapAddress;
  154.             System.out.println("\n"+mapAddress);
  155.         } catch (IOException e) {
  156.             e.printStackTrace();               
  157.         }
  158.         return result;     
  159.     }
  160.    
  161.     public static String getMac(){
  162.         String result = null;  
  163.         try {  
  164.                 Process p = Runtime.getRuntime().exec("/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s");  
  165.                 BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));  
  166.                 String line = null;  
  167.                 String ssidStr = null;
  168.                 String signal = null;
  169.                
  170.                 String queryString = "https://maps.googleapis.com/maps/api/browserlocation/json?browser=firefox&sensor=true";
  171.        
  172.                 ArrayList ssidList = new ArrayList();
  173.                 ArrayList bssidList = new ArrayList();
  174.                 ArrayList rssiList = new ArrayList();
  175.  
  176.                 line = in.readLine();  
  177.                 while ((line = in.readLine()) != null) {  
  178.                     line = line.replaceAll("^\\s+", "");
  179.                    
  180.                     //Pattern p1 = Pattern.compile("((.?)*\\s\\w*):(\\w*:\\w*:\\w*:\\w*:\\w*)\\s((.?)*)\\s(\\d+)");
  181.                     Pattern p1 = Pattern.compile("((.?)+)\\s(..:..:..:..:..:..)\\s(-\\d*)");
  182.                     Matcher m1 = p1.matcher(line);
  183.                     if(m1.find()){
  184.                         ssidStr = m1.group(1);
  185.                         ssidStr = ssidStr.replaceAll(" ","%20");
  186.                         ssidList.add(ssidStr);
  187.                    
  188.                         //System.out.println("ssid: "+ssidStr);
  189.  
  190.                         bssidList.add(m1.group(3));
  191.                         //System.out.println("bssid: "+m1.group(3));
  192.  
  193.                         signal = m1.group(4);
  194.                         signal = signal.replaceAll(" ","");
  195.  
  196.                         //System.out.println("signal: "+signal);
  197.                         rssiList.add(signal);
  198.                     }
  199.                    
  200.                 }
  201.             int arraySize=ssidList.size();
  202.             if(arraySize==0){
  203.                 result="I don't know where the target is";
  204.                 System.out.println("I don't know where the target is");
  205.             }
  206.             else{
  207.                 result=googleLookup(bssidList,ssidList,rssiList);
  208.  
  209.             }
  210.         } catch (IOException e) {  
  211.             e.printStackTrace();  
  212.         }  
  213.         return result;     
  214.     }  
  215.  
  216.     public static boolean isWindows() {
  217.  
  218.         String os = System.getProperty("os.name").toLowerCase();
  219.         // windows
  220.         return (os.indexOf("win") >= 0);
  221.  
  222.     }
  223.  
  224.     public static boolean isMac() {
  225.  
  226.         String os = System.getProperty("os.name").toLowerCase();
  227.         // Mac
  228.         return (os.indexOf("mac") >= 0);
  229.  
  230.     }
  231.  
  232.     public static boolean isLinux() {
  233.  
  234.         String os = System.getProperty("os.name").toLowerCase();
  235.         // linux or unix
  236.         return (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0);
  237.  
  238.     }
  239.  
  240.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement