2014-06-23 26 views
0

下面的代碼是返回100.78.162.xxx IP,我想知道我們如何可以確定該IP的位置國家在PHP或Android它自己。任何幫助?如何檢查我的移動IP地址

public static String getIPAddress(boolean useIPv4) { 
     try { 
      List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); 
      for (NetworkInterface intf : interfaces) { 
       List<InetAddress> addrs = Collections.list(intf.getInetAddresses()); 
       for (InetAddress addr : addrs) { 
        if (!addr.isLoopbackAddress()) { 
         String sAddr = addr.getHostAddress().toUpperCase(); 
         boolean isIPv4 = InetAddressUtils.isIPv4Address(sAddr); 
         if (useIPv4) { 
          if (isIPv4) 
           return sAddr; 
         } else { 
          if (!isIPv4) { 
           int delim = sAddr.indexOf('%'); // drop ip6 port suffix 
           return delim<0 ? sAddr : sAddr.substring(0, delim); 
          } 
         } 
        } 
       } 
      } 
     } catch (Exception ex) { } // for now eat exceptions 
     return ""; 
    } 
String ip = Utils.getIPAddress(true); 
+0

您需要一個將IP映射到國家/地區的數據庫。這裏的關鍵字是「GeoIP」。只是谷歌它... – bidifx

+0

請看看下面類似的問題: [http://stackoverflow.com/questions/7766978/geo-location-based-on-ip-address-php][1] [1]:http://stackoverflow.com/questions/7766978/geo-location-based-on-ip-address-php – Tikky

+0

這可能是有用的:http://stackoverflow.com/questions/6437172/how-知道地理位置的IP地址 – Badrul

回答

0

您可以使用服務,如ip2location獲得基於ipAddress.Its最好是在服務器端結合的位置,並使用一個Web服務公開此服務。或者您可以使用location_api等服務直接從其他服務中使用服務。

注意:您也可以使用GPS信號(如果您可以使用此信號)準確獲取位置。