ソースを参照

map增加地点定位等功能

李林森 7 年 前
コミット
922f055d00
2 ファイル変更253 行追加38 行削除
  1. 35 13
      src/demo/map.vue
  2. 218 25
      src/package/map/src/map.vue

+ 35 - 13
src/demo/map.vue

@@ -4,7 +4,8 @@
         <nut-demoheader 
         :name="$route.name"
         ></nut-demoheader>
-        <nut-map class="map-box" :option="options"></nut-map>
+        <nut-map class="map-box" :option="map1"></nut-map>
+         <!-- <nut-map :id="test2" class="map-box" :option="options" :markers="markers"></nut-map> -->
         <!-- DEMO区域 -->
     </div>
 </template>
@@ -12,18 +13,39 @@
 <script>
 export default {
     data(){
-        return{
-            options:{
-                center:[39.914850, 116.403765],
-                zoom: 15,
-                minZoom:2,                
-                maxZoom:18,
-                mapZoomType:'CENTER',
-                MapTypeId:'SATELLITE',
-                draggable: true,
-                scrollwheel: true,
-                disableDoubleClickZoom: true
+        return{    
+            map1:{
+                id:'test1',        
+                options:{                
+                    center:[39.914850, 116.403765],
+                    zoom: 15,
+                    minZoom:2,                
+                    maxZoom:18,
+                    mapZoomType:'CENTER',
+                    //MapTypeId:'SATELLITE',
+                    draggable: true,
+                    scrollwheel: true,
+                    disableDoubleClickZoom: true
+                },
+                markers:[
+                    {
+                        //设置Marker的位置坐标
+                        position:[39.914850, 116.403765],                
+                        animation:'BOUNCE', //'BOUNCE'反复弹跳'DOWN'落下,'DROP'从天而降,'UP'升起  
+                        //设置Marker可拖动
+                        draggable: true,
+                        //自定义Marker图标为大头针样式
+                        icon:"../../asset/img/cases/fuli.png",
+                        //自定义Marker图标的阴影
+                        //shadow:"",
+                        //设置Marker标题,鼠标划过Marker时显示
+                        title: '测试',
+                        //设置Marker的可见性,为true时可见,false时不可见
+                        visible: true
+                    }
+                ]
             }
+
         }
     },
     methods:{
@@ -34,6 +56,6 @@ export default {
 <style lang="scss">
 .map-box{
     width: 100%;
-    height: 200px;
+    height: 500px;
 }
 </style>

+ 218 - 25
src/package/map/src/map.vue

@@ -1,5 +1,15 @@
 <template>
-    <div class="nut-map" id="nutuiMapTencent"></div>
+    <div class="nutui-mapbox">
+        <div class="search"><input type="text" @keyup="searchKeyword" v-model="Keyword"/></div>
+        <div class="nut-map" :id="option.id"></div>
+    </div>
+    
+    <!-- 
+    1、当前位置的定位,获取当前经纬度
+    2、可以安装传入的经纬度显示位置。
+    3、搜索功能,搜到然后标记出来。(多标记功能) 
+    4、获取不到经纬度给一个友情提示
+    -->
 </template>
 <script>
 export default {
@@ -13,49 +23,232 @@ export default {
         option:{
             type:Object,
             default:{}
-        }
+        }        
     },
     data() {
-        return {};
+        return {
+            Keyword:'',
+            map:null,
+            tc:null,           
+            searchService:null,
+            markerArrays:[],
+            searchMarkers:[]
+        };
     },
-    methods: {        
+    methods: {
+        searchKeyword(){
+                let markers = this.searchMarkers;
+                let markerArrays = this.markerArrays;
+                var keyword = this.Keyword;//搜索关键字
+                var region = '衡水';//城市
+                var pageIndex = 1;
+                var pageCapacity = 10;// 设置每页搜索结果数
+                this.clearOverlays(markers);
+                this.clearOverlays(markerArrays);
+                //根据输入的城市设置搜索范围
+                this.searchService.setLocation(region);
+                //设置搜索页码
+                this.searchService.setPageIndex(pageIndex);
+                //设置每页的结果数
+                this.searchService.setPageCapacity(pageCapacity);
+                //根据输入的关键字在搜索范围内检索
+                this.searchService.search(keyword);
+                //根据输入的关键字在圆形范围内检索
+                //var region = new qq.maps.LatLng(39.916527,116.397128);
+                //searchService.searchNearBy(keyword, region , 2000);
+                //根据输入的关键字在矩形范围内检索
+                //region = new qq.maps.LatLngBounds(new qq.maps.LatLng(39.936273,116.440043),new qq.maps.LatLng(39.896775,116.354212));
+                //searchService.searchInBounds(keyword, region);
+        },  
         getMap() {
+            //初始化地图信息 
             let that = this;
+            let tcMapScript = document.querySelector('#tcMapScript');
             return new Promise(function (resolve, reject) {
-                    window.init = function () {
-                        resolve(qq)
+                    window.initTcMap = function () {     
+                        window.tcMap = qq;                    
+                        resolve(qq);
+                    };
+                    if(!tcMapScript){
+                        var script = document.createElement("script");
+                        script.id="tcMapScript"
+                        script.type = "text/javascript";
+                        script.src = "http://map.qq.com/api/js?v=2.exp&callback=initTcMap&key="+that.key;
+                        script.onerror = reject;
+                        document.head.appendChild(script);
+                    }else{
+                        if(window.tcMap){
+                            resolve(window.tcMap);
+                        }
+
                     }
-                    var script = document.createElement("script");
-                    script.type = "text/javascript";
-                    script.src = "http://map.qq.com/api/js?v=2.exp&callback=init&key="+that.key;
-                    script.onerror = reject;
-                    document.head.appendChild(script);
+                    
             })
         },
-        init(tt){
-            //console.log(tt);
-            let propsOptions = this.option;
-            let center =new tt.maps.LatLng(propsOptions.center[0],propsOptions.center[1]);
-            let mapZoomType =  tt.maps.MapZoomType[propsOptions.MapZoomType];
-            let MapTypeId = tt.maps.MapTypeId[propsOptions.MapTypeId];
-            let options = Object.assign(propsOptions,{
-                center:center,
-              mapZoomType:mapZoomType,
-              MapTypeId:MapTypeId
+        marker(){            
+            let tc = this.tc;
+            //当前的地图
+            let map = this.map;
+            //地图显示的中心点(标记点)            
+            let markers = this.option.markers; 
+            //渲染坐标
+            for(let index=0,item;item=markers[index];index++){
+                //添加显示地图
+                item = Object.assign(item,{
+                    map:map
+                });                
+                //添加位置
+                if(item.position&&item.position[0]&&item.position[1]) {
+                    let position = new tc.maps.LatLng(item.position[0],item.position[1]);
+                    item = Object.assign(item,{                    
+                        position:position
+                    });
+                }
+                //添加动画                
+                if(item.animation){
+                    let animation = tc.maps.MarkerAnimation[item.animation];
+                    item = Object.assign(item,{                    
+                        animation:animation
+                    });
+                }
+                //添加地图icon
+                if(item.icon){
+                    let icon = new tc.maps.MarkerImage(item.icon);
+                    item = Object.assign(item,{                    
+                        icon:icon
+                    });
+                }
+                //添加地图阴影
+                if(item.shadow){
+                    let shadow = new tc.maps.MarkerImage(item.shadow);
+                    item = Object.assign(item,{                    
+                        shadow:shadow
+                    });
+                }                
+                let marker = new tc.maps.Marker(item);
+                this.markerArrays.push(marker)
+            }            
+        },
+        clearOverlays(overlays) {
+            let overlay;
+            while (overlay = overlays.pop()) {
+                overlay.setMap(null);
+            }
+        },
+        newSearchService(){
+            let that = this;
+            let tc = this.tc;
+            let map = this.map;
+            let searchMarkers = this.searchMarkers;
+             //设置Poi检索服务,用于本地检索、周边检索
+            this.searchService = new tc.maps.SearchService({
+                //检索成功的回调函数
+                complete: function(results) {
+                    //设置回调函数参数
+                    var pois = results.detail.pois;
+                    if(pois){
+                        var infoWin = new tc.maps.InfoWindow({
+                            map: map
+                        });
+                        var latlngBounds = new tc.maps.LatLngBounds();
+                        for (var i = 0, l = pois.length; i < l; i++) {
+                            var poi = pois[i];
+                            //扩展边界范围,用来包含搜索到的Poi点
+                            latlngBounds.extend(poi.latLng);        
+                            (function(n) {
+                                var marker = new tc.maps.Marker({
+                                    map: map
+                                });
+                                marker.setPosition(pois[n].latLng);        
+                                marker.setTitle(i + 1);
+                                searchMarkers.push(marker);
+                                tc.maps.event.addListener(marker, 'click', function() {
+                                    infoWin.open();
+                                    infoWin.setContent('<div style="width:280px;height:100px;">' + 'POI的ID为:' +
+                                        pois[n].id + ',POI的名称为:' + pois[n].name + ',POI的地址为:' + pois[n].address + ',POI的类型为:' + pois[n].type + '</div>');
+                                    infoWin.setPosition(pois[n].latLng);
+                                });
+                            })(i);
+                        }
+                        that.searchMarkers = searchMarkers;
+                        //调整地图视野
+                        map.fitBounds(latlngBounds);
+                    }
+                    
+                },
+                //若服务请求失败,则运行以下函数
+                error: function(val) {
+                    console.log("出错了。",val);
+                }
             });
-            let elm = document.querySelector('#nutuiMapTencent');
-            let map  = new tt.maps.Map(elm,options)
+        },
+        init(){
+            //console.log(tt);
+            let tt = this.tc;
+            let propsOptions = this.option.options; 
+            //
+            let center = null; 
+            let id = this.option.id;
+            //地图显示的中心点(标记点)
+            if(propsOptions.center[0]&&propsOptions.center[1]){
+                let center =new tt.maps.LatLng(propsOptions.center[0],propsOptions.center[1]);
+                propsOptions = Object.assign(propsOptions,{                    
+                     center:center
+                });
+            }
+            //缩放类型,可设置以地图中心点或双指中心点为焦点(移动端)。 //不填默认缩放手指中心位置
+            if(propsOptions.MapZoomType){
+                let mapZoomType =  tt.maps.MapZoomType[propsOptions.MapZoomType];
+                propsOptions = Object.assign(propsOptions,{                    
+                     mapZoomType:mapZoomType
+                });
+            }       
+            //地图显示类型 默认显示2d平面地图
+            if(propsOptions.MapTypeId){
+                let MapTypeId = tt.maps.MapTypeId[propsOptions.MapTypeId];
+                propsOptions = Object.assign(propsOptions,{                    
+                MapTypeId:MapTypeId
+                });
+            }            
+            //地图初始化
+            let elm = document.querySelector('#'+id);
+            this.map  = new tt.maps.Map(elm,propsOptions);
+            //是否创建一个marker 检索
+            this.$nextTick(()=>{
+                this.marker();
+                this.newSearchService();
+            })     
+            //是否有检索功能
+
         }
     },
     mounted(){
         let that = this;
         this.getMap().then(
-            tt=>{
-                that.init(tt);
+            qq=>{
+                that.tc = qq;
+                that.init();
             }
         );
     }
 }
 </script>
 <style lang="scss">
+.nutui-mapbox{
+    position: relative;
+    .search{
+        position: absolute;
+        z-index: 2;
+        top: 26px;
+        left: 71px;
+    }
+    .nut-map{
+        position: absolute;
+        width: 100%;
+        height: 100%;
+        top:0;
+        left:0;
+        z-index: 1;
+    }
+}
 </style>