|
|
@ -2,6 +2,85 @@ import nextTick from "dai-js/tools/nextTick"; |
|
|
|
|
|
|
|
export const mapType = typeof window.TMap !== "undefined" ? "qq" : "td"; |
|
|
|
|
|
|
|
export const QQMap = window.TMap; |
|
|
|
export const TDMap = window.T; |
|
|
|
|
|
|
|
export function searchNearby(map, keyword) { |
|
|
|
if (mapType == "qq") { |
|
|
|
return new Promise((reslove) => { |
|
|
|
const search = new QQMap.service.Search(map, { pageSize: 10 }); |
|
|
|
search |
|
|
|
.searchNearby({ |
|
|
|
keyword, |
|
|
|
radius: 1000, |
|
|
|
autoExtend: true, |
|
|
|
center: map.getCenter(), |
|
|
|
}) |
|
|
|
.then((result) => { |
|
|
|
let { data } = result; |
|
|
|
if (Array.isArray(data) && data.length > 0) { |
|
|
|
const { |
|
|
|
location: { lat, lng, address }, |
|
|
|
} = data[0]; |
|
|
|
reslove({ |
|
|
|
msg: "success", |
|
|
|
data: { |
|
|
|
lng, |
|
|
|
lat, |
|
|
|
address, |
|
|
|
data, |
|
|
|
}, |
|
|
|
}); |
|
|
|
} else { |
|
|
|
reslove({ |
|
|
|
msg: "failed", |
|
|
|
error: "未检索到相关位置坐标", |
|
|
|
}); |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch((error) => { |
|
|
|
reslove({ |
|
|
|
msg: "failed", |
|
|
|
error, |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
return new Promise(async (reslove) => { |
|
|
|
const search = new TDMap.LocalSearch(map, { pageCapacity: 10 }); |
|
|
|
search.setQueryType(1); |
|
|
|
search.searchNearby(keyword, map.getCenter(), 1000000000); |
|
|
|
|
|
|
|
await nextTick(1000); |
|
|
|
|
|
|
|
const result = search.getResults(); |
|
|
|
const data = result ? result.getPois() : null; |
|
|
|
|
|
|
|
console.log("检索结果", data); |
|
|
|
|
|
|
|
if (Array.isArray(data) && data.length > 0) { |
|
|
|
const { lonlat, address, name } = data[0]; |
|
|
|
const lng = lonlat.split(" ")[0]; |
|
|
|
const lat = lonlat.split(" ")[1]; |
|
|
|
reslove({ |
|
|
|
msg: "success", |
|
|
|
data: { |
|
|
|
lng, |
|
|
|
lat, |
|
|
|
address: address + name, |
|
|
|
data, |
|
|
|
}, |
|
|
|
}); |
|
|
|
} else { |
|
|
|
reslove({ |
|
|
|
msg: "failed", |
|
|
|
error: "未检索到相关位置坐标", |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 封装了地图相关函数,兼容天地图、腾讯地图常用api
|
|
|
|
export default function init(ele, position, params) { |
|
|
|
this.mapType = mapType; |
|
|
@ -17,12 +96,8 @@ export default function init(ele, position, params) { |
|
|
|
this.setCenter = function (lat, lng) {}; |
|
|
|
this.setMarker = function (lat, lng, title) {}; |
|
|
|
this.getAddress = async function (lat, lng) {}; |
|
|
|
this.searchNearby = async function (keyword) {}; |
|
|
|
this.on = function (eventType, fn) {}; |
|
|
|
|
|
|
|
const QQMap = window.TMap; |
|
|
|
const TDMap = window.T; |
|
|
|
|
|
|
|
let { latitude, longitude } = position; |
|
|
|
if (!latitude || latitude == "" || latitude == "0") { |
|
|
|
latitude = 39.9088810666821; |
|
|
@ -88,46 +163,6 @@ export default function init(ele, position, params) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
this.search = new QQMap.service.Search(this.map, { pageSize: 10 }); |
|
|
|
this.searchNearby = async function (keyword) { |
|
|
|
return new Promise((reslove) => { |
|
|
|
this.search |
|
|
|
.searchNearby({ |
|
|
|
keyword, |
|
|
|
radius: 1000, |
|
|
|
autoExtend: true, |
|
|
|
center: this.map.getCenter(), |
|
|
|
}) |
|
|
|
.then((result) => { |
|
|
|
let { data } = result; |
|
|
|
if (Array.isArray(data) && data.length > 0) { |
|
|
|
const { |
|
|
|
location: { lat, lng, address }, |
|
|
|
} = data[0]; |
|
|
|
reslove({ |
|
|
|
msg: "success", |
|
|
|
data: { |
|
|
|
lng, |
|
|
|
lat, |
|
|
|
address, |
|
|
|
}, |
|
|
|
}); |
|
|
|
} else { |
|
|
|
reslove({ |
|
|
|
msg: "failed", |
|
|
|
error: "未检索到相关位置坐标", |
|
|
|
}); |
|
|
|
} |
|
|
|
}) |
|
|
|
.catch((error) => { |
|
|
|
reslove({ |
|
|
|
msg: "failed", |
|
|
|
error, |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
this.on = function (eventType, fn) { |
|
|
|
if (eventType == "dragend") { |
|
|
|
this.map.on("moveend", (e) => { |
|
|
@ -202,42 +237,6 @@ export default function init(ele, position, params) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
this.search = new TDMap.LocalSearch(this.map, { pageCapacity: 10 }); |
|
|
|
this.searchNearby = async function (keyword) { |
|
|
|
return new Promise(async (reslove) => { |
|
|
|
this.search.setQueryType(1); |
|
|
|
this.search.searchNearby(keyword, this.map.getCenter(), 1000000000); |
|
|
|
|
|
|
|
await nextTick(1000); |
|
|
|
|
|
|
|
const result = this.search.getResults(); |
|
|
|
const data = result ? result.getPois() : null; |
|
|
|
|
|
|
|
console.log("检索结果", data); |
|
|
|
|
|
|
|
if (Array.isArray(data) && data.length > 0) { |
|
|
|
const { lonlat, address, name } = data[0]; |
|
|
|
const lng = lonlat.split(" ")[0]; |
|
|
|
const lat = lonlat.split(" ")[1]; |
|
|
|
this.setCenter(lat, lng); |
|
|
|
this.setMarker(lat, lng); |
|
|
|
reslove({ |
|
|
|
msg: "success", |
|
|
|
data: { |
|
|
|
lng, |
|
|
|
lat, |
|
|
|
address: address + name, |
|
|
|
}, |
|
|
|
}); |
|
|
|
} else { |
|
|
|
reslove({ |
|
|
|
msg: "failed", |
|
|
|
error: "未检索到相关位置坐标", |
|
|
|
}); |
|
|
|
} |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
this.on = function (eventType, fn) { |
|
|
|
if (eventType == "dragend") { |
|
|
|
this.map.on("dragend", (e) => { |
|
|
@ -250,5 +249,11 @@ export default function init(ele, position, params) { |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
this.searchNearby = async function (keyword) { |
|
|
|
const ret = await searchNearby(this.map, keyword); |
|
|
|
|
|
|
|
return ret; |
|
|
|
}; |
|
|
|
|
|
|
|
return this; |
|
|
|
} |
|
|
|