class demoMap {
|
constructor() {
|
this.clickDot = null
|
this.searchDot = []
|
this.nowCity = ''
|
this.city()
|
}
|
init(id) {
|
var map = new AMap.Map(id, { zoom: 13 });
|
return map
|
}
|
// 当前城市
|
city(fn) {
|
let t = this
|
AMap.plugin('AMap.CitySearch', function () {
|
var citySearch = new AMap.CitySearch()
|
citySearch.getLocalCity(function (status, result) {
|
if (status === 'complete' && result.info === 'OK') {
|
// 查询成功,result即为当前所在城市信息
|
t.nowCity = result.city
|
fn ? fn(result) : null
|
}
|
})
|
})
|
}
|
// 设置城市
|
setCity(v, map) {
|
let t = this
|
t.nowCity = ''
|
t.search(v, e => {
|
if (e.length) {
|
let lng = e[0].location.lng
|
let lat = e[0].location.lat
|
t.geo({
|
lng, lat
|
}, res => {
|
if (res.addressComponent) {
|
t.nowCity = res.addressComponent.city
|
}
|
})
|
if (map && e[0].location) {
|
map.setCenter([lng, lat])
|
}
|
}
|
}, false)
|
}
|
// 点击
|
click(map, fn, dot) {
|
let t = this
|
map.on('click', e => {
|
let os = {
|
lat: e.lnglat.lat,
|
lng: e.lnglat.lng,
|
type: e.type,
|
target: e.target
|
}
|
if (!dot) {
|
t.clearDot(map)
|
t.clickDot = t.dot(map, os)
|
t.geo(os, res => {
|
os.geo = res
|
fn ? fn(os) : null
|
})
|
t.clickDot.on('click', es => {
|
fn ? fn({ code: 1000, e: es.target.w.extData }) : null
|
})
|
} else {
|
fn ? fn(os) : null
|
}
|
})
|
return map
|
}
|
// 生成小点
|
dot(map, opt, click) {
|
opt = opt || {}
|
var marker = new AMap.Marker({
|
position: new AMap.LngLat(opt.lng, opt.lat),
|
extData: opt,
|
clickable: !click,
|
title: opt.name || ''
|
});
|
map.add(marker);
|
return marker
|
}
|
// 地址 <-> 经纬度 AMap.Geocoder
|
geo(opt, fn) {
|
let t = this
|
AMap.plugin('AMap.Geocoder', function () {
|
var geocoder = new AMap.Geocoder({
|
// city 指定进行编码查询的城市,支持传入城市名、adcode 和 citycode
|
city: t.nowCity,
|
pageSize: 30
|
})
|
|
var search = opt.type === 'address' ? opt.search : [opt.lng, opt.lat]
|
|
geocoder.getAddress(search, function (status, result) {
|
if (status === 'complete' && result.info === 'OK') {
|
// result为对应的地理位置详细信息
|
fn ? fn(result.regeocode) : null
|
}
|
})
|
})
|
}
|
// 输入提示
|
input(val, fn, dot, map) {
|
var t = this
|
AMap.plugin('AMap.Autocomplete', function () {
|
// 实例化Autocomplete
|
var autoOptions = {
|
//city 限定城市,默认全国
|
city: t.nowCity
|
}
|
var autoComplete = new AMap.Autocomplete(autoOptions);
|
autoComplete.search(val, function (status, result) {
|
// 搜索成功时,result即是对应的匹配数据
|
if (status === 'complete' && result.info === 'OK') {
|
// result为对应的地理位置详细信息
|
var list = result.tips || []
|
fn ? fn(list) : null
|
if (dot && map) {
|
// 生成 点
|
t.clearDot(map)
|
list.forEach(v => {
|
if (v.location) {
|
var r = {
|
lng: v.location.lng,
|
lat: v.location.lat,
|
address: v.address,
|
name: v.name,
|
desc: v.district,
|
code: v.adcode
|
}
|
r.dot = t.dot(map, r)
|
t.searchDot.push(r)
|
}
|
})
|
if (t.searchDot.length) {
|
map.setCenter([t.searchDot[0].lng, t.searchDot[0].lat])
|
}
|
t.searchClick(e => {
|
fn ? fn({ code: 1000, e }) : null
|
})
|
}
|
}
|
})
|
})
|
}
|
// 搜索
|
search(val, fn, dot, map) {
|
var t = this
|
AMap.plugin('AMap.PlaceSearch', function () {
|
var placeSearch = new AMap.PlaceSearch({
|
// city 指定搜索所在城市,支持传入格式有:城市名、citycode和adcode
|
city: t.nowCity,
|
pageSize: 50
|
})
|
|
placeSearch.search(val, function (status, result) {
|
// 查询成功时,result即对应匹配的POI信息
|
if (status === 'complete' && result.info === 'OK') {
|
var list = (result.poiList || {}).pois || []
|
fn ? fn(list) : null
|
if (dot && map) {
|
t.clearDot(map)
|
list.forEach(v => {
|
if (v.location) {
|
var r = {
|
lng: v.location.lng,
|
lat: v.location.lat,
|
address: v.address,
|
name: v.name,
|
type: v.type
|
}
|
r.dot = t.dot(map, r)
|
t.searchDot.push(r)
|
}
|
})
|
map.setCenter([t.searchDot[0].lng, t.searchDot[0].lat])
|
t.searchClick(e => {
|
fn ? fn({ code: 1000, e }) : null
|
})
|
}
|
}
|
})
|
})
|
}
|
// 点 事件
|
searchClick(fn) {
|
let t = this
|
if (t.searchDot.length) {
|
t.searchDot.forEach(r => {
|
r.dot.on('click', e => {
|
let val = {}
|
try {
|
val = e.target.w.extData
|
// if (val.dot) { delete val.dot }
|
} catch (e) { }
|
fn ? fn(val) : null
|
})
|
})
|
}
|
}
|
// 删除点
|
clearDot(map) {
|
let t = this
|
if (t.searchDot.length) {
|
t.searchDot.forEach(r => {
|
if (r.dot) {
|
map.remove(r.dot)
|
}
|
})
|
t.searchDot = []
|
}
|
if (t.clickDot) {
|
map.remove(t.clickDot)
|
t.clickDot = null
|
}
|
}
|
}
|
|
export default new demoMap()
|