var map, district, polygons = [], citycode; var provinceSelect = document.getElementById('province'); var citySelect = document.getElementById('city'); var districtSelect = document.getElementById('district'); var areaSelect = document.getElementById('street'); map = new AMap.Map('container', { resizeEnable: true, center: [116.30946, 39.937629], zoom: 3 }); AMapUI.loadUI(['control/BasicControl'], function(BasicControl) { var zoomCtrl1 = new BasicControl.Zoom({ theme: 'dark' }) map.addControl(zoomCtrl1); }); AMap.plugin(['AMap.Scale'], function() { map.addControl(new AMap.Scale()); }) //为地图注册click事件获取鼠标点击出的经纬度坐标 map.on('click', function(e) { document.getElementById("lnglat").value = e.lnglat.getLng() + ',' + e.lnglat.getLat(); regeoCode(); }); var auto = new AMap.Autocomplete({ input: "tipinput" }); AMap.event.addListener(auto, "select", select); //注册监听,当选中某条记录时会触发 function select(e) { if (e.poi && e.poi.location) { map.setZoom(15); map.setCenter(e.poi.location); } } var geocoder, marker; function geoCode() { if (!geocoder) { geocoder = new AMap.Geocoder({}); } var address = document.getElementById('addressm').value; geocoder.getLocation(address, function(status, result) { if (status === 'complete' && result.geocodes.length) { var lnglat = result.geocodes[0].location document.getElementById('lnglat').value = lnglat; if (!marker) { marker = new AMap.Marker(); map.add(marker); } marker.setPosition(lnglat); map.setFitView(marker); } else { alert(JSON.stringify(result)) } }); } document.getElementById('addressm').onkeydown = function(e) { if (e.keyCode === 13) { geoCode(); return false; } return true; }; function regeoCode() { if (!geocoder) { geocoder = new AMap.Geocoder({ city: "010", //城市设为北京,默认:“全国” radius: 1000 //范围,默认:500 }); } var lnglat = document.getElementById('lnglat').value.split(','); if (!marker) { marker = new AMap.Marker(); map.add(marker); } marker.setPosition(lnglat); geocoder.getAddress(lnglat, function(status, result) { if (status === 'complete' && result.regeocode) { var address = result.regeocode.formattedAddress; document.getElementById('addressm').value = address; } else { alert(JSON.stringify(result)) } }); } //行政区划查询 var opts = { subdistrict: 1, //返回下一级行政区 showbiz: false //最后一级返回街道信息 }; district = new AMap.DistrictSearch(opts); //注意:需要使用插件同步下发功能才能这样直接使用 district.search('中国', function(status, result) { if (status == 'complete') { getData(result.districtList[0]); } }); function getData(data, level) { //清空下一级别的下拉列表 if (level === 'province') { citySelect.innerHTML = ''; districtSelect.innerHTML = ''; areaSelect.innerHTML = ''; } else if (level === 'city') { districtSelect.innerHTML = ''; areaSelect.innerHTML = ''; } else if (level === 'district') { areaSelect.innerHTML = ''; } var subList = data.districtList; if (subList) { var contentSub = new Option('--请选择--'); var curlevel = subList[0].level; var curList = document.querySelector('#' + curlevel); curList.add(contentSub); for (var i = 0, l = subList.length; i < l; i++) { var name = subList[i].name; var levelSub = subList[i].level; var cityCode = subList[i].citycode; contentSub = new Option(name); contentSub.setAttribute("value", levelSub); contentSub.center = subList[i].center; contentSub.adcode = subList[i].adcode; curList.add(contentSub); } if (curlevel == "province") { showCityInfo(curList) } } } function search(obj) { //清除地图上所有覆盖物 for (var i = 0, l = polygons.length; i < l; i++) { polygons[i].setMap(null); } var option = obj[obj.options.selectedIndex]; var keyword = option.text; //关键字 var adcode = option.adcode; district.setLevel(option.value); //行政区级别 district.setExtensions('all'); //行政区查询 //按照adcode进行查询可以保证数据返回的唯一性 district.search(adcode, function(status, result) { if (status === 'complete') { getData(result.districtList[0], obj.id); getAddress(); } }); } function getAddress() { var address = ""; var provinceText = provinceSelect[provinceSelect.options.selectedIndex].text; if (provinceText.length > 0) { address += provinceText; } var cityText = citySelect[citySelect.options.selectedIndex]; if (citySelect && cityText.text && cityText.adcode) { address += cityText.text; } var districtText = districtSelect[districtSelect.options.selectedIndex]; if (districtText && districtText.text && districtText.adcode) { address += districtText.text; } var areaText = areaSelect[areaSelect.options.selectedIndex]; if (areaText && areaText.text && areaText.adcode) { address += areaText.text; } document.getElementById('addressm').value = address geoCode(); } function setCenter(obj) { getAddress(); //map.setCenter(obj[obj.options.selectedIndex].center) } //获取用户所在城市信息 function showCityInfo(curList) { //实例化城市查询类 var citysearch = new AMap.CitySearch(); //自动获取用户IP,返回当前城市 citysearch.getLocalCity(function(status, result) { if (status === 'complete' && result.info === 'OK') { if (result && result.city && result.bounds) { var cityinfo = result.city; var citybounds = result.bounds; var cityadcode = result.adcode; var cityindex = 0; document.getElementById('addressm').value = cityinfo; for (var i = 0; i < curList.options.length; i++) { if (cityadcode == curList.options[i].adcode) { cityindex = i; curList.options[cityindex].selected = true; search(curList); //geoCode(); break; } } } } else { console.log(result.info); } }); }