interval() {
this.timer = setInterval(function() {
this.awaitPlaceSearch();
}.bind(this), 50)
},
awaitPlaceSearch() {
if(this.placeSearch && this.geoCoder) {
clearInterval(this.timer);
this.isUseNearBy = false;
this.placeSearch.search(this.handleCityName, (status, result) => {
if (status === 'complete') {
if(result.poiList.pois.length) {
if(result.poiList.pois.length % this.pageSize !== 0) {
this.isUseNearBy = true;
this.bottomLineShow = true;
} else {
this.isUseNearBy = false;
this.bottomLineShow = false;
}
const geos = result.poiList.pois[0].location;
this.center = [geos.lng, geos.lat];
this.getAreaInfo('location', (res) => {
this.lists = result.poiList.pois//将查询到的地点赋值
this._initScroll();
this.getAddress(0);
this.createdMap([this.ruleForm.long, this.ruleForm.lat]);
this.addMarker();
})
} else {
if(result.cityList && result.cityList.length > 1) {
this.lists = [];
this.onCloseModal();
this.$message.info(`当前搜索地址在${result.cityList.length}个城市有若干结果,请明确后再搜索`);
} else {
this.lists = [];
this.onCloseModal();
this.$message.info('未能查询到该地址,请更换关键字查询');
}
}
} else if(status === 'no_data') {
this.$message.warning(`您输入的地址“${this.cityName}”,未查询到相关信息`);
this.getAreaInfo('address', (res) => {
this.lists = [
{
location: {
lng: res.geocodes[0].location.lng,
lat: res.geocodes[0].location.lat,
},
name: res.geocodes[0].formattedAddress,
address: '',
}
]
this.center = [res.geocodes[0].location.lng, res.geocodes[0].location.lat]
this._initScroll();
this.getAddress(0);
this.createdMap([this.ruleForm.long, this.ruleForm.lat]);
this.addMarker();
this.isUseNearBy = true;
this.bottomLineShow = true;
});
} else {
this.fixedPosition();//精准定位或IP地址定位
}
})
}
},
当然也是在保证this.placeSearch和this.geoCoder(即AMap.Geocoder和AMap.PlaceSearch)都加载到,同样也是上面说的笨办法。我是先用AMap.PlaceSearch.search方法,通过关键字查询传入的地址,如果查询到,则确定新的地图中心点经纬度,再通过AMap.Geocoder.getAddress方法传入新的地图中心点经纬度,解析出省市区信息,也可能关键字查询不明确(未明确省市区),比如只写了xx路,就有可能在全国多处都有结果,则会关闭弹框,提示明确后再查询,还可能关键字根本就查询不到,AMap.PlaceSearch.search返回no_data,则传入关键字使用AMap.Geocoder.getLocation方法,解析关键字所在城市,总之这里的逻辑就是AMap.Geocoder和AMap.PlaceSearch搭配使用。此处this.getAreaInfo方法是解析地址省市区的方法,设置了一个回调函数作为参数:
//依赖高德该接口获得省市区信息
getAreaInfo(type, callback) {
if(type === 'address') { // 在编辑的情况下通过地址名称查询
this.geoCoder.getLocation(this.handleCityName, (status, result) => {
if (status === 'complete' && result.geocodes.length) {
this.ruleForm.regeocode.adcode = result.geocodes[0].adcode
this.ruleForm.regeocode.province = result.geocodes[0].addressComponent.province
this.ruleForm.regeocode.city = result.geocodes[0].addressComponent.city == '' ? result.geocodes[0].addressComponent.province : result.geocodes[0].addressComponent.city
this.ruleForm.regeocode.district = result.geocodes[0].addressComponent.district;
callback(result);
} else if(status === 'no_data') {
this.lists = [];
this.onCloseModal();
this.$message.info('未能查询到该地址,请更换关键字查询');
} else {
this.fixedPosition();//精准定位或IP地址定位
}
});
} else if(type === 'location') { // 在查看的情况下通过经纬度查询
this.geoCoder.getAddress(this.center, (status, result) => {
if (status === 'complete' && result.regeocode && result.regeocode.addressComponent) {
this.ruleForm.regeocode.adcode = result.regeocode.addressComponent.adcode
this.ruleForm.regeocode.province = result.regeocode.addressComponent.province
this.ruleForm.regeocode.city = result.regeocode.addressComponent.city == '' ? result.regeocode.addressComponent.province : result.regeocode.addressComponent.city
this.ruleForm.regeocode.district = result.regeocode.addressComponent.district;
callback(result);
} else {
this.fixedPosition();//精准定位或IP地址定位
}
});
}
},
以上在获取到地址时都会设置为一个地址列表,设置在地图左侧,供用户选择,滚动使用better-scroll,可以上拉加载进行分页,这里不展开说了。
通过以上步骤,大部分情况下都能查询到精确的关键字,一部分高德没有收录的企业信息,也能定位到所在街道乡镇省市区。此外还在点击地图的时候触发了AMap.PlaceSearch.searchNearBy方法查询鼠标点击处的周边,作为补充:
// 查看更多,此方法也给上拉加载使用
searchMore(e) {
if(e) {
this.isUseNearBy = true;
this.placeSearch.opt.pageIndex = 1;
this.center = [e.lnglat.getLng(), e.lnglat.getLat()];
} else {
this.placeSearch.opt.pageIndex += 1;
}
this.geoCoder.getAddress(this.center, (status, result) => {
if (status === 'complete' && result.info === 'OK') {
let addressComponent = result.regeocode.addressComponent;
this.ruleForm.regeocode.adcode = addressComponent.adcode
this.ruleForm.regeocode.province = addressComponent.province
this.ruleForm.regeocode.city = addressComponent.city == '' ? addressComponent.province : addressComponent.city
this.ruleForm.regeocode.district = addressComponent.district;
let cityName = this.isUseNearBy ? '' : this.handleCityName;
if(this.isUseNearBy) {
this.placeSearch.searchNearBy(cityName, this.center, 1000, (status, result) => {
this.searchCallBack(status, result, e)
});
} else {
this.placeSearch.search(cityName, (status, result, e) => {
this.searchCallBack(status, result)
});
}
} else {
this.emptyLocationData();
}
})
},
searchCallBack(status, result, event) {
if (status === 'complete' && result.poiList.pois.length) {
if(event) {
this.lists = result.poiList.pois;
if(this.lists.length % this.pageSize !== 0) {
this.bottomLineShow = true;
} else {
this.bottomLineShow = false;
}
this.getAddress(0);
if(this.scroll) {
this.scroll.scrollTo(0, 0);
this.scroll.finishPullUp();
} else {
this._initScroll();
}
this.addMarker();
} else {
this.lists = concat(this.lists, result.poiList.pois);
if(this.lists.length % this.pageSize !== 0) {
this.bottomLineShow = true;
} else {
this.bottomLineShow = false;
}
this.originalStatus();
}
} else {
this.bottomLineShow = true;
}
},
主要的部分就是上面这些,还有一些里面用到的方法,我截几张图吧,第一次写文章,写的比较乱。
限时特惠:本站每日持续更新5-20节内部创业项目课程,一年会员
只需199元,全站资源免费下载点击查看详情
站长微信:
jjs406
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。