百度地图地址提示api调用实例(附完整代码)
程序员文章站
2022-07-03 18:13:03
...
最近在做关于在线回收资源的项目,其中需要用户填写地址,所以就打算用百度地图的第三方接口,输入提示功能。采用java语言编写后台。先来看个效果图吧。
下面看一下前端代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript" src="js/jquery-3.2.1.min.js" ></script>
<script>
$(document).ready(function(){
$("#query").keydown(function(){
$(".addresstop").empty();
$.post("Servletapi",
{
region:$("#hidden1_region").val(),
query:$("#query").val()
},
function(result){
var jsobject = JSON.parse(result);
for (var i = 0; i < jsobject.result.length; i++) {
$(".addresstop").append("<div class='addressli'>"
+"<div class='addressli1'>"+jsobject.result[i].name+"</div>"
+"<div class='addressli2'>"+jsobject.result[i].city+""+jsobject.result[i].district+"</div></div>");
}
});
});
});
</script>
<input type="text" id="query"><input type="text" value="北京" id="hidden1_region">
<div class="addresstop">
</div>
</body>
</html>
后台servlet
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
Url url = new Url();
String query = URLEncoder.encode(request.getParameter("query"), "UTF-8");
String region = URLEncoder.encode(request.getParameter("region"), "UTF-8");
String requestUrl = "http://api.map.baidu.com/place/v2/suggestion?region=REGION&query=QUERY&city_limit=true&output=json&ak=你的ak";
requestUrl = requestUrl.replace("QUERY",query).replace("REGION",region);
JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST","");
System.out.println(jsonObject);
PrintWriter out = response.getWriter();
out.print(jsonObject);
out.close();
}
工具类:
package until;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.alibaba.fastjson.JSONObject;
public class CommonUtil {
/*
* 发送https请求
* @param requestUrl 请求地址
* @param requestMethod 请求方法
* @param JSONObject 解析获取的json数据
*
* */
public static JSONObject httpsRequest(String requestUrl,String requestMethod,String outputStr){
JSONObject jsonObject = null;
//创建SSLContext对象
//--
try {
URL url = new URL(requestUrl);
HttpURLConnection conn;
try {
conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(true);
//设置请求方式
conn.setRequestMethod(requestMethod);
if(null!=outputStr){
OutputStream outputStream = conn.getOutputStream();
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
//从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream,"utf-8");
BufferedReader bufferedReadr = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while((str = bufferedReadr.readLine())!=null){
buffer.append(str);
}
bufferedReadr.close();
inputStream.close();
inputStreamReader.close();
inputStream = null;
conn.disconnect();
jsonObject = JSONObject.parseObject(buffer.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonObject;
}
}
jquery那个包比较好找我就不说了
fastjson那个包可以在我的另一篇博客下载下载地址链接
整体思路就是通过jq keydown方法不断向后台传输所输入的地址,进而查询数据。
这里面有一点稍微有点麻烦,就是json串传到前端的时候的进行数据提取。大家可以自己多尝试尝试,一点也不难。
我已经把项目打包传上去了,大家可以进行下载。
不过注意的是填上你的ak码。
csdn下载
上一篇: Spring Cloud Config