一、搜索具體地址,自動填寫經緯度,並在地圖上標記javascript
二、點擊地圖上一點,可從新填寫經緯度而且標記java
<el-form-item label="店鋪地址" prop="address"> <el-input v-model="fristForm.address"></el-input> <el-input class="long-lat" v-model="fristForm.longitude" placeholder="經度" ></el-input> <el-input class="long-lat" v-model="fristForm.latitude" placeholder="緯度" ></el-input> <el-button size="mini" type="primary" @click="searchKeyword" >搜索</el-button > </el-form-item> <span class="changeAddress">點擊地圖更換分店定位地址</span> <!-- 渲染地圖的div容器 --> <div id="container" class="mapbox"></div>
var searchService,geocoder,map,markersArray = []; <script> export default { mounted() { this.init(); }, methods:{ init() { var that = this; var center = new qq.maps.LatLng(39.916527, 116.397128); var map = new qq.maps.Map(document.getElementById("container"), { center: center, zoom: 13 }); var latlngBounds = new qq.maps.LatLngBounds(); qq.maps.event.addListener(map, "click", function(event) { console.log(event); that.fristForm.longitude = event.latLng.getLng(); // 經度 that.fristForm.latitude = event.latLng.getLat(); // 緯度 if (markersArray) { for (let i in markersArray) { markersArray[i].setMap(null); } } var marker = new qq.maps.Marker({ map: map, position: event.latLng }); markersArray.push(marker); }); geocoder = new qq.maps.Geocoder({ complete: function(result) { console.log(result); that.fristForm.longitude = result.detail.location.lng; that.fristForm.latitude = result.detail.location.lat; map.setCenter(result.detail.location); var marker = new qq.maps.Marker({ map: map, position: result.detail.location }); markersArray.push(marker); } }); }, }, // 搜索地址 searchKeyword() { var keyword = this.fristForm.address; this.clearOverlays(markersArray); //根據輸入的城市設置搜索範圍 // searchService.setLocation("北京"); //根據輸入的關鍵字在搜索範圍內檢索 if (keyword) { // searchService.search(keyword); geocoder.getLocation(keyword); } else { alert("請輸入地址"); } }, } </script>
以上代碼使用的是jsapi功能,目前對應功能已升級JavaScript API GL,地址解析功能可直接調取接口使用,歡迎你們體驗!git
地址解析(地址轉座標)web
JavaScript API GL參考手冊segmentfault
做者:houqq連接:https://segmentfault.com/a/11...api
來源:segmentfaultdom
著做權歸做者全部。商業轉載請聯繫做者得到受權,非商業轉載請註明出處。ide