vue + 天地图搜索api+样式

最终实现的效果图
在这里插入图片描述

html部分

理论上应该用天地图js4.0api,但我用的时候总是报MIME错误,所以我就用的3.0

<script src="http://api.tianditu.gov.cn/api?v=3.0&tk=天地图的key" type="text/javascript"></script>
<div class="u-width-61 siteItem">
    <div id="map"></div>
    <div class="select_class">
        <el-input clearable v-model="keyword" placeholder="请输入关键词" @keyup.enter.native="searchR" @clear="searchR">
            <el-button slot="append" icon="el-icon-search" @click="searchR"></el-button>
        </el-input>
        <div class="my-dropdown1" v-show="visible">
            <div class="list-item1" v-for="item in cityOptions" :key="item.value" @click="searchR(item)">
                <span style="color: #409EFF;margin-right: 10px;">{{ item.label }}</span>
                <span>({{ item.count }})</span>
            </div>
            <div class="list-item2" v-for="item in addressOptions" :key="item.value" @click="searchR(item)">
                <i class="el-icon-map-location"></i>
                <div>
                    <span>{{ item.label }}</span>
                    <p>{{ item.address }}</p>
                </div>
            </div>
        </div>
    </div>
</div>

css部分

.siteItem{
	height:600px;
    border: 1px solid #ebeef5;
    border-radius: 6px;
    position: relative;
    #map{
        width: 100%;
        height: 100%;
    }
}
.select_class{
    position: absolute;
    top: 6px;
    left: 6px;
    z-index: 400;
    .my-dropdown1 {
        max-height: 400px;
        overflow: auto;
        border-radius: 4px;
        border: 1px solid #dcdfe6;
        background-color: #fff;
    }
    .list-item1{
        cursor: pointer;
        margin-bottom: 4px;
        padding: 10px 15px;
    }
    .list-item2{
        display: flex;
        border-bottom:1px solid #eaeaea;
        padding: 15px;
        margin-bottom: 15px;
        p{margin: 6px 0 0;color: #909399;font-size: 12px;}
        i{font-size: 24px;color: #409EFF;margin-right: 6px;}
        background-color:#fff;
        transition: background-color .3s;
        cursor: pointer;
        &:hover{
            i{color: #f00;}
            background-color:#fbfbfb;
        }
    }
}

js部分

searchR(item){
    if(!this.keyword){
        this.cityOptions = [];
        this.addressOptions = [];
        return;
    }
    let params = {
keyWord:this.keyword,"level":3,"mapBound":"47.95898437500239,7.759699085623154,162.04101562500443,56.875842480765385","queryType":1,"start":0,"count":10
    };
    if(item.value) params['specify'] = item.value;
    HttpR('https://api.tianditu.gov.cn/v2/search?postStr='+JSON.stringify(params)+'&type=query&tk='+tdtMapKey).then(r => {
        let data = r.data;
        this.cityOptions = [];
        this.addressOptions = [];
        this.visible = true;
        if(data.resultType == 2){
            this.addressOptions = [];
            this.cityOptions = data.statistics.allAdmins.map(v => ({ label:v.adminName,value:v.adminCode,count:v.count,lonlat:v.lonlat,type:2,isleaf:v.isleaf }));
        }else if(data.resultType == 1){
            this.cityOptions = [];
            this.addressOptions = data.pois.map(v => ({ label:v.name,value:v.nid,address:v.address,lonlat:v.lonlat,type:1 }));
        }else if(data.resultType == 3){
            let [ lng, lat ] = data.area.lonlat.split(',');
            this.map.setView([ lat, lng ],10)
        }else{
            this.visible = false;
        }
    })
},
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue Leaflet 是一种结合了Vue框架和Leaflet库的前端技术,用于展示和操作地图。地图是一种具有高清影像和矢量数据的地图服务,提供了丰富的地理信息资源和功能,如地图展示、地图操作、定位导航等。 Vue Leaflet 可以通过调用地图的API接口,获取并展示地图的各类地理信息。通过Vue的组件化开发方式,可以方便地在Vue项目中使用这些地理信息,实现自定义的地图功能。例如,在Vue Leaflet 中可以实现地图、标记点、线段、面等地理要素的显示和编辑。 Vue Leaflet 提供了一套方便易用的API和组件,可以轻松地在Vue项目中集成和使用地图。比如,可以使用Vue Leaflet 提供的地图组件将地图展示在网页中,可以使用它提供的标记点组件在地图上添加标记,可以使用它提供的工具条组件进行地图的操作和导航等。 使用Vue Leaflet 可以有效地提高开发效率和用户体验。通过其简洁的API和灵活的组件,开发人员可以快速地实现各种地图需求,如显示地图、标记地点、展示线段等。并且,Vue Leaflet 结合了Vue框架的优势,可以更好地组织和管理地图相关的逻辑代码,使开发工作更加方便和高效。 总之,Vue Leaflet 是一种方便、灵活和高效的前端技术,用于展示和操作地图。它通过结合Vue框架和Leaflet库,提供了一套方便易用的API和组件,帮助开发人员快速实现各种地图需求,提高开发效率和用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值