逆地理编码
This commit is contained in:
@@ -137,7 +137,7 @@
|
|||||||
|
|
||||||
<!-- 2. 加载地图和插件 (去掉了 Geolocation 插件,避免弹窗) -->
|
<!-- 2. 加载地图和插件 (去掉了 Geolocation 插件,避免弹窗) -->
|
||||||
<script
|
<script
|
||||||
src="https://webapi.amap.com/maps?v=2.0&key=2cc1d822e313307fe311c3127a1deeb5&plugin=AMap.MoveAnimation,AMap.Driving,AMap.TruckDriving,AMap.AutoComplete,AMap.ToolBar,AMap.Scale">
|
src="https://webapi.amap.com/maps?v=2.0&key=2cc1d822e313307fe311c3127a1deeb5&plugin=AMap.MoveAnimation,AMap.Driving,AMap.TruckDriving,AMap.AutoComplete,AMap.ToolBar,AMap.Scale,AMap.Geocoder">
|
||||||
</script>
|
</script>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
@@ -167,9 +167,11 @@
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
var map, marker, driving, truckDriving;
|
var map, marker, driving, truckDriving, geocoder;
|
||||||
var currentLat, currentLng;
|
var currentLat, currentLng;
|
||||||
var isTruckMode = false;
|
var isTruckMode = false;
|
||||||
|
var isInitialLocationSet = false;
|
||||||
|
|
||||||
|
|
||||||
function initMap() {
|
function initMap() {
|
||||||
map = new AMap.Map('container', {
|
map = new AMap.Map('container', {
|
||||||
@@ -178,6 +180,11 @@
|
|||||||
viewMode: '3D'
|
viewMode: '3D'
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// --- 2. 初始化 geocoder ---
|
||||||
|
geocoder = new AMap.Geocoder({
|
||||||
|
city: "全国" // 设置地理编码范围
|
||||||
|
});
|
||||||
|
|
||||||
// 通知 Flutter 地图加载完毕
|
// 通知 Flutter 地图加载完毕
|
||||||
map.on('complete', function () {
|
map.on('complete', function () {
|
||||||
console.log("JS->: Map is ready.");
|
console.log("JS->: Map is ready.");
|
||||||
@@ -230,6 +237,7 @@
|
|||||||
/**
|
/**
|
||||||
* 核心功能 1: 接收 Flutter 传来的定位数据
|
* 核心功能 1: 接收 Flutter 传来的定位数据
|
||||||
* Flutter 端调用: webViewController.evaluateJavascript("updateMyLocation(...)")
|
* Flutter 端调用: webViewController.evaluateJavascript("updateMyLocation(...)")
|
||||||
|
* 经度 维度
|
||||||
*/
|
*/
|
||||||
function updateMyLocation(lat, lng, angle) {
|
function updateMyLocation(lat, lng, angle) {
|
||||||
var rawLat = parseFloat(lat);
|
var rawLat = parseFloat(lat);
|
||||||
@@ -244,6 +252,7 @@
|
|||||||
currentLat = mPoint.lat;
|
currentLat = mPoint.lat;
|
||||||
var position = [currentLng, currentLat];
|
var position = [currentLng, currentLat];
|
||||||
|
|
||||||
|
// 更新车辆标记位置 (保持不变)
|
||||||
if (!marker) {
|
if (!marker) {
|
||||||
marker = new AMap.Marker({
|
marker = new AMap.Marker({
|
||||||
map: map,
|
map: map,
|
||||||
@@ -254,8 +263,6 @@
|
|||||||
angle: isNaN(rawAngle) ? 0 : rawAngle,
|
angle: isNaN(rawAngle) ? 0 : rawAngle,
|
||||||
});
|
});
|
||||||
map.setCenter(position);
|
map.setCenter(position);
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
marker.moveTo(position, {
|
marker.moveTo(position, {
|
||||||
duration: 1000,
|
duration: 1000,
|
||||||
@@ -263,6 +270,61 @@
|
|||||||
});
|
});
|
||||||
if (!isNaN(rawAngle)) marker.setAngle(rawAngle);
|
if (!isNaN(rawAngle)) marker.setAngle(rawAngle);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- 4. 逆地理编码并设置默认起点 ---
|
||||||
|
// 只有在第一次获取到位置时,才设置默认起点,避免覆盖用户手动输入的起点
|
||||||
|
if (!isInitialLocationSet) {
|
||||||
|
geocoder.getAddress(position, function (status, result) {
|
||||||
|
if (status === 'complete' && result.regeocode) {
|
||||||
|
let shortAddress = '';
|
||||||
|
const regeo = result.regeocode;
|
||||||
|
const addressComponent = regeo.addressComponent;
|
||||||
|
const pois = regeo.pois;
|
||||||
|
|
||||||
|
// 策略1: 优先使用最近的、类型合适的POI的名称
|
||||||
|
if (pois && pois.length > 0) {
|
||||||
|
// 查找第一个类型不是“商务住宅”或“地名地址信息”的POI,这类POI通常是具体的建筑或地点名
|
||||||
|
const significantPoi = pois.find(p => p.type.indexOf('商务住宅') === -
|
||||||
|
1 && p.type.indexOf('地名地址信息') === -1);
|
||||||
|
if (significantPoi) {
|
||||||
|
shortAddress = significantPoi.name;
|
||||||
|
} else {
|
||||||
|
// 如果找不到,就用第一个POI的名字
|
||||||
|
shortAddress = pois[0].name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 策略2: 如果没有POI,使用"道路+门牌号"
|
||||||
|
else if (addressComponent.street && addressComponent.streetNumber) {
|
||||||
|
shortAddress = addressComponent.street + addressComponent
|
||||||
|
.streetNumber;
|
||||||
|
}
|
||||||
|
// 策略3: 如果还没有,使用"区+乡镇"
|
||||||
|
else if (addressComponent.district) {
|
||||||
|
shortAddress = addressComponent.district + (addressComponent
|
||||||
|
.township || '');
|
||||||
|
}
|
||||||
|
// 策略4: 降级到使用完整的、但可能很长的地址
|
||||||
|
else {
|
||||||
|
shortAddress = regeo.formattedAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 如果拼接出的地址过长,可以再做一次截断
|
||||||
|
if (shortAddress.length > 20) {
|
||||||
|
// 可以在这里添加更复杂的截断逻辑,比如按关键字
|
||||||
|
shortAddress = regeo.formattedAddress.substring(0, 20) + '...';
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 将获取到的地址填充到起点输入框
|
||||||
|
document.getElementById('startInput').value = shortAddress;
|
||||||
|
isInitialLocationSet = true; // 标记为已设置,不再更新
|
||||||
|
} else {
|
||||||
|
// 如果逆地理编码失败,依然使用“当前位置”作为提示
|
||||||
|
document.getElementById('startInput').placeholder = "当前位置";
|
||||||
|
console.error('逆地理编码失败:', result);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user