使用百度地图API 逆地址解析 。数据库中查询出地址,调用百度API解析
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=XWV87PbnVsMGnrRU3nEBRvnG"></script>
public class BaiduMapCarController { @Autowired private CommonShopMapper commonShopMapper; @RequestMapping("/carmap") public ModelAndView print(HttpServletRequest request, HttpServletResponse response, LoginForm command) throws UnsupportedEncodingException { ModelAndView view = null; List<BdUserListBean> bdUserListBean = new ArrayList<BdUserListBean>(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); request.setCharacterEncoding("utf-8"); try { HashMap<String, Object> hashMap = new HashMap<String, Object>(); // hashMap.put("dir", URLEncoder.encode(command.getDirectory(), "UTF-8")); if(command.getId()==null){ hashMap.put("dir", "黄浦"); } else{ /**嘉定区 奉贤区 宝山区 徐汇区 普陀区 杨浦区 松江区 浦东区 虹口区 金山区 长宁区 闵行区 闸北区 青浦区 静安区 黄浦区 */ int key =Integer.parseInt(command.getId()); String dir="嘉定区"; switch (key) { case 1: dir="嘉定区"; break; case 2: dir="奉贤区"; break; case 3: dir="宝山区"; break; case 4: dir="嘉定区"; break; case 5: dir="徐汇区"; break; case 6: dir="普陀区"; break; case 7: dir="杨浦区"; break; case 8: dir="松江区"; break; case 9: dir="浦东区"; break; case 10: dir="虹口区"; break; case 11: dir="金山区"; break; case 12: dir="长宁区"; break; case 13: dir="闵行区"; break; case 14: dir="闸北区"; break; case 15: dir="青浦区"; break; case 16: dir="静安区"; break; case 17: dir="黄浦区"; break; } hashMap.put("dir", dir); } List<CommonShop> commonShopList = commonShopMapper.selectCommonShopList(hashMap);//总的维修站 StringBuffer sb = new StringBuffer(); StringBuffer sl = new StringBuffer(); if (!commonShopList.isEmpty()) { int size = commonShopList.size(); if(size>1000){ size=1000; } sb.append("["); sl.append("["); for (int i = 0; i < size; i++) { sb.append("\""); sb.append(commonShopList.get(i).getDir()); sb.append(commonShopList.get(i).getAddress()); sb.append("\""); sl.append("new BMap.Point("); sl.append(commonShopList.get(i).getLng()); sl.append(","); sl.append(commonShopList.get(i).getLat()); sl.append(")"); if (i == size - 1) { sb.append(""); } else { sb.append(","); } if (i == size - 1) { sl.append(""); } else { sl.append(","); } } sb.append("]"); sl.append("]"); } System.out.println("----w---" + sb.toString()); view = new ModelAndView("/carmap", "command", sb.toString()); System.out.println("----l---" +sl.toString()); view.addObject("command1", sl.toString()); } catch (Exception e) { e.printStackTrace(); } return view; } }
jsp:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <%--<!DOCTYPE html>--%> <%--<html>--%> <%--<head>--%> <%--<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />--%> <%--<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />--%> <%--<style type="text/css">--%> <%--body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}--%> <%--</style>--%> <%--<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=XWV87PbnVsMGnrRU3nEBRvnG"></script>--%> <%--<title>嘀嘀配件商户分布</title>--%> <%--</head>--%> <%--<body>--%> <%--<div id="allmap"></div>--%> <%--<input id="datamap" value="${command}"/>--%> <%--</body>--%> <%--</html>--%> <%--<script type="text/javascript">--%> <%--// 百度地图API功能--%> <%--var map = new BMap.Map("allmap"); // 创建Map实例--%> <%--// var point = new BMap.Point(121.337035, 31.129651);--%> <%--// var marker = new BMap.Marker(point); // 创建标注--%> <%--// map.addOverlay(marker); // 将标注添加到地图中--%> <%--// map.centerAndZoom(point, 15);--%> <%--// var opts = {--%> <%--// width : 200, // 信息窗口宽度--%> <%--// height: 100, // 信息窗口高度--%> <%--// title : "维修" , // 信息窗口标题--%> <%--// enableMessage:true,//设置允许信息窗发送短息--%> <%--// message:""--%> <%--// }--%> <%--// var infoWindow = new BMap.InfoWindow("地址:上海市松江区沪亭南路234号", opts); // 创建信息窗口对象--%> <%--// marker.addEventListener("click", function(){--%> <%--// map.openInfoWindow(infoWindow,point); //开启信息窗口--%> <%--// });--%> <%--map.centerAndZoom(new BMap.Point(121.412962,31.12775), 15);--%> <%--// var data_info =document.getElementById("datamap").value ;--%> <%--// alert(data_info)--%> <%--var data_info =${command}--%> <%--var opts = {--%> <%--width : 250, // 信息窗口宽度--%> <%--height: 80, // 信息窗口高度--%> <%--title : "信息窗口" , // 信息窗口标题--%> <%--enableMessage:true//设置允许信息窗发送短息--%> <%--};--%> <%--for(var i=0;i<data_info.length;i++){--%> <%--var marker = new BMap.Marker(new BMap.Point(data_info[i][0],data_info[i][1])); // 创建标注--%> <%--var content = data_info[i][2];--%> <%--map.addOverlay(marker); // 将标注添加到地图中--%> <%--addClickHandler(content,marker);--%> <%--}--%> <%--function addClickHandler(content,marker){--%> <%--marker.addEventListener("click",function(e){--%> <%--openInfo(content,e)}--%> <%--);--%> <%--}--%> <%--function openInfo(content,e){--%> <%--var p = e.target;--%> <%--var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);--%> <%--var infoWindow = new BMap.InfoWindow(content,opts); // 创建信息窗口对象--%> <%--map.openInfoWindow(infoWindow,point); //开启信息窗口--%> <%--}--%> <%--</script>--%> <!DOCTYPE html> <html <%--onload="bdGEO()"--%>> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf8" /> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <title>批量地址</title> <style type="text/css"> body, html{width: 100%;height: 100%;margin:0;font-family:"微软雅黑";} #l-map{height:100%;width:100%;} #r-result{width:100%; font-size:14px;line-height:20px;} </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=XWV87PbnVsMGnrRU3nEBRvnG"></script> </head> <body> <div id="l-map"></div> <div id="r-result"> <input type="button" value="批量地址解析" onclick="bdGEO()" /> <div id="result"></div> </div> </body> </html> <script type="text/javascript"> // 百度地图API功能 var map = new BMap.Map("l-map"); map.centerAndZoom(new BMap.Point(121.324644,31.160271), 15); map.enableScrollWheelZoom(true); var index = 0; var myGeo = new BMap.Geocoder(); var adds = ${command1}; window.onload=bdGEO(); function bdGEO(){ var add = adds[index]; geocodeSearch(add); index++; } function geocodeSearch(add){ if(index < adds.length){ setTimeout(window.bdGEO,400); } myGeo.getPoint(add, function(point){ if (point) { document.getElementById("result").innerHTML += index + "、" + add + ":" + point.lng + "," + point.lat + "</br>"; var address = new BMap.Point(point.lng, point.lat); // addMarker(address,new BMap.Label(index+":"+add,{offset:new BMap.Size(20,-10)})); var marker = new BMap.Marker(point); map.addOverlay(marker); var opts = { width : 400, // 信息窗口宽度 height: -10, // 信息窗口高度 title : add , // 信息窗口标题 enableMessage:true,//设置允许信息窗发送短息 message:"" } var infoWindow = new BMap.InfoWindow("", opts); // 创建信息窗口对象 marker.addEventListener("click", function(){ map.openInfoWindow(infoWindow,point); //开启信息窗口 }); } }, "上海市"); } // 编写自定义函数,创建标注 function addMarker(point,label){ var marker = new BMap.Marker(point); map.addOverlay(marker); // marker.setLabel(label); } </script>