var state = { // 1:click—map 2:select-map input_state: 1, list_state: 1 } window.onload = function load() { document.getElementById("searchDiv").innerHTML = ""; document.getElementById("resultDiv").style.display = "none"; const init = new Promise((resolve, reject) => { if(T) { console.log('地图初始化成功') resolve(T) reject('error') } }) init.then(T => { map = new T.Map('map'); center = new T.LngLat(113.214781,23.406641); map.centerAndZoom(center, 15); 请修改为 通过点击一个按钮实现获取当前位置并设置为地图中心点后的代码
时间: 2024-04-27 12:23:17 浏览: 38
nanostate::vertical_traffic_light:-小型有限状态机
var state = { // 1:click—map 2:select-map input_state: 1, list_state: 1 }
window.onload = function load() {
document.getElementById("searchDiv").innerHTML = "";
document.getElementById("resultDiv").style.display = "none";
}
function setCurrentLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
const { latitude, longitude } = position.coords;
const center = new T.LngLat(longitude, latitude);
map.centerAndZoom(center, 15);
});
} else {
console.log('Geolocation is not supported by this browser.');
}
}
const init = new Promise((resolve, reject) => {
if(T) {
console.log('地图初始化成功');
resolve(T);
} else {
reject('error');
}
});
init.then(T => {
map = new T.Map('map');
setCurrentLocation();
});
请将按钮的HTML代码添加到页面中,例如:<button onclick="setCurrentLocation()">获取当前位置</button>。同时,确保在页面中正确引入了Tencent地图的JavaScript SDK。
阅读全文