Java开发导入腾讯地图描点_JAVA 对接腾讯地图,经纬度转换

package com.lvjing.util;

import net.sf.json.JSONArray;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Service;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.HashMap;

import java.util.Map;

/**

* \* Created with IntelliJ IDEA.

* \* User: 46512

* \* Author:ZY

* \* Date: 2018/7/24

* \* To change this template use File | Settings | File Templates.

* \* Description:

* \

*/

@Service

public class GPSTranslate {

// key,需要在腾讯地图申请一个KEY

private static final String KEY = "";

/**

* @Description: 通过经纬度获取位置

* @Param: [log, lat]

* @return: java.lang.String

* @Author: zy

* @Date: 2018/7/24

*/

public static Map getLocation(String lng, String lat) {

Map resultMap = new HashMap();

// 参数解释:lng:经度,lat:维度。KEY:腾讯地图key

String urlString = "http://apis.map.qq.com/ws/coord/v1/translate?locations=" +

lat + "," + lng +"&type=1"+"&key=" + KEY ;

String result = "";

try {

URL url = new URL(urlString);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setDoOutput(true);

// 腾讯地图使用GET

conn.setRequestMethod("GET");

BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

String line;

// 获取地址解析结果

while ((line = in.readLine()) != null) {

result += line + "\n";

//System.out.println(result);

}

in.close();

} catch (Exception e) {

e.getMessage();

}

// 转JSON格式

JSONObject jsonObject = JSONObject.fromObject(result);

//System.out.println(jsonObject);

// 获取地址(行政区划信息) 包含有国籍,省份,城市

JSONObject a = new JSONObject();

//String code = jsonObject.getString("locations");

String locations = jsonObject.get("locations").toString();

//System.out.println(locations);

JSONArray jsonArray = jsonObject.getJSONArray("locations");

//System.out.println(jsonArray.getJSONObject(0).get("lat"));

String lat_result = jsonArray.getJSONObject(0).get("lat").toString();

String lng_result = jsonArray.getJSONObject(0).get("lng").toString();

resultMap.put("lat", lat_result);

resultMap.put("lng", lng_result);

return resultMap;

}

public static void main(String[] args) {

// 测试

String lat = "30.287574000";//维度

String lng = "120.07875200";//经度

Map map = getLocation(lng, lat);

System.out.println(map);

System.out.println("la:" + map.get("lat"));

System.out.println("lo:" + map.get("lng"));

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值