如何用IP查询我的移动位置


How to check my mobile locality with IP

下面的代码是返回100.78.162.xxx 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);

您可以使用ip2location之类的服务来获取基于ipAddress的位置。最好将其合并到服务器端,并使用web服务公开此服务。或者你可以使用像location_api这样的服务直接从其他服务中消费。

注意:你也可以使用GPS信号(如果你被允许使用)来获得准确的位置。