123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- <template>
- <view class="setbox">
- <map
- id="container"
- class="map"
- :latitude="center.lat"
- :longitude="center.lng"
- @poitap="onClcikImgMap"
- @tap="onClcikImgMap"
- :markers="covers"
- scale="15"
- :style="'width: 750rpx;height:' + mapheight + 'px;'"
- ></map>
- <view class="maptop">
- <view class="maptopflex">
- <image :src="serimg" class="maptopimg"></image>
- <input ref='input' class="maptopint" id="tipInputs" v-model="searchtxt" @blur="getBlur" @input="searchKeyword" placeholder="请输入地点进行查找" />
- </view>
- <!-- 地图联想 -->
- <view class="addrSearch_list" >
- <view class="addrSearch_item" v-for="(item, index) in searchList" :key="index" @click="handleAddrClick(item)" >
- <view class="wrap_name">
- <!-- #ifdef H5 -->
- <text class="wrap_text">{{item.name.substring(0, item.name.indexOf(searchtxt))}}</text>
- <text class="wrap_text" style="color: #2797FF;">{{searchtxt}}</text>
- <text class="wrap_text">{{item.name.substring(item.name.indexOf(searchtxt) + searchtxt.length)}}</text>
- <!-- #endif -->
- <!-- #ifndef H5 -->
- <rich-text :nodes="richNodes(item)"></rich-text>
- <!-- #endif -->
- </view>
- <view class="wrap_addr">
- <text class="wrap_addrs">{{ item.addr }}</text>
- </view>
- </view>
- </view>
- </view>
- <view class="mapfot">
- <view class="mapfott"><text class="mapfottit">标点位置</text></view>
- <view class="mapfotx">
- <image :src="adrimg" class="mapfotimg"></image>
- <text class="mapfottxt" v-if="address">{{ address }}</text>
- </view>
- <view class="mapfotn" @click="getSureFn"><text class="mapfotntit">确定</text></view>
- </view>
- </view>
- </template>
- <script>
- import myAmapFun from '@/utils/initmap.js';
- import amapFile from '@/plugins/amap-wx.js';
- import self from "@/utils/location.js";
- // #ifdef APP-PLUS
- // var myAmapFun = new amapFile.AMapWX({
- // key: 'ccbe52b1ec5f66295fa4609c90a819b7',
- // batch: true
- // });
- // #endif
- export default{
- data(){
- return{
- serimg:require("@/mine/static/score/serch.png"),
- adrimg:require("@/mine/static/score/adress.png"),
- mapimg:require("@/mine/static/score/map.png"),
- // center:{
- // lng:'117.211954',
- // lat:'31.839676',
- // },
- center:{
- lng:'',
- lat:'',
- },
- mapflag:false,
- // userId: this.$store.state.user.userId,
- // deptId: this.$store.state.user.deptId,
- address:'',
- searchtxt:'',
- type:'add',//新增还是修改,
- placeSearchComponent:'',
- autoCompleteComponent:'',
- map:'',
- covers: [{
- id:0,
- latitude: '',
- longitude: '',
- width:68, //宽
- height:68, //高
- iconPath: require("@/mine/static/score/map.png"),
- anchor:{x: 0.5, y: 0.5}
- }],
- mapheight:0,
- mapContext:"",
- searchList:[],
- focus:false,
- location:'',
- setflag:false,
- }
- },
- onReady() {
- this.getcreateMap()
- },
- onShow() {
- var that=this;
- if(this.setflag){
- this.setflag=false;
- // #ifndef H5
- this.getlocationadr()
- // #endif
- }
- },
- onLoad(e) {
- if(e.type=='update'){
- this.type='update'
- this.center.lng=e.lng;
- this.center.lat=e.lat;
- this.address=e.address;
- }else{
- if(e.address){
- this.type='update'
- this.center.lng=e.lng;
- this.center.lat=e.lat;
- this.address=e.address;
- }else{
- this.type='add';
- }
-
- }
- if(e.fromurl){
- this.fromurl=e.fromurl
- }
- },
- mounted() {
- var that=this;
- this.getinfo();
- if(this.type=='add'){
- this.getlocationadr()
- }else{
- var lng=this.center.lng;
- var lat=this.center.lat;
- that.covers = [];
- that.covers = [{
- id:1,
- latitude: lat,
- longitude: lng,
- width:68, //宽
- height:68, //高
- iconPath: that.mapimg,
- anchor:{x: 0.5, y: 0.5}
- }]
- // #ifdef APP
- that.getCity(lat, lng)
- // #endif
- }
- },
- methods:{
- getBlur(){
-
- },
- getSureFn(){
- var address={
- address:this.address,
- lng:this.center.lng,
- lat:this.center.lat,
- }
- uni.$emit('refreshaddr',address);
- uni.navigateBack({delta: 1});
- },
- getNextFn(){
- var address={
- address:this.address,
- lng:this.center.lng,
- lat:this.center.lat,
- }
- uni.$emit('refreshaddr',address);
- uni.navigateBack({delta: 1});
- },
- searchKeyword(){
- var that=this;
- if(this.searchtxt){
- // #ifdef H5
- AMap.plugin('AMap.PlaceSearch', function(){
- let autoOptions = {
- city:'全国',
- citylimit: true,
- pageIndex: 1,
- pageSize: 20,
- }
- let placeSearch = new AMap.PlaceSearch(autoOptions);
- placeSearch.search(that.searchtxt, function(status, result) {
- // 搜索成功时,result即是对应的匹配数据
- if (status === 'complete' && result.info === 'OK') {
- let items = result.poiList.pois;
- if(items.length > 0){
- var pois = [];
- for (let i in items) {
- var obj = {
- "name": items[i].name,
- "addr": items[i].address,
- "lng": items[i].location.lng,
- "lat": items[i].location.lat
- };
- pois.push(obj);
- }
- that.searchList = pois;
- }
- }else{
- that.searchList = [];
- }
- })
- })
- // #endif
- // #ifndef H5
- myAmapFun.getInputtips({
- keywords: that.searchtxt,
- city: '全国',
- citylimit: true,
- success: function(result){
- if(result.tips.length > 0){
- let pois = [];
- let items = result.tips;
- for (let i in items) {
- if(items[i].location&&items[i].location.length>0){
- var lnglat = items[i].location.split(',');
- var obj = {
- "name": items[i].name,
- "addr": items[i].district + items[i].address,
- "lng": lnglat[0],
- "lat": lnglat[1]
- };
- pois.push(obj);
- }
- }
- that.searchList = pois;
- }else{
- that.searchList = [];
- }
- },
- fail: function(info){
- that.searchList = [];
- console.log('fail-search', info)
- }
- })
- // #endif
- }
- },
- handleAddrClick(addr){
- var that=this;
- var lng=addr.lng;
- var lat=addr.lat;
- that.center.lng=lng;
- that.center.lat=lat;
- that.covers = [];
- that.covers = [{
- id:2,
- width:68, //宽
- height:68, //高
- latitude: addr.lat,
- longitude: addr.lng,
- iconPath: that.mapimg,
- anchor:{x: 0.5, y: 0.5}
- }]
- // #ifdef APP
- that.address=addr.name || addr.addr;
- that.searchList=[];
- that.$refs.input.blur()
- that.getCity(lat, lng)
- // #endif
- },
- richNodes(item){
- return [{
- children: [
- {
- type: 'text',
- attrs: {
- class:'wrap_text'
- },
- text: item.name.substring(0, item.name.indexOf(this.searchtxt))
- },
- {
- type: 'text',
- attrs: {
- class:'wrap_texts'
- },
- text: this.searchtxt
- },
- {
- type: 'text',
- attrs: {
- class:'wrap_text'
- },
- text: item.name.substring(item.name.indexOf(this.searchtxt) + this.searchtxt.length)
- }
- ]
- }]
- },
- // 获取当前屏幕信息
- getinfo(){
- var that=this;
- uni.getSystemInfo({
- success(e) {
- that.mapheight=e.windowHeight;
- },
- })
- },
- getcreateMap(){
- //mapId 就是你在 map 标签中定义的 id
- this.mapContext = uni.createMapContext('container', this);
- },
- onClcikImgMap(e) {
- var that=this;
- var lng=e.detail.longitude;
- var lat=e.detail.latitude;
- that.center.lng=lng;
- that.center.lat=lat;
- that.covers = [];
- that.covers = [{
- id:3,
- width:68, //宽
- height:68, //高
- latitude: e.detail.latitude,
- longitude: e.detail.longitude,
- iconPath: that.mapimg,
- anchor:{x: 0.5, y: 0.5}
- }]
- // #ifdef APP
- that.getCity(lat, lng)
- // #endif
- },
- //获取中心点
- getCenterLanLat() {
- var that = this;
- this.mapContext.getCenterLocation({
- type: 'gcj02',
- success: res => {
- that.center.lng=res.longitude;
- that.center.lat=res.latitude;
- that.covers = [];
- that.covers = [{
- id:4,
- width:68, //宽
- height:68, //高
- latitude: e.detail.latitude,
- longitude: e.detail.longitude,
- iconPath: that.mapimg,
- anchor:{x: 0.5, y: 0.5}
- }]
- // #ifdef APP
-
- that.getCity(res.latitude, res.longitude)
- // #endif
- },
- fail: err => {
- console.log('获取当前地图中心的经纬度2', err);
- },
- });
- },
- getCity(latitude, longitude,idx) {
- var that = this;
- that.location = `${longitude},${latitude}`
- myAmapFun.getRegeo({
- //如果经纬度有问题会导致不进入回调方法,从而报错
- location: that.location,
- success: function(e) {
- //成功回调
- that.address='';
- that.address = e[0].regeocodeData.formatted_address; //详细地址
- // 存起来
- if(idx&&idx==1){
- var newobj={
- lng:longitude,
- lat:latitude,
- address:that.address
- }
- self.setUxLocation(newobj)
- }
- },
- fail: function(info) {
- //失败回调
- console.log(info)
- }
- })
- },
- // 获取定位
- getlocationadr(){
- var that=this;
- self.getLocation(function(res){
- if(res==-1){
- that.setflag=true
- }else{
- that.center.lng=res.lng;
- that.center.lat=res.lat;
- that.address=res.address;
- that.covers = [];
- that.covers = [{
- id:4,
- width:68, //宽
- height:68, //高
- latitude: res.lat,
- longitude: res.lng,
- iconPath: that.mapimg,
- anchor:{x: 0.5, y: 0.5}
- }]
- var newobj={
- lng:res.lng,
- lat:res.lat,
- address:res.address
- }
- self.setUxLocation(newobj)
- }
- })
- },
- getlocation(){
- var that = this;
- uni.getLocation({
- type: 'gcj02',
- geocode: true,
- success: function(res) {
- that.center.lng=res.longitude;
- that.center.lat=res.latitude;
- that.covers = [];
- that.covers = [{
- id:4,
- width:68, //宽
- height:68, //高
- latitude: res.latitude,
- longitude: res.longitude,
- iconPath: that.mapimg,
- anchor:{x: 0.5, y: 0.5}
- }]
- // #ifdef MP-WEIXIN
- that.getCity(res.latitude, res.longitude,1)
- // #endif
-
- // #ifdef H5
- var adr= address.province+address.city+address.district+address.street+address.streetNum+address.poiName
- // that.address=res.longitude
- that.address=adr
- // #endif
- // #ifdef APP
- that.getCity(res.latitude, res.longitude)
- // #endif
- },
- fail: function(red) {
- uni.showToast({
- title:"获取定位失败"
- })
- },
- })
- },
- // 点击刷新方法
- refresh(e){
- this.getlocation();
- },
- }
- }
- </script>
- <style lang="scss" scoped>
- .maptop {
- position: fixed;
- left: 34rpx;
- right: 34rpx;
- top: 24rpx;
- }
- .maptopflex {
- display: flex;
- align-items: center;
- padding: 0 32rpx;
- box-sizing: border-box;
- flex-direction: row;
- width: 684rpx;
- height: 84rpx;
- background: #ffffff;
- box-shadow: 0px 0px 12rpx 0px rgba(75, 75, 75, 0.35);
- border-radius: 6rpx;
- margin-bottom: 10rpx;
- }
- .maptopimg {
- width: 36rpx;
- height: 38rpx;
- margin-right: 16rpx;
- }
- .maptopint {
- font-size: 32rpx;
- color: #666666;
- flex: 1;
- height: 84rpx;
- }
- .mapfot {
- position: fixed;
- left: 34rpx;
- right: 34rpx;
- bottom: 24rpx;
- background: #fff;
- box-shadow: 0px 0px 12rpx 0px rgba(75, 75, 75, 0.35);
- border-radius: 6rpx;
- padding: 40rpx 32rpx 32rpx;
- min-height: 300rpx;
- }
- .mapfott {
- margin-bottom: 28rpx;
- }
- .mapfottit {
- font-size: 33rpx;
- font-weight: bold;
- color: #343434;
- }
- .mapfotx {
- display: flex;
- margin-bottom: 32rpx;
- min-height: 70rpx;
- flex-direction: row;
- }
- .mapfotimg {
- width: 26rpx;
- height: 28rpx;
- flex: 0 0 auto;
- margin-right: 12rpx;
- }
- .mapfottxt {
- flex: 1;
- font-size: 32rpx;
- color: #343434;
- line-height: 32rpx;
- }
- .mapfotn {
- width: 618rpx;
- height: 76rpx;
- background: #1678ff;
- border-radius: 6rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- }
- .mapfotntit {
- font-size: 34rpx;
- color: #fff;
- }
- .addrSearch_item {
- border-bottom: 2upx solid #f5f5f5;
- padding: 4upx;
- background: #ffffff;
- }
- .addrSearch_item .wrap_name {
- display: flex;
- flex-direction: row !important;
- }
- .wrap_text {
- font-size: 34upx;
- color: #151515;
- line-height: 40upx;
- }
- .wrap_texts{
- font-size: 34upx;
- color: #2797FF;
- line-height: 40upx;
- }
- .addrSearch_item .wrap_addr {
- margin-top: 2upx;
- }
- .wrap_addrs {
- font-size: 28upx;
- color: #999;
- line-height: 32upx;
- }
- </style>
|