几个工具类

一:通过web获取所在的经纬度(注意这里有的台式机不支持,所以最好用手机访问):

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>

<body>

<p id="demo"></p>
<button onclick="getLocation()">获取经纬度</button>


</body>
<script>


var x=document.getElementById("demo");
var x2=document.getElementById("hideval");
function getLocation(){    //检测是否支持地理定位 如果支持,则运行 getCurrentPosition() 方法。如果不支持,则向用户显示一段消息。
  if (navigator.geolocation){//如果getCurrentPosition()运行成功, //则向参数showPosition中规定的函数返回一个coordinates对象
    navigator.geolocation.getCurrentPosition(showPosition);
  }else{
    x.innerHTML="地理位置浏览器支持";
  }
}

function showPosition(position){  //showPosition() 函数获得并显示经度和纬度
  x.innerHTML="纬度: " + position.coords.latitude + "<br />经度: " + position.coords.longitude;
  x2.innerHTML= +position.coords.latitude +","+position.coords.longitude;
}
</script>

</html>

二:通过IP获取所在的省市:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.nio.charset.Charset;
import org.json.JSONException;
import org.json.JSONObject;
/**通过ip获得位置
 * java根据url获取json对象
 * @author openks
 * @since 2013-7-16
 * 需要添加java-json.jar才能运行
 */
public class GetPlaceByIp {
  private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
      sb.append((char) cp);
    }
    return sb.toString();
  }
  public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
      BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
      String jsonText = readAll(rd);
      JSONObject json = new JSONObject(jsonText);
      return json;
    } finally {
      is.close();
     // System.out.println("同时 从这里也能看出 即便return了,仍然会执行finally的!");
    }
  }
  public static void main(String[] args) throws IOException, JSONException {
   //这里调用百度的ip定位api服务 详见 http://api.map.baidu.com/lbsapi/cloud/ip-location-api.htm
    JSONObject json = readJsonFromUrl("http://api.map.baidu.com/location/ip?ak=DdUThOBpfjwAzk30cl4mg3UmhqOvsG4i&ip=121.34.107.240");
    System.out.println(json.toString());
    System.out.println(((JSONObject) json.get("content")).get("address"));
  }
}

三:通过经纬度获取两点的距离:

/**   
 * Created by yuliang on 2015/3/20.   
 */    
public class LocationUtils {    
    private static double EARTH_RADIUS = 6378.137;    
    
    private static double rad(double d) {    
        return d * Math.PI / 180.0;    
    }    
    
    /**   
     * java根据经纬度坐标计算两点的距离算法
     * 通过经纬度获取距离(单位:米)   
     * @param lat1   
     * @param lng1   
     * @param lat2   
     * @param lng2   
     * @return   
     */    
    public static double getDistance(double lat1, double lng1, double lat2,    
                                     double lng2) {    
        double radLat1 = rad(lat1);    
        double radLat2 = rad(lat2);    
        double a = radLat1 - radLat2;    
        double b = rad(lng1) - rad(lng2);    
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)    
                + Math.cos(radLat1) * Math.cos(radLat2)    
                * Math.pow(Math.sin(b / 2), 2)));    
        s = s * EARTH_RADIUS;    
        s = Math.round(s * 10000d) / 10000d;    
        s = s*1000;    
        return s;    
    }    
    public static void main(String[] args) {
        System.out.println(getDistance(22.5535653864,114.0703272860,22.5665111688,114.0375015218));
    }
}    
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

public class localIp {
    /**
     * 获取本机Ip 
     *  
     *  通过 获取系统所有的networkInterface网络接口 然后遍历 每个网络下的InterfaceAddress组。
     *  获得符合 <code>InetAddress instanceof Inet4Address</code> 条件的一个IpV4地址
     * @return
     */
    @SuppressWarnings("rawtypes")
    private static String localIp(){
        String ip = null;
        Enumeration allNetInterfaces;
        try {
            allNetInterfaces = NetworkInterface.getNetworkInterfaces();            
            while (allNetInterfaces.hasMoreElements()) {
                NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
                List<InterfaceAddress> InterfaceAddress = netInterface.getInterfaceAddresses();
                for (InterfaceAddress add : InterfaceAddress) {
                    InetAddress Ip = add.getAddress();
                    if (Ip != null && Ip instanceof Inet4Address) {
                        ip = Ip.getHostAddress();
                    }
                }
            }
        } catch (SocketException e) {
        }
        return ip;
    }
    public static void main(String[] args) {
        String ip = localIp.localIp();
        System.out.println(ip);
    }
}
原文地址:https://www.cnblogs.com/XJJD/p/7792722.html