<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>javascript
<style type="text/css">
html, body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
overflow: hidden;
}
#filter {
float: left;
width: 30%;
height: 100%;
}
#map {
width: 70%;
height: 100%;
}
#dealers_list {
margin: 10px; overflow-y: scroll; height: 500px
}
#dealers_list .item{
cursor: pointer;
margin-bottom: 5px;
}
</style>
</head>
<body>
<div id="filter">
<div style="margin: 10px;">
<span>選擇區域</span>
<span><select id="area"><option value="">所有區域</option><option value="1">南區</option><option value="2">北區</option></select></span>
</div>
<div style="margin: 10px;">
<span>選擇城市</span>
<span><select id="city"></select></span>
</div>
<div style="margin: 10px;">
<span>驗證是否在區域中</span><br/>
<span>
經度:<input type="text" name="longitude" id="longitude"><br/>
維度:<input type="text" name="latitude" id="latitude"><br/>
<input type="button" btn="search" value="查詢">
</span>
</div>
<div id="dealers_list">
</div>
</div>css
<div id="map"></div>
<canvas id="canvas"></canvas>html
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=1XjLLEhZhQNUzd93EjU5nOGQ"></script>
<script type="text/javascript" src="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.js"></script>
<script type="text/javascript" src="/js/mapv.js"></script>
<link rel="stylesheet" href="http://api.map.baidu.com/library/DrawingManager/1.4/src/DrawingManager_min.css"/>
<script type="text/javascript">
$(function(){
//加載地圖
window._loadMap = function(cityid,dealerid){
$.get("/test/map",{cityid:cityid,dealerid:dealerid},function(res){
$("body").append(res);
},"html");
}
//加載區域城市
window._loadCity = function(big_areaid){
$.get("/test/getcitys",{big_areaid:big_areaid},function(res){
if(res&&res.code==1){
$("#dealers_list").html("");
data = res.data;
str = "<option value=''>請選擇</option>";
for (i = 0; i < data.length; i++) {
str += "<option value='"+data[i].cityid+"'>"+data[i].cityname+"</option>";
}
$("#city").html(str);
}else{
alert(res.msg);
}
},"json");
}
//加載城市經銷商
window._loadDealer = function(cityid){
$.get("/test/getdealers",{cityid:cityid},function(res){
if(res&&res.code==1){
data = res.data;
str = "";
for (i = 0; i < data.length; i++) {
str += "<div class='item' data-dealerid='"+data[i].dealerid+"'>"+data[i].dealername+"</div>";
}
$("#dealers_list").html(str);
}else{
alert(res.msg);
}
},"json");
}
//大區選擇
$("#area").change(function(){
big_areaid = $(this).val();
if(big_areaid==""){
$("#city").html("");
return false;
}
_loadMap(0,0);
_loadCity(big_areaid);
});java
window.dealerid = 0;
window.cityid = 0;
//城市選擇
$("#city").change(function(){
cityid = $(this).val();
if(cityid==""){
alert("請選擇城市");
return false;
}
_loadDealer(cityid);
_loadMap(cityid,0);
});
//經銷商點擊
$("#dealers_list").delegate('.item', 'click', function(event) {
dealerid = $(this).attr("data-dealerid");
_loadMap(cityid,dealerid);
});jquery
//是否當前位置是否在區域內
$('[btn="search"]').click(function(){
longitude = $("#longitude").val();
latitude = $("#latitude").val();
$.post("/test/search",{dealerid:dealerid,longitude:longitude,latitude:latitude},function(res){
if(res&&res.code==1)
{
alert(res.msg);
}else{
alert(res.msg);
}
},"json");
});git
//初始化地圖
_loadMap(0,0);
});
</script>
</body>
</html>
json
<script type="text/javascript">canvas
// 百度地圖API功能
var map = new BMap.Map("map", {
enableMapClick: true
}); // 建立Map實例
@if($dealer)
//點擊單一商戶
point = new BMap.Point({{$dealer['longitude']}}, {{$dealer['latitude']}});
map.centerAndZoom(point, 13); // 初始化地圖,設置中心點座標和地圖級別
@elseif($city)
//選擇某一個城市
map.centerAndZoom("{{$city['cityname']}}",13);
@else
//默認顯示城市
map.centerAndZoom("北京",13);
@endif
//
map.enableScrollWheelZoom(true); // 開啓鼠標滾輪縮放
map.addControl(new BMap.NavigationControl());api
// 編寫自定義函數,建立商家標註
function addMarker(point,dealername,show_delete){
var marker = new BMap.Marker(point);
map.addOverlay(marker);
var label = new BMap.Label(dealername,{offset:new BMap.Size(20,-10)});
marker.setLabel(label);app
if(show_delete)
{
var delete_label = new BMap.Label("<a href='javascript:;'>點擊圖標刪除區域</a>",{offset:new BMap.Size(-38,26)});
marker.setLabel(delete_label);
marker.addEventListener("click",function(){
$.post("/test/removesign",{dealerid:dealerid},function(res){
if(res&&res.code==1){
_loadMap(cityid,dealerid);
}else{
alert(res.msg);
}
},"json");
});
}
}
//添加商戶到地圖
var _loadDealerToMap = function(){
@if($dealer)
var point = new BMap.Point({{$dealer['longitude']}}, {{$dealer['latitude']}});
addMarker(point,"{{$dealer['dealername']}}",{{$dealer['points']?1:0}});
@elseif($dealers)
@foreach($dealers as $v)
@if($v['longitude'] && $v['latitude'])
var point = new BMap.Point({{$v['longitude']}}, {{$v['latitude']}});
addMarker(point,"{{$v['dealername']}}",false);
@endif
@endforeach
@endif
}
//setTimeout(_loadDealerToMap(),1000);
_loadDealerToMap();
//使用工具劃區域後執行事件
var overlaycomplete = function (e) {
if(drawingManager.getDrawingMode() != "polygon"){
alert("請選用多邊形畫圖工具!");
return false;
}
if(!dealerid){
alert("請先選擇一個經銷商!");
return false;
}
points = e.overlay.getPath();
$.post("/test/signdealer",{dealerid:dealerid,points:points},function(res){
if(res&&res.code==1){
//從新刷新地圖
_loadMap(cityid,dealerid);
}else{
alert(res.msg);
}
},"json");
//debugger;
};
var styleOptions = {
strokeColor: "red", //邊線顏色。
fillColor: "red", //填充顏色。當參數爲空時,圓形將沒有填充效果。
strokeWeight: 3, //邊線的寬度,以像素爲單位。
strokeOpacity: 0.8, //邊線透明度,取值範圍0 - 1。
fillOpacity: 0.6, //填充的透明度,取值範圍0 - 1。
strokeStyle: 'solid' //邊線的樣式,solid或dashed。
};
//實例化鼠標繪製工具
var drawingManager = new BMapLib.DrawingManager(map, {
isOpen: false, //是否開啓繪製模式
drawingType: BMAP_DRAWING_POLYGON,
enableDrawingTool: true, //是否顯示工具欄
enableCalculate: false,
drawingToolOptions: {
anchor: BMAP_ANCHOR_TOP_RIGHT, //位置
offset: new BMap.Size(5, 10), //偏離值
drawingModes : [
BMAP_DRAWING_POLYGON //多邊形
]
},
polygonOptions: styleOptions, //多邊形的樣式
});
//drawingManager.setDrawingMode(BMAP_DRAWING_POLYGON);
//添加鼠標繪製工具監聽事件,用於獲取繪製結果
drawingManager.addEventListener('overlaycomplete', overlaycomplete);
map.setMapStyle({
styleJson: [
{
"featureType": "all",
"elementType": "all",
"stylers": {
"lightness": 61,
"saturation": -100
}
}
]
});
@if($dealer)
$.get('/test/getdealerare',{dealerid:{{$dealer['dealerid']}}}, function (data) {
if(data)
{
var dataSet = new mapv.DataSet(data);
var options = {
fillStyle: 'rgba(255, 80, 53, 0.8)',
strokeStyle: 'rgba(250, 255, 53, 0.8)',
lineWidth: 2,
draw: 'simple',
zIndex: 1,
size: 5, // 大小值
}
var mapvLayer = new mapv.baiduMapLayer(map, dataSet, options);
}
});
@endif
</script>
/** * 判斷是否在區域內部 * @param [array] $p 須要查詢的經緯度 * @param [array] $points 查詢的圈的經緯度集合 * @return boolean */ private function _isInside($p,$points) { $count = count($points); if($count < 3) return false; $result = false; for($i = 0, $j = $count - 1; $i < $count; $i++) { $p1 = $points[$i]; $p2 = $points[$j]; if($p1['lat'] < $p['lat'] && $p2['lat'] >= $p['lat'] || $p2['lat'] < $p['lat'] && $p1['lat'] >= $p['lat']){ if($p1['lng'] + ($p['lat'] - $p1['lat']) / ($p2['lat'] - $p1['lat']) * ($p2['lng'] - $p1['lng']) < $p['lng']){ $result = !$result; } } $j = $i; } return $result; }