Java 汉字拆分转为拼音 及根据经纬度获取所在位置

Java 汉字拆分转为拼音 及根据经纬度获取所在位置

java网络代码拼凑人

package com.ruoyi.hfiveinterface.util;

import com.alibaba.fastjson.JSONObject;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.junit.Test;
import org.springframework.stereotype.Service;

import java.net.URL;


@Service
public class ChineseToPinyinUtil {


    /**
     * 汉字转为拼音
     * @param chinese
     * @return
     */
    public  String toPinyin(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0];
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr;
    }


    /**
     * 获取字符串拼音的第一个字母
     * @param chinese
     * @return
     */
    public  String getFirstCharUpperCase(String chinese){
        String pinyinStr = "";
        char[] newChar = chinese.toCharArray();  //转为单个字符
        HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
        defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        for (int i = 0; i < newChar.length; i++) {
            if (newChar[i] > 128) {
                try {
                    pinyinStr += PinyinHelper.toHanyuPinyinStringArray(newChar[i], defaultFormat)[0].charAt(0);
                } catch (BadHanyuPinyinOutputFormatCombination e) {
                    e.printStackTrace();
                }
            }else{
                pinyinStr += newChar[i];
            }
        }
        return pinyinStr.substring(0, 1).toUpperCase();
    }

    /**

     * 得到汉字首字母的拼音

     * @param str

     * @return 拼音首字母(大写)

     */

    public static String getPinYinHeaderChar(String str){
        String convert="";

        for ( int i = 0; i < str.length(); i++ ) {
            char word=str.charAt(i);

            String[] pinYinArray=PinyinHelper.toHanyuPinyinStringArray(word);

            if ( pinYinArray!=null ){
                convert+=pinYinArray[0].charAt(0);

            }else {
                convert+=word;

            }

        }
        return convert.toUpperCase();
    }


    /**
     * 根据经纬度 获取 用户的具体位置
     * @param lat
     * @param log
     * @return
     */
    public String getAdd(String lat, String log) {
        // log lat
        // 参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
        //String urlString = "http://gc.ditu.aliyun.com/regeocoding?l=" + lat + "," + log + "&type=010";
        String urlString = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=ZyCkKVoOyVa8vemSLStGV39XicQj4uAd&output=json&coordtype=wgs84ll&location=" + lat + "," + log.trim();
        String res = "";
        String formatted_address = "";
        try {
            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line + "\n";
            }
            in.close();
//            {"status":0,"result":{"location":{"lng":121.50989050677055,"lat":31.229327880651959},"formatted_address":"上海市黄浦区中山南路187","business":"外滩,陆家嘴,董家渡","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"上海市","city":"上海市","city_level":2,"district":"黄浦区","town":"","town_code":"","adcode":"310101","street":"中山南路","street_number":"187","direction":"东北","distance":"91"},"pois":[],"roads":[],"poiRegions":[],"sematic_description":"","cityCode":289}}
            JSONObject jsStr = JSONObject.parseObject(res); //将字符串{“id”:1}

//            JSONArray jsonArray = jsStr.getJSONArray("HeWeather6");
            String result = jsStr.getString("result");
//            System.out.println(result);
            JSONObject resultjsStr = JSONObject.parseObject(result);
            formatted_address = resultjsStr.getString("formatted_address");
//            System.out.println(formatted_address);
            //int jsID = Integer.parseInt(jsStr.getString("id"));//获取id的值
        } catch (Exception e) {
            System.out.println("error in wapaction,and e is " + e.getMessage());
        }
        return formatted_address;
    }


    /**
     * 根据经纬度 获取 城市 的数据
     * @param lat
     * @param log
     * @return
     */
    public static String getLocation(String lat, String log) {
        // log lat
        // 参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
        //String urlString = "http://gc.ditu.aliyun.com/regeocoding?l=" + lat + "," + log + "&type=010";
        String urlString = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=ZyCkKVoOyVa8vemSLStGV39XicQj4uAd&output=json&coordtype=wgs84ll&location=" + lat + "," + log.trim();
        String res = "";
        String formatted_address = "";
        try {
            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line + "\n";
            }
            in.close();
//            {"status":0,"result":{"location":{"lng":121.50989050677055,"lat":31.229327880651959},"formatted_address":"上海市黄浦区中山南路187","business":"外滩,陆家嘴,董家渡","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"上海市","city":"上海市","city_level":2,"district":"黄浦区","town":"","town_code":"","adcode":"310101","street":"中山南路","street_number":"187","direction":"东北","distance":"91"},"pois":[],"roads":[],"poiRegions":[],"sematic_description":"","cityCode":289}}
            JSONObject jsStr = JSONObject.parseObject(res); //将字符串{“id”:1}

//            JSONArray jsonArray = jsStr.getJSONArray("HeWeather6");
            String result = jsStr.getString("result");
//            System.out.println(result);
            JSONObject resultjsStr = JSONObject.parseObject(result);//
            //System.out.println("打印字符串"+resultjsStr);
            String locationInfo=resultjsStr.getString("addressComponent");
            //System.out.println("打印返回结果详细信息"+locationInfo);
            JSONObject jsonObjLocation = JSONObject.parseObject(locationInfo);
            //System.out.println(jsonObjLocation);
            formatted_address = jsonObjLocation.getString("city");
//            System.out.println(formatted_address);
            //int jsID = Integer.parseInt(jsStr.getString("id"));//获取id的值
        } catch (Exception e) {
            System.out.println("error in wapaction,and e is " + e.getMessage());
        }
        return formatted_address;
    }

    /**
     * 根据经纬度 获取 省份 和 城市 的数据
     * @param lat
     * @param log
     * @return
     */
    public String getProvinceAndCity(String lat, String log) {
        // log lat
        // 参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
        //String urlString = "http://gc.ditu.aliyun.com/regeocoding?l=" + lat + "," + log + "&type=010";
        String urlString = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=ZyCkKVoOyVa8vemSLStGV39XicQj4uAd&output=json&coordtype=wgs84ll&location=" + lat + "," + log.trim();
        String res = "";
        String formatted_address = "";
        try {
            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line + "\n";
            }
            in.close();
//            {"status":0,"result":{"location":{"lng":121.50989050677055,"lat":31.229327880651959},"formatted_address":"上海市黄浦区中山南路187","business":"外滩,陆家嘴,董家渡","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"上海市","city":"上海市","city_level":2,"district":"黄浦区","town":"","town_code":"","adcode":"310101","street":"中山南路","street_number":"187","direction":"东北","distance":"91"},"pois":[],"roads":[],"poiRegions":[],"sematic_description":"","cityCode":289}}
            JSONObject jsStr = JSONObject.parseObject(res); //将字符串{“id”:1}

//            JSONArray jsonArray = jsStr.getJSONArray("HeWeather6");
            String result = jsStr.getString("result");
//            System.out.println(result);
            JSONObject resultjsStr = JSONObject.parseObject(result);//
            //System.out.println("打印字符串"+resultjsStr);
            String locationInfo=resultjsStr.getString("addressComponent");
            //System.out.println("打印返回结果详细信息"+locationInfo);
            JSONObject jsonObjLocation = JSONObject.parseObject(locationInfo);
            //System.out.println("打印省市区返回的json字符串" + jsonObjLocation);


            String province = jsonObjLocation.getString("province");
            String city = jsonObjLocation.getString("city");
            formatted_address =province+ ","+city;
//            System.out.println(formatted_address);
            //int jsID = Integer.parseInt(jsStr.getString("id"));//获取id的值
        } catch (Exception e) {
            System.out.println("error in wapaction,and e is " + e.getMessage());
        }
        return formatted_address;
    }


    /**
     * 根据经纬度 获取 省份 和 城市 和具体城市信息 的数据
     * @param lat
     * @param log
     * @return
     */
    public String getProvinceAndCityAndInfoAddress(String lat, String log) {
        // log lat
        // 参数解释: 纬度,经度 type 001 (100代表道路,010代表POI,001代表门址,111可以同时显示前三项)
        //String urlString = "http://gc.ditu.aliyun.com/regeocoding?l=" + lat + "," + log + "&type=010";
        String urlString = "http://api.map.baidu.com/reverse_geocoding/v3/?ak=ZyCkKVoOyVa8vemSLStGV39XicQj4uAd&output=json&coordtype=wgs84ll&location=" + lat + "," + log.trim();
        String res = "";
        String formatted_address = "";
        try {
            URL url = new URL(urlString);
            java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(conn.getInputStream(), "UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                res += line + "\n";
            }
            in.close();
//            {"status":0,"result":{"location":{"lng":121.50989050677055,"lat":31.229327880651959},"formatted_address":"上海市黄浦区中山南路187","business":"外滩,陆家嘴,董家渡","addressComponent":{"country":"中国","country_code":0,"country_code_iso":"CHN","country_code_iso2":"CN","province":"上海市","city":"上海市","city_level":2,"district":"黄浦区","town":"","town_code":"","adcode":"310101","street":"中山南路","street_number":"187","direction":"东北","distance":"91"},"pois":[],"roads":[],"poiRegions":[],"sematic_description":"","cityCode":289}}
            JSONObject jsStr = JSONObject.parseObject(res); //将字符串{“id”:1}

//            JSONArray jsonArray = jsStr.getJSONArray("HeWeather6");
            String result = jsStr.getString("result");
//            System.out.println(result);
            JSONObject resultjsStr = JSONObject.parseObject(result);//
            //System.out.println("打印字符串"+resultjsStr);
            String locationInfo=resultjsStr.getString("addressComponent");
            //System.out.println("打印返回结果详细信息"+locationInfo);
            JSONObject jsonObjLocation = JSONObject.parseObject(locationInfo);
            //System.out.println("打印省市区返回的json字符串" + jsonObjLocation);


            String province = jsonObjLocation.getString("province");
            String city = jsonObjLocation.getString("city");
            String district = jsonObjLocation.getString("district");
            String street = jsonObjLocation.getString("street");
            System.out.println(district + street);
            //"district":"黄浦区","town":"","town_code":"","adcode":"310101","street":"中山南路"
            formatted_address =province+ ","+city+","+district+street;
//            System.out.println(formatted_address);
            //int jsID = Integer.parseInt(jsStr.getString("id"));//获取id的值
        } catch (Exception e) {
            System.out.println("error in wapaction,and e is " + e.getMessage());
        }
        return formatted_address;
    }



}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值