|
|
@@ -0,0 +1,150 @@
|
|
|
+<template>
|
|
|
+ <div class="l-flex--col c-coordinate-picker u-relative">
|
|
|
+ <div
|
|
|
+ ref="map"
|
|
|
+ class="l-flex__fill"
|
|
|
+ />
|
|
|
+ <el-input
|
|
|
+ id="searchInput"
|
|
|
+ v-model.trim="posName"
|
|
|
+ class="c-coordinate-picker__search u-width--md"
|
|
|
+ placeholder="输入搜索地址"
|
|
|
+ suffix-icon="el-icon-search"
|
|
|
+ clearable
|
|
|
+ />
|
|
|
+ <el-input
|
|
|
+ v-model="posAddress"
|
|
|
+ placeholder="标注地址"
|
|
|
+ />
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script>
|
|
|
+import AMapLoader from '@amap/amap-jsapi-loader'
|
|
|
+
|
|
|
+export default {
|
|
|
+ name: 'CoordinatePicker',
|
|
|
+ props: {
|
|
|
+ lng: {
|
|
|
+ type: [String, Number],
|
|
|
+ default: null
|
|
|
+ },
|
|
|
+ lat: {
|
|
|
+ type: [String, Number],
|
|
|
+ default: null
|
|
|
+ },
|
|
|
+ address: {
|
|
|
+ type: String,
|
|
|
+ default: ''
|
|
|
+ }
|
|
|
+ },
|
|
|
+ data () {
|
|
|
+ return {
|
|
|
+ posName: '',
|
|
|
+ posAddress: ''
|
|
|
+ }
|
|
|
+ },
|
|
|
+ mounted () {
|
|
|
+ this.posAddress = this.address
|
|
|
+ if (this.lng !== null && this.lng !== '' && this.lat !== null && this.lat !== '') {
|
|
|
+ this.center = [Number(this.lng), Number(this.lat)]
|
|
|
+ } else {
|
|
|
+ this.center = null
|
|
|
+ }
|
|
|
+ this.initMap()
|
|
|
+ },
|
|
|
+ beforeDestroy () {
|
|
|
+ this.marker = null
|
|
|
+ this.map?.destroy()
|
|
|
+ this.map = null
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ geolocation () {
|
|
|
+ if (navigator.geolocation && !this.center) {
|
|
|
+ navigator.geolocation.getCurrentPosition(position => {
|
|
|
+ this.center = [position.coords.longitude, position.coords.latitude]
|
|
|
+ this.center && this.initMap()
|
|
|
+ }, err => {
|
|
|
+ console.log(err)
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ this.center && this.initMap()
|
|
|
+ }
|
|
|
+ },
|
|
|
+ updateMarker (AMap, pos) {
|
|
|
+ this.center = pos
|
|
|
+ if (this.marker) {
|
|
|
+ this.marker.setPosition(pos)
|
|
|
+ } else {
|
|
|
+ this.marker = new AMap.Marker({
|
|
|
+ icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png',
|
|
|
+ position: pos,
|
|
|
+ offset: new AMap.Pixel(-13, -30)
|
|
|
+ })
|
|
|
+ this.marker.setMap(this.map)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ initMap () {
|
|
|
+ AMapLoader.load({
|
|
|
+ key: process.env.VUE_APP_GAODE_MAP_KEY,
|
|
|
+ version: '2.0',
|
|
|
+ plugins: ['AMap.PlaceSearch', 'AMap.Geolocation', 'AMap.AutoComplete', 'AMap.Geocoder']
|
|
|
+ }).then(AMap => {
|
|
|
+ this.map = new AMap.Map(this.$refs.map, { center: this.center })
|
|
|
+ this.center && this.updateMarker(AMap, this.center)
|
|
|
+ this.map.setFitView()
|
|
|
+
|
|
|
+ const geocoder = new AMap.Geocoder({ extensions: 'base' })
|
|
|
+
|
|
|
+ this.map.on('click', e => {
|
|
|
+ console.log(e)
|
|
|
+ this.updateMarker(AMap, [e.lnglat.lng, e.lnglat.lat])
|
|
|
+ geocoder.getAddress(this.center, (status, result) => {
|
|
|
+ console.log(result)
|
|
|
+ this.posAddress = result.regeocode.formattedAddress || ''
|
|
|
+ })
|
|
|
+ this.map.setFitView()
|
|
|
+ })
|
|
|
+
|
|
|
+ const placeSearch = new AMap.PlaceSearch({ map: this.map })
|
|
|
+ new AMap.Autocomplete({
|
|
|
+ input: 'searchInput' // 绑定的搜索关键字的input标签ID,用这个注册
|
|
|
+ }).on('select', e => {
|
|
|
+ console.log(e)
|
|
|
+ if (e.poi.location) {
|
|
|
+ this.updateMarker(AMap, [e.poi.location.lng, e.poi.location.lat])
|
|
|
+ this.posAddress = e.poi.name
|
|
|
+ }
|
|
|
+ placeSearch.setCity(e.poi.adcode)
|
|
|
+ placeSearch.search(e.poi.name)
|
|
|
+ })
|
|
|
+ })
|
|
|
+ },
|
|
|
+ getValue () {
|
|
|
+ return {
|
|
|
+ lng: this.center ? `${this.center[0]}` : '',
|
|
|
+ lat: this.center ? `${this.center[1]}` : '',
|
|
|
+ address: this.posAddress
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+</script>
|
|
|
+
|
|
|
+<style lang="scss" scoped>
|
|
|
+.c-coordinate-picker {
|
|
|
+ position: relative;
|
|
|
+
|
|
|
+ &__search {
|
|
|
+ position: absolute;
|
|
|
+ top: $spacing;
|
|
|
+ left: $spacing;
|
|
|
+ }
|
|
|
+}
|
|
|
+</style>
|
|
|
+<style>
|
|
|
+.amap-sug-result {
|
|
|
+ z-index: 9999 !important;
|
|
|
+ border: none !important;
|
|
|
+}
|
|
|
+</style>
|