一、獲取key及在index.htm中引入
- 首先需要成爲高德開發者,申請到適合項目的
key
.並在index.html
中進行引入
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.3&key=d79ff396531b948ce14d5be1c733fc36"></script>
二、在配置文件中進行相應配置
- 根據
vue
腳手架的不用需要在不同的文件中進行配置。
- 我項目使用的是
cli3
的腳手架,需要在Vue.config.js
中進行高德地圖配置
externals: {
'AMap': 'AMap'
}
三、在需要用到的地方進行地圖初始化及定位操作
- 因項目需求只需要在註冊頁面進行默認定位,故只引用一次就行。並沒有單獨的抽離出來,可以根據項目的需求進行抽離。
import AMap from "AMap";
data() {
return {
locationData: {
lat: "",
lon: "",
province: "",
city: "",
district: "",
nowPlace: "",
Address: ""
}
};
},
methods:{
getLocation() {
const self = this;
AMap.plugin("AMap.Geolocation", function() {
var geolocation = new AMap.Geolocation({
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0,
convert: true
});
geolocation.getCurrentPosition();
AMap.event.addListener(geolocation, "complete", onComplete);
AMap.event.addListener(geolocation, "error", onError);
function onComplete(data) {
console.log("定位成功信息:", data);
self.newGetAddress(data.position.lat, data.position.lng);
}
function onError(data) {
console.log("定位失敗錯誤:", data);
self.getLngLatLocation();
}
});
},
getLngLatLocation() {
const self = this;
AMap.plugin("AMap.CitySearch", function() {
var citySearch = new AMap.CitySearch();
citySearch.getLocalCity(function(status, result) {
if (status === "complete" && result.info === "OK") {
console.log("通過ip獲取當前城市:", result);
AMap.plugin("AMap.Geocoder", function() {
var geocoder = new AMap.Geocoder({
city: result.adcode
});
var lnglat = result.rectangle.split(";")[0].split(",");
geocoder.getAddress(lnglat, function(status, data) {
if (status === "complete" && data.info === "OK") {
console.log(data);
self.userInfo.ProvinceName = data.regeocode.addressComponent.province.toString();
self.userInfo.CCityName =
data.regeocode.addressComponent.city;
self.userInfo.RegionName =
data.regeocode.addressComponent.district;
}
});
});
}
});
});
},
newGetAddress: function(latitude, longitude) {
const _thisSelf = this;
_thisSelf.locationData.lat = latitude;
_thisSelf.locationData.lon = longitude;
const mapObj = new AMap.Map("mapAmap1");
mapObj.plugin("AMap.Geocoder", function() {
const geocoder = new AMap.Geocoder({
city: "全國",
radius: 100
});
const lnglat = [longitude, latitude];
const reg1 = /^[1][1][0][0-9][0-9][0-9]/;
const reg2 = /^[1][2][0][0-9][0-9][0-9]/;
const reg3 = /^[5][0][0][0-9][0-9][0-9]/;
const reg4 = /^[3][1][0][0-9][0-9][0-9]/;
geocoder.getAddress(lnglat, function(status, result) {
console.log("getAddress", result);
if (status === "complete" && result.info === "OK") {
const adcode = result.regeocode.addressComponent.adcode;
if (
reg1.test(adcode) ||
reg2.test(adcode) ||
reg3.test(adcode) ||
reg4.test(adcode)
) {
_thisSelf.locationData.city =
result.regeocode.addressComponent.province;
} else {
_thisSelf.locationData.city =
result.regeocode.addressComponent.city;
}
_thisSelf.locationData.province =
result.regeocode.addressComponent.province;
_thisSelf.locationData.district =
result.regeocode.addressComponent.district;
_thisSelf.locationData.Address = result.regeocode.formattedAddress;
_thisSelf.locationData.nowPlace =
result.regeocode.addressComponent.province +
result.regeocode.addressComponent.city +
result.regeocode.addressComponent.district;
_thisSelf.userInfo.ProvinceName = _thisSelf.locationData.province;
_thisSelf.userInfo.CCityName = _thisSelf.locationData.city;
_thisSelf.userInfo.RegionName = _thisSelf.locationData.district;
} else {
console.log(_thisSelf.locationData);
}
});
});
}
},
created() {
this.getLocation();
}
- 至此整個定位的實現全部結束,可以準確的獲取到當前所在的省市區。