<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="keywords" content="互助,同行互联" />
<meta name="description" content="同行互联是提供综合解决方案的互助服务平台" />
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport" />
<title>合作修理厂</title>
<!--引用百度地图API-->
<style type="text/css">
html,body{margin:0;padding:0;}
.iw_poi_title {color:#CC5522;font-size:14px;font-weight:bold;overflow:hidden;padding-right:13px;white-space:nowrap}
.iw_poi_content {font:12px arial,sans-serif;overflow:visible;padding-top:4px;white-space:-moz-pre-wrap;word-wrap:break-word}
#getLocation{
100%;
border: none;
background-color: #00af84;
height: 2.02666rem;
line-height: 2.02666rem;
color: #fff;
text-align: center;
background-color: #00af84;
font-size: 0.8rem;
}
</style>
<!-- <script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=1.1&services=true"></script> -->
<script type="text/javascript" src="${base}/resources/admin/js/jquery.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=你的密钥"></script>
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
wx.config({
debug: false,
appId: "${wx_appid}",
timestamp: "${timestamp}",
nonceStr: "${nonceStr}",
signature: "${signature}",
jsApiList: ['openLocation',
'getLocation']
});
</script>
</head>
<body>
<!--百度地图容器-->
<div style="100%;height:350px;border:#ccc solid 1px;" id="dituContent"></div>
<button id="getLocation">获取定位</button>
</body>
<script type="text/javascript">
var map = new BMap.Map("dituContent");
// 地理位置接口
var clickflag = 1;
document.querySelector('#getLocation').onclick = function(){
clickflag += 1;
if(clickflag == 2){
mylocation();
}
}
// 我的位置
function mylocation(){
wx.getLocation({
success: function (res) {
/*
自定义标记自己地理位置的图标
var myIcon = new BMap.Icon("${base}/resources/front/images/txhl.png", new BMap.Size(300, 157));
var marker = new BMap.Marker(point, {icon: myIcon});
map.addOverlay(marker);*/
// 坐标转换
var latitude = res.latitude;
var longitude = res.longitude;
//微信坐标转换之前的经纬度
var ggPoint = new BMap.Point(longitude,latitude);
map.centerAndZoom(ggPoint, 12);
map.addControl(new BMap.NavigationControl());
//坐标转换完之后的回调函数
translateCallback = function (data){
if(data.status === 0) {
var marker = new BMap.Marker(data.points[0]);
map.addOverlay(marker);
var label = new BMap.Label("我的位置",{offset:new BMap.Size(20,-10)});
marker.setLabel(label); //添加"我的位置"
map.setCenter(data.points[0]);
// 转换之后的经纬度
// alert(data.points[0].lng);
// alert(data.points[0].lat);
// 根据转换之后的经纬度获取地名
var p1 = new BMap.Point(data.points[0].lng,data.points[0].lat);
var p2 = new BMap.Point(116.44825,39.994137);
// autoViewport为true自动调整地图为false不自动调整
var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: false}});
driving.search(p1, p2);
// 按照具体地名转换
/* var gc = new BMap.Geocoder();
gc.getLocation(new BMap.Point(data.points[0].lng,data.points[0].lat), function(rs){
var addComp = rs.addressComponents;
//我的具体地址
var myself = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber;
// 找最近修理产过的距离
var output = "到最近修理厂距离";
var searchComplete = function (results){
if (transit.getStatus() != BMAP_STATUS_SUCCESS){
return ;
}
var plan = results.getPlan(0);
output += plan.getDuration(true) + "
"; //获取时间
output += "总路程为:" ;
output += plan.getDistance(true) + "
"; //获取距离
}
var transit = new BMap.DrivingRoute(map, {renderOptions: {map: map},
onSearchComplete: searchComplete,
onPolylinesSet: function(){
setTimeout(function(){alert(output)},"1000");
}});
transit.search(myself, "北京丰顺路宝汽车服务有限公司");
});*/
}
}
setTimeout(function(){
var convertor = new BMap.Convertor();
var pointArr = [];
pointArr.push(ggPoint);
// 将微信坐标转成百度坐标
convertor.translate(pointArr, 1, 5, translateCallback);
}, 1000);
// 找最近修理厂的距离线
/* var p1 = new BMap.Point(116.301934,39.977552);
var p2 = new BMap.Point(116.508328,39.919141);
var driving = new BMap.DrivingRoute(map, {renderOptions:{map: map, autoViewport: true}});
driving.search(p1, p2);*/
},
cancel: function (res) {
alert('拒绝授权获取地理位置');
},
error:function(){
alert('定位失败');
}
});
}
//创建和初始化地图函数:
function initMap(){
createMap();//创建地图
setMapEvent();//设置地图事件
addMapControl();//向地图添加控件
addMarker();//向地图中添加marker
mylocation();
}
//创建地图函数:
function createMap(){
//在百度地图容器中创建一个地图
var point = new BMap.Point(116.395645,39.929986);//定义一个中心点坐标
map.centerAndZoom(point,11);//设定地图的中心点和坐标并将地图显示在地图容器中
window.map = map;//将map变量存储在全局
}
//地图事件设置函数:
function setMapEvent(){
map.enableDragging();//启用地图拖拽事件,默认启用(可不写)
map.enableScrollWheelZoom();//启用地图滚轮放大缩小
map.enableDoubleClickZoom();//启用鼠标双击放大,默认启用(可不写)
map.enableKeyboard();//启用键盘上下左右键移动地图
}
//地图控件添加函数:
function addMapControl(){
//向地图中添加缩放控件
var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE});
map.addControl(ctrl_nav);
//向地图中添加缩略图控件
var ctrl_ove = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:1});
map.addControl(ctrl_ove);
//向地图中添加比例尺控件
var ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT});
map.addControl(ctrl_sca);
}
//标注点数组
var markerArr = [
{title:"北京限公司",content:"西直门北大街",point:"116.44825|39.994137",isOpen:0,icon:{w:30,h:35,l:0,t:0,x:6,lb:5}}
,{title:"北京务有限公司",content:"西直门北大街",point:"116.306246|39.992368",isOpen:0,icon:{w:30,h:35,l:0,t:0,x:6,lb:5}}
,{title:"北有限公司",content:"西直门北大街",point:"116.295322|39.839187",isOpen:0,icon:{w:30,h:35,l:0,t:0,x:6,lb:5}}
,{title:"北京服务有限公司",content:"西直门北大街",point:"116.482745|39.847164",isOpen:0,icon:{w:30,h:35,l:0,t:0,x:6,lb:5}}
,{title:"北京路限公司",content:"西直门北大街",point:"116.313145|39.968926",isOpen:0,icon:{w:30,h:35,l:0,t:0,x:6,lb:5}}
,{title:"北京丰务有限公司",content:"西直门北大街",point:"116.437326|39.977773",isOpen:0,icon:{w:30,h:35,l:0,t:0,x:6,lb:5}}
,{title:"北京务有限公司",content:"西直门北大街",point:"116.323493|39.863559",isOpen:0,icon:{w:30,h:35,l:0,t:0,x:6,lb:5}}
,{title:"北京丰顺路车服限公司",content:"西直门北大街",point:"116.463773|39.86799",isOpen:0,icon:{w:30,h:35,l:0,t:0,x:6,lb:5}}
,{title:"北京有限公司",content:"西直门北大街",point:"116.363162|39.905637",isOpen:0,icon:{w:30,h:35,l:0,t:0,x:6,lb:5}}
,{title:"北京有限公司",content:"西直门北大街",point:"116.439626|39.912279",isOpen:0,icon:{w:30,h:35,l:0,t:0,x:6,lb:5}}
];
//创建经纬度到地图上的处理函数
function addMarker(){
for(var i=0;i<markerArr.length;i++){
var json = markerArr[i];
var p0 = json.point.split("|")[0];
var p1 = json.point.split("|")[1];
var point = new BMap.Point(p0,p1);
var iconImg = createIcon(json.icon);
var marker = new BMap.Marker(point,{icon:iconImg});
var iw = createInfoWindow(i);
//修理厂标题
// var label = new BMap.Label(json.title,{"offset":new BMap.Size(json.icon.lb-json.icon.x+10,-20)});
// marker.setLabel(label);
map.addOverlay(marker);
//修理厂标题样式
/*label.setStyle({
borderColor:"#808080",
color:"#333",
cursor:"pointer"
});*/
(function(){
var index = i;
var _iw = createInfoWindow(i);
var _marker = marker;
_marker.addEventListener("click",function(){
this.openInfoWindow(_iw);
});
/*_iw.addEventListener("open",function(){
_marker.getLabel().hide();
})
_iw.addEventListener("close",function(){
_marker.getLabel().show();
})
label.addEventListener("click",function(){
_marker.openInfoWindow(_iw);
})*/
if(!!json.isOpen){
label.hide();
_marker.openInfoWindow(_iw);
}
})()
}
}
//创建InfoWindow
function createInfoWindow(i){
var json = markerArr[i];
var iw = new BMap.InfoWindow("<b class='iw_poi_title' title='" + json.title + "'>" + json.title + "</b><div class='iw_poi_content'>"+json.content+"</div>");
return iw;
}
//创建一个Icon
function createIcon(json){
var icon = new BMap.Icon("${base}/resources/front/images/map_flag.png", new BMap.Size(json.w,json.h),{imageOffset: new BMap.Size(-json.l,-json.t),infoWindowOffset:new BMap.Size(json.lb+5,1),offset:new BMap.Size(json.x,json.h)})
return icon;
}
initMap();//创建和初始化地图
</script>
</html>