首先在腾讯地图上注册/登录
然后申请KEY:
https://lbs.qq.com/console/mykey.html?console=mykey
进去了后点击创建新密钥,把该填的填一下(注意下面几项一定要勾选,如果没有勾选,可以后面再我的key中点击设置更改)
我的是使用授权IP方式访问,授权IP不填写则为全部IP都可以访问
前台代码
<script>
function getIntAndLat(){
var addr = getaddr();
$.ajax({
url: "",
type : "POST",
data : {
address : addr
},
success : function (data) {
console.log('===>>> 查询地址信息:'+ JSON.parse(data));
console.log("===>>> status:"+JSON.parse(data).status);
var data1 = JSON.parse(data);
var result = JSON.parse(data).result;
console.log("===>>> result:"+JSON.stringify(result.location));
if(data1.status == 0){
console.log("===>>> 坐标:"+ JSON.stringify(result.location.lng+','+result.location.lat));
//获取经纬度填充
//经度
$("#lng").val(JSON.stringify(result.location.lng));
//纬度
$("#lat").val(JSON.stringify(result.location.lat));
}
},
error: function () {
}
});
}
</script>
自己根据情况编写controller即可
@ResponseBody
@RequestMapping("getIngAndLat")
public String getIngAndLat(String address){
System.out.println("------------- 经纬度查询 -------------");
String Key = "填写你申请的KEY";
StringBuffer sb = new StringBuffer("https://apis.map.qq.com/ws/geocoder/v1/?");
sb.append("address="+address);
sb.append("&key="+Key);
System.out.println("===>>> 查询链接:"+sb.toString());
return HttpRequestUtils.getURLContent(sb.toString());
}
后台处理
public static String getURLContent(String urlStr) {
//请求的url
URL url = null;
//请求的输入流
BufferedReader in = null;
//输入流的缓冲
StringBuffer sb = new StringBuffer();
try{
url = new URL(urlStr);
in = new BufferedReader(new InputStreamReader(url.openStream(),"UTF-8") );
String str = null;
//一行一行进行读入
while((str = in.readLine()) != null) {
sb.append( str );
}
} catch (Exception ex) {
} finally{
try{
if(in!=null) {
in.close(); //关闭流
}
}catch(IOException ex) {
}
}
String result =sb.toString();
return result;
}
public static void main(String[] args) {
String s = getURLContent("https://apis.map.qq.com/ws/geocoder/v1/?address=北京市&key=1231313");
System.out.println(s);
}