uniapp页面使用高德AMap.CitySearch

121 阅读1分钟
mounted() {
			if (window.AMap) {
							//todo 如果能访问到AMap直接初始化
							this.getAddress();
						} else {
							const script1=document.createElement('script')
							script1.innerHTML='window._AMapSecurityConfig = {securityJsCode: "7e0849fe23e1de536be90a76e19fad1a",};'
							document.head.appendChild(script1);
							//todo 动态引入
							const script = document.createElement('script');
							script.src = "https://webapi.amap.com/maps?v=1.4.15&key=fcf68206574a811aa228f0e83cd4a1e0&plugin=AMap.CitySearch";;
							script.onload = () => {
								//todo 初始化地图
								this.getAddress();
							}
							document.head.appendChild(script);
						}
		},
getAddress() {
				   //实例化城市查询类
				        var citysearch = new AMap.CitySearch();
				        //自动获取用户IP,返回当前城市
				        citysearch.getLocalCity(function(status, result) {
									console.log(status)
									console.log(result)
				            if (status === 'complete' && result.info === 'OK') {
				                if (result && result.city && result.bounds) {
				                    var cityinfo = result.city;
				                    var citybounds = result.bounds;
				                }
				            } else {
				            }
				        });
				}