地址解析经纬度

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.util.HashMap;

import java.util.Map;


import org.apache.commons.lang.StringUtils;

//开启地址解析

public static final String KEY_1 = "百度云申请key";

    /**
     * 返回输入地址的经纬度坐标
     * key lng(经度),lat(纬度)
     */
    public static Map<String,String> getGeocoderLatitude(String address){
        BufferedReader in = null;
        try {
            //将地址转换成utf-8的16进制
            address = URLEncoder.encode(address, "UTF-8");
          URL tirc = new URL("http://api.map.baidu.com/geocoder?address="+ address +"&output=json&key="+ KEY_1);
 
 
            in = new BufferedReader(new InputStreamReader(tirc.openStream(),"UTF-8"));
            String res;
            StringBuilder sb = new StringBuilder("");
            while((res = in.readLine())!=null){
                sb.append(res.trim());
            }
            String str = sb.toString();
            Map<String,String> map = null;
            if(StringUtils.isNotEmpty(str)){
                int lngStart = str.indexOf("lng\":");
                int lngEnd = str.indexOf(",\"lat");
                int latEnd = str.indexOf("},\"precise");
                if(lngStart > 0 && lngEnd > 0 && latEnd > 0){
                    String lng = str.substring(lngStart+5, lngEnd);
                    String lat = str.substring(lngEnd+7, latEnd);
                    map = new HashMap<String,String>();
                    map.put("lng", lng);
                    map.put("lat", lat);
                    return map;
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
        try {
                 in.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
         return null;
     }
你可以使用高德地图的 JavaScript API 中的 `AMap.Geocoder` 类来实现地址解析经纬度。 首先,在你的 Nuxt.js 项目中安装高德地图的 JavaScript API: ```bash npm install @amap/amap-jsapi-loader ``` 然后,在你的 Vue 组件中引入并使用 `AMap.Geocoder` 类: ```vue <template> <div> <input type="text" v-model="address" /> <button @click="search">搜索</button> <div v-if="latitude && longitude"> 纬度:{{ latitude }} 经度:{{ longitude }} </div> </div> </template> <script> import AMapLoader from '@amap/amap-jsapi-loader'; export default { data() { return { address: '', latitude: null, longitude: null, }; }, methods: { async search() { const AMap = await AMapLoader.load({ key: 'YOUR_AMAP_API_KEY', version: '2.0', }); const geocoder = new AMap.Geocoder({ city: '全国', }); geocoder.getLocation(this.address, (status, result) => { if (status === 'complete' && result.geocodes.length) { const { lat, lng } = result.geocodes[0].location; this.latitude = lat; this.longitude = lng; } else { console.error('解析地址失败'); } }); }, }, }; </script> ``` 这里使用了 `@amap/amap-jsapi-loader` 包来动态加载高德地图的 JavaScript API,然后通过 `AMap.Geocoder` 类的 `getLocation` 方法将地址解析经纬度,并将结果存储在组件的 `latitude` 和 `longitude` 数据中。需要注意的是,在 `AMap.Geocoder` 的构造函数中需要传入一个 `city` 参数,表示解析地址时所在的城市,这里使用了 `全国` 表示在全国范围内解析地址。如果你知道要解析地址所在的城市,可以将其传入该参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值