//输入提示,并实现搜索开始=========
AMap.plugin(['AMap.Autocomplete','AMap.PlaceSearch'],function(){
var autoOptions = {
city: "", //城市,默认全国
input: "searchipt"//使用联想输入的input的id
};
autocomplete= new AMap.Autocomplete(autoOptions);
var placeSearch = new AMap.PlaceSearch({
map:'' //map:map
});
AMap.event.addListener(autocomplete, "select", function(e){
//TODO 针对选中的poi实现自己的功能
//placeSearch.search(e.poi.name); //默认搜索功能
//重写搜索点及其提示信息begin=====
placeSearch.setCity(e.poi.adcode);
if (e.poi && e.poi.location) {
map.setZoom(17);
map.setCenter(e.poi.location);
}
placeSearch.search(e.poi.name, checkD); //关键字查询查询
var searchInfoWindow = new AMap.InfoWindow({
offset: new AMap.Pixel(105, -8),
isCustom:true
});
function checkD(status, result) {
if (status === 'complete' && result.info === 'OK') {
for(var h=0;h<result.poiList.pois.length;h++){//返回搜索列表循环绑定marker
var jy=result.poiList.pois[h]['location'];//经纬度
var name=result.poiList.pois[h]['name'];//地址
var searchmarker=new AMap.Marker({ //加点
map: map,
position: jy,
icon: new AMap.Icon({
image: localIcon
})
});
searchmarker.extData = {'getLng':jy['lng'],'getLat':jy['lat'],'name':name,'address':result.poiList.pois[h]['address']};//自定义想传入的参数
searchmarker.on("click",function(e) {
var hs=e.target.extData;
var content = searchContent(hs['name'],hs['address'],hs['getLng'],hs['getLat']);
searchInfoWindow.setContent(content);//点击以后窗口展示的内容
searchInfoWindow.open(map, e.target.getPosition());
});
}
}
}
//重写搜索点及其提示信息end=====
});
});
//输入提示,并实现搜索结束=========
//点击搜索按钮 开始=========
$('#searchbtn').on('click', function () {
var searchVal = $('#searchipt').val();
var flag = false;
var searchPPOI;
if(lineArr.length > 0){
for(var j=0;j<lineArr.length;j++){
var data = lineArr[j];
if(String(data[0]).indexOf(searchVal) > -1){
flag = true;
searchPPOI = data[1];
break;
}
}
}
if(flag){
map.setCenter(searchPPOI);
map.setZoom(17);
var searchInfoWindow = new AMap.InfoWindow({
offset: new AMap.Pixel(75, -8),
isCustom:true
});
var searchmarker=new AMap.Marker({ //加点
map: map,
position: searchPPOI,
icon: new AMap.Icon({
image: localIcon
})
});
searchmarker.extData = {'getLng':searchPPOI[0],'getLat':searchPPOI[1],'name':searchVal,'address':''};//自定义想传入的参数
searchmarker.on("click",function(e) {
var hs=e.target.extData;
var content = searchContent(hs['name'],hs['address'],hs['getLng'],hs['getLat']);
searchInfoWindow.setContent(content);//点击以后窗口展示的内容
searchInfoWindow.open(map, e.target.getPosition());
});
}else{
var autoOptions_btn = {
city: "" //城市,默认全国
};
var searchInfoWindow = new AMap.InfoWindow({
offset: new AMap.Pixel(105, -8),
isCustom:true
});
var autocomplete_btn = new AMap.Autocomplete(autoOptions_btn);
autocomplete_btn.search(searchVal, function(status, result){
if (status === 'complete' && result.info === 'OK') {
map.setCenter(result.tips[0]['location']);
map.setZoom(17);
for(var h=0;h<result.tips.length;h++){//返回搜索列表循环绑定marker
var jy=result.tips[h]['location'];//经纬度
var name=result.tips[h]['name'];//地址
var searchmarker=new AMap.Marker({ //加点
map: map,
position: jy,
icon: new AMap.Icon({
image: localIcon
})
});
searchmarker.extData = {'getLng':jy['lng'],'getLat':jy['lat'],'name':name,'address':result.tips[h]['address']};//自定义想传入的参数
searchmarker.on("click",function(e) {
var hs=e.target.extData;
var content = searchContent(hs['name'],hs['address'],hs['getLng'],hs['getLat']);
searchInfoWindow.setContent(content);//点击以后窗口展示的内容
searchInfoWindow.open(map, e.target.getPosition());
});
}
}
});
}
});
//点击搜索按钮 结束=========
//构造地点查询类,随便点击某一个点 开始======
var placeSearch = new AMap.PlaceSearch();
var infoWindow=new AMap.AdvancedInfoWindow({});
map.on('hotspotclick', function(result) {
placeSearch.getDetails(result.id, function(status, result) {
if (status === 'complete' && result.info === 'OK') {
placeSearch_CallBack(result);
}
});
});
//回调函数
function placeSearch_CallBack(data) { //infoWindow.open(map, result.lnglat);
var poiArr = data.poiList.pois;
createContent(poiArr[0]);
}
function createContent(poi) { //信息窗体内容
var info = [];
info.push(searchContent(poi.name,poi.address,poi.location.lng,poi.location.lat));
infoWindow = new AMap.InfoWindow({
offset: new AMap.Pixel(102, 20),
isCustom:true,
content: info.join('<br>') //使用默认信息窗体框样式,显示信息内容
});
infoWindow.open(map, poi.location);
}
//构造地点查询类,随便点击某一个点 结束======
//实时路况图层begin====
var trafficLayer = new AMap.TileLayer.Traffic({
zIndex: 10
});
trafficLayer.setMap(map);
trafficLayer.hide();//第一次进来不显示
var isVisible = false;
AMap.event.addDomListener(document.getElementById('lukuang'), 'click', function() {
if (isVisible) {
trafficLayer.hide();
isVisible = false;
} else {
trafficLayer.show();
isVisible = true;
}
}, false);
//实时路况图层end====
//卫星图层begin====
var satelliteLayer = new AMap.TileLayer.Satellite({
zIndex:11
});
satelliteLayer.setMap(map);
satelliteLayer.hide();//第一次进来不显示
var isVisible = false;
AMap.event.addDomListener(document.getElementById('weixing'), 'click', function() {
if (isVisible) {
satelliteLayer.hide();
isVisible = false;
} else {
satelliteLayer.show();
isVisible = true;
}
}, false);
//卫星图层end====
//右键菜单begin============
var contextMenu = new AMap.ContextMenu(); //创建右键菜单
//右键添加Marker标记
contextMenu.addItem("设为起点", function(e) {
var marker = new AMap.Marker({
icon:localIcon,
map: map,
position: contextMenuPositon //基点位置
});
}, 3);
//右键添加Marker标记
contextMenu.addItem("设为终点", function(e) {
var marker = new AMap.Marker({
map: map,
icon:localIcon,
position: contextMenuPositon //基点位置
});
}, 3);
//地图绑定鼠标右击事件——弹出右键菜单
map.on('rightclick', function(e) {
contextMenu.open(map, e.lnglat);
contextMenuPositon = e.lnglat;
});
//右键菜单end============