java 获取本机经纬度

package com.smm.web.mtower.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

/**
 * Created by zhenghao on 2016/6/24.
 * 当前位置处理工具
 */
public class LocationUtil {

    /**
     * 获取指定IP对应的经纬度(为空返回当前机器经纬度)
     * @return
     */
    public static String[] getIPXY() {

        //获取本机公网ip
        String ip = getPublicWebIP();

        if(ip==null || ip.trim().equals("")) return null;

        //百度map ak
        String ak = "TjoQT*****************Bj8jV";

        try {

            URL url = new URL("http://api.map.baidu.com/location/ip?ak=" + ak + "&ip=" + ip + "&coor=bd09ll");
            InputStream inputStream = url.openStream();
            InputStreamReader inputReader = new InputStreamReader(inputStream);
            BufferedReader reader = new BufferedReader(inputReader);
            StringBuilder sb = new StringBuilder();
            String str;

            do {
                str = reader.readLine();
                sb.append(str);
            } while (null != str);


            str = sb.toString();

            if (str.isEmpty()) {
                return null;
            }


            // 获取坐标位置
            int index = str.indexOf("point");
            int end = str.indexOf("}}", index);


            if (index == -1 || end == -1) {
                return null;
            }


            str = str.substring(index - 1, end + 1);
            if (str.isEmpty()) {
                return null;
            }


            String[] ss = str.split(":");
            if (ss.length != 4) {
                return null;
            }


            String x = ss[2].split(",")[0];
            String y = ss[3];


            x = x.substring(x.indexOf("\"") + 1, x.indexOf("\"", 1));
            y = y.substring(y.indexOf("\"") + 1, y.indexOf("\"", 1));


            return new String[] { x, y };


        } catch (IOException e) {
            e.printStackTrace();
        }


        return null;
    }

    public static String getPublicWebIP() {

        String http_url ="http://www.ip.cn/";

        try{

            URL url = new URL(http_url);
            InputStream inputStream = url.openStream();
            InputStreamReader inputReader = new InputStreamReader(inputStream);
            BufferedReader reader = new BufferedReader(inputReader);
            StringBuilder sb = new StringBuilder();
            String str;

            do {
                str = reader.readLine();
                sb.append(str);
            } while (null != str);


            str = sb.toString();

            if (str.isEmpty()) {
                return null;
            }


            //从内容中截取代码 <code>117.184.120.234</code>

            int start = str.indexOf("<code>") + "<code>".length();

            int end = str.indexOf("</code>");

            str = str.substring(start,end);

            return str;


        }catch (Exception e){

            e.printStackTrace();
        }

        return null;
    }


    public static void main(String[] args) {

        String [] location = LocationUtil.getIPXY();

        assert location != null;

        String widu=location[1];
        String jndu=location[0];

        System.out.println(widu);

        System.out.println(jndu);

    }

}

转载于:https://my.oschina.net/zhenghao/blog/700352

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用Java API for Location Service (JSR-179)获取本机经纬度信息。以下是获取经纬度的基本步骤: 1. 导入JSR-179库: ```java import javax.microedition.location.*; ``` 2. 创建定位服务对象: ```java LocationProvider provider = LocationProvider.getInstance(null); ``` 3. 获取位置: ```java Location location = provider.getLocation(-1); ``` `getLocation()`方法的参数表示获取位置的超时时间,如果为-1则表示一直等待直到获取到位置信息。 4. 获取经纬度: ```java Coordinates coordinates = location.getQualifiedCoordinates(); double latitude = coordinates.getLatitude(); double longitude = coordinates.getLongitude(); ``` `getLatitude()`和`getLongitude()`方法分别返回纬度和经度信息。 完整的示例代码如下: ```java import javax.microedition.location.*; public class LocationDemo { public static void main(String[] args) { try { LocationProvider provider = LocationProvider.getInstance(null); Location location = provider.getLocation(-1); Coordinates coordinates = location.getQualifiedCoordinates(); double latitude = coordinates.getLatitude(); double longitude = coordinates.getLongitude(); System.out.println("Latitude: " + latitude); System.out.println("Longitude: " + longitude); } catch (Exception e) { e.printStackTrace(); } } } ``` 需要注意的是,上述代码只能在支持JSR-179的J2ME环境中运行。在标准Java SE环境中,可以使用第三方库或者调用操作系统的API获取位置信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值