vue 地图找房功能

<template>
    <div class="mapSeekShop">
        <div class="nav clearfix">
            <img class="navBack fl" src="../../../static/images/common/back.png" alt="" @click="navBack()">
            <h2 class="navTitle fl">地图找房</h2>
        </div>
        <div id="container"></div>
        <!-- <div class="containerTab"> -->
        <ul  class="containerTab">
            <li @click="sellBtn" :class="{'lastLi':tab===0}">销售</li>
            <li @click="merchantsBtn" :class="{'lastLi':tab===1}">招商</li>
        </ul>
        <!-- </div> -->
    </div>  
</template>

<script>
import $ from '../../../static/js/jquery.min.js';
import axios from 'axios';
import qs from 'qs';
export default {
name: "mapSeekShop",
    data () {
        return {
            sessionCityId: "110100",  // 全局城市Id
            sessionProvinceId: "11000", // 全局省Id
            seekMapArr: [],  
            tab: 1
        }
    },
    computed:{

    },
    methods:{
        // 点击返回按钮
        navBack(){        
            this.$router.push({path: '/home'});            
        },
        seekMapData(type){
            let url = this.changeData() + '/project/projectOnMap'
            axios(url,{
                method: 'post',
                params: {
                    cityId: this.sessionCityId || 110100, //所属城市【必传】
                    projectType: type, // 项目类型【必传】,1是销售、2是招商
                }
            }).then(data => {
                // console.log(data);
                var lnglats = data.data.projectList;
                // console.log(this.seekMapArr);
                // 百度地图API功能
                var mp = new BMap.Map("container");
                mp.centerAndZoom(new BMap.Point(116.4573195952, 39.911365), 11);
                mp.enableScrollWheelZoom();
                var geoc = new BMap.Geocoder();//逆地址解析
                // 复杂的自定义覆盖物
                function ComplexCustomOverlay(point, text){
                    this._point = point;
                    this._text = text;
                }
                // 继承API的BMap.Overlay 
                ComplexCustomOverlay.prototype = new BMap.Overlay();
                // 实现初始化方法 
                ComplexCustomOverlay.prototype.initialize = function(map){
                    this._map = map;
                    var div = this._div = document.createElement("div");
                    div.style.position = "absolute";
                    div.style.zIndex = BMap.Overlay.getZIndex(this._point.lat);
                    div.style.backgroundColor = "#84C0F7";
                    div.style.border = "1px solid #84C0F7";
                    div.style.color = "white";
                    div.style.height = ".4rem";
                    div.style.borderRadius = "5px";
                    div.style.padding = ".02rem .05rem";
                    div.style.lineHeight = ".4rem";
                    div.style.whiteSpace = "nowrap";
                    div.style.MozUserSelect = "none";
                    div.style.fontSize = ".24rem"
                    var span = this._span = document.createElement("span");
                    div.appendChild(span);     
                    span.appendChild(document.createTextNode(this._text));
                    var that = this;

                    var arrow = this._arrow = document.createElement("div");
                    arrow.style.background = "url(../../../static/images/home/blueSquare.png) no-repeat";
                    arrow.style.position = "absolute";
                    arrow.style.width = ".8rem";
                    arrow.style.height = ".2rem";
                    arrow.style.top = ".42rem";
                    arrow.style.left = "50%";
                    arrow.style.marginLeft = "-.1rem";
                    arrow.style.overflow = "hidden";
                    div.appendChild(arrow);

                    mp.getPanes().labelPane.appendChild(div);

                    return div;
                }
                // 实现绘制方法
                ComplexCustomOverlay.prototype.draw = function(){
                    var map = this._map;
                    // 根据地理坐标转换为像素坐标,并设置给容器
                    var pixel = map.pointToOverlayPixel(this._point);
                    this._div.style.left = pixel.x - parseInt(this._arrow.style.left) + "px";
                    this._div.style.top  = pixel.y - 30 + "px";
                }
                //在调用聚合方法时会将会调用标注的getPosition方法
                //获取该覆盖物的位置
                ComplexCustomOverlay.prototype.getPosition = function () {       
                    return this._point;
                };
                // 自定义覆盖物添加事件方法
                ComplexCustomOverlay.prototype.addEventListener = function (event, fun) {
                    this._div['on' + event] = fun;
                }
            
                for (var i = 0; i < lnglats.length; i++) {
                    //测试定位//开始自定义覆盖物
                    var typeName;
                    if(lnglats[i].projectType == 1){
                        typeName = "销售";
                    }else{
                        typeName = "招商";
                    }

                    var myCompOverlay = new ComplexCustomOverlay(new BMap.Point(lnglats[i].lng,lnglats[i].lat), typeName+"-"+lnglats[i].projectName,lnglats[i].projectId);
                    // console.log(myCompOverlay);
                    mp.addOverlay(myCompOverlay);  // 将标注添加到地图中
                    //显示定位图标   
                    myCompOverlay.addEventListener("touchend", createClickEvent(lnglats[i].projectId,lnglats[i].projectType,lnglats[i].houseType)); //覆盖物点击事件
                }
                //不使用覆盖物,使用百度标注
                // var marker = new BMap.Marker(new BMap.Point(104.082042, 30.663608));  // 创建标注
                // mp.addOverlay(marker);
                // marker.addEventListener("touchend", createClickEvent(marker));
                function createClickEvent(id,type,houseType){
                    return function (e) {
                        if(type == 1){
                            window.location.href = "./sellDetail?id="+id+"&url=mapSeekShop&type=1&houseType="+houseType;
                        }else{
                            window.location.href = "./merchantsDetail?id="+id+"&url=mapSeekShop&type=2&houseType="+houseType;
                        }
                    }
                }
            }).catch(err => {
                console.log(err)
            });
        },
        // 点击销售按钮
        sellBtn(){
            this.tab = 1;
            this.seekMapData(1);
        },
        // 点击招商按钮
        merchantsBtn(){
            this.tab = 0;
            this.seekMapData(2);
        }
    },
    mounted() {
        this.sessionCityId = sessionStorage.cityId;
        this.sessionProvinceId = sessionStorage.provinceId;

        //截取Url里面的参数
        function GetRequest() {
            var urlInfo = location.search; //获取url中"?"符后的字串
            var theRequest = new Object();
            if (urlInfo.indexOf("?") != -1) {
                var str = urlInfo.substr(1);
                var strs = str.split("&");
                for (var i = 0; i < strs.length; i++) {
                    theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
                }
            }
            return theRequest;
        }
        //通过url取数
        var request = new Object();
        request = GetRequest();
        var typeIndex = request['type'];
        if(typeIndex){
            if(typeIndex == "1"){
                this.tab = 1;
                this.seekMapData(1);
            }else if(typeIndex == "2"){
                this.tab = 0;
                this.seekMapData(2);
            }
        }else{
            this.seekMapData(1);
        }
    },
    created(){ 
        
    }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import "../../../static/css/commonNav.css"; 

.mapSeekShop{
   width: 7.5rem;
   margin: 0 auto;
}
#container{
    width: 7.5rem;
    margin: 0 auto;
}
.containerTab{
    width: 2.3rem;
    height: .4rem;
    border: 1px solid #ebebeb;
    position: fixed;
    bottom: .72rem;
    left: .3rem;
    background: #fff;
    padding-top: .1rem;
    padding-bottom: .1rem;
    border-radius: 6px;
    box-shadow: 2px 0px 12px rgba(254,254,254,.6);
    li{
        width: 50%;          
        color: #84C0F7;
        float: left;
        text-align: center;
        font-size: .28rem;

    }
    .lastLi{
        color: #666;
        border-left: 1px solid #ebebeb;
        box-sizing: border-box;
    }
}
</style>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue可以通过使用第三方地图API来实现地图定位功能,例如使用高德地图API或百度地图API。 以下是使用高德地图API实现地图定位功能的步骤: 1. 在index.html文件中引入高德地图API的JS文件: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> ``` 2. 在Vue组件中创建地图容器: ```html <template> <div id="map-container"></div> </template> ``` 3. 在Vue组件的mounted()钩子函数中初始化地图: ```js mounted() { // 初始化地图 const map = new AMap.Map('map-container', { zoom: 11, center: [116.397428, 39.90923] }); // 添加定位插件 map.plugin('AMap.Geolocation', function() { const geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认:true timeout: 10000, // 超过10秒后停止定位,默认:无穷大 maximumAge: 0, // 定位结果缓存0毫秒,默认:0 convert: true, // 自动偏移坐标,偏移后的坐标为高德坐标,默认:true showButton: true, // 显示定位按钮,默认:true buttonPosition: 'LB', // 定位按钮停靠位置,默认:'LB',左下角 buttonOffset: new AMap.Pixel(10, 20), // 定位按钮距离容器左下角的偏移量,默认:Pixel(10, 20) showMarker: true, // 定位成功后在定位到的位置显示点标记,默认:true showCircle: true, // 定位成功后用圆圈表示定位精度范围,默认:true panToLocation: true, // 定位成功后将定位到的位置作为地图中心点,默认:true zoomToAccuracy: true // 定位成功后自动调整地图视野到定位点,用于范围较小的定位,默认:false }); map.addControl(geolocation); geolocation.getCurrentPosition(); }); } ``` 4. 在Vue组件的style标签中设置地图容器的样式: ```css #map-container { width: 100%; height: 400px; } ``` 以上代码实现了在Vue组件中显示一个地图容器,并在mounted()钩子函数中初始化地图,并添加了高德地图的定位插件,实现地图定位的功能
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值