3***@qq.com
3***@qq.com
  • 发布:2019-04-25 15:30
  • 更新:2020-07-07 16:50
  • 阅读:13124

uni.createMapContext(mapId,this) 获取原生map后 使用原生api操作无效问题

分类:uni-app
<template>  
    <view class="all-view uni-bg-gray">  
        <map id="navmap" :style="'width: 100%;height:' + allViewHeight +'px;'" :latitude="src_latitude" :longitude="src_longitude" :markers="markers"  
         :polyline="poly" :hidden="false">  
        </map>  
    </view>  
</template>  

<script>  
    import {  
        mapState,  
        mapMutations  
    } from 'vuex';  
    import amap from '../../amap-wx.js';  
    export default {  
        data() {  
            return {  
                src_latitude:0,  
                src_longitude:0,  
                des_latitude:0,  
                des_longitude:0,  
                //地图停车场标志  
                markers: [{  
                    id: 0,  
                    iconPath: '../../static/img/icon_address_blue.png',  
                    width: 22,  
                    height: 32  
                },{  
                    id: 0,  
                    iconPath: '../../static/img/carpark.png',  
                    width: 22,  
                    height: 32  
                }],  
                poly:[],  
                allViewHeight: 800,  
                searchObj:null,  
                nav_map:null,  
                info:'',  
                parkname:'',  
                bodytext:''  
            };  
        },  
        computed: {  
            ...mapState(['amapKey'])  
        },  
        onReady(options) {  
            var nmap = uni.createMapContext("navmap",this).$getAppMap();  
            this.nav_map = nmap;  
            let self = this;  
            nmap.showUserLocation( true );  
            console.log("map:::::"+JSON.stringify(nmap));  
            var searchObj = new plus.maps.Search(nmap);  
            searchObj.onPoiSearchComplete = function( state, result ){  
                console.log("onPoiSearchComplete: "+state+" , "+result.currentNumber);  
                if ( state == 0 ) {  
                    if ( result.currentNumber <= 0 ) {  
                        alert( "没有检索到结果" );  
                    }  
                    for(var i=0; i<result.currentNumber; i++){  
                        var pos = result.getPosition( i );  
                        var marker = new plus.maps.Marker( pos.point );  
                        marker.setLabel( pos.name );  
                        map.addOverlay( marker );  
                    }  
                } else {  
                    alert( "检索失败" );  
                }  
            }   
            var pt = new plus.maps.Point( self.src_latitude, self.src_longitude );   
            searchObj.poiSearchNearBy( "停车场", pt, 1000 );  
        },  
        onLoad(options) {  
            setTimeout(function() {  
                let allView = self.getSystemInfo();  
                allView.then(function(res) {  
                    console.log(res.windowHeight);  
                    self.allViewHeight = res.windowHeight;  
                });  
            }, 500);  
            //console.log(JSON.stringify(options));  
            //console.log(JSON.stringify(this.markers));  
            this.src_longitude = options.origin.split(",")[0];  
            this.src_latitude = options.origin.split(",")[1];  
            this.markers[0].latitude = this.src_latitude;  
            this.markers[0].longitude = this.src_longitude;  
            this.info = options.info;  
            this.bodytext = options.bodytext;  
            this.parkname = options.parkname;  
            this.des_longitude = options.destination.split(",")[0];  
            this.des_latitude = options.destination.split(",")[1];  
            this.markers[1].latitude = this.des_latitude;  
            this.markers[1].longitude = this.des_longitude;  
            let self = this;  

            this.amapPlugin = new amap.AMapWX({  
                key: this.amapKey  
            });  

            this.amapPlugin.getDrivingRoute({  
              origin: options.origin,  
              destination: options.destination,  
              success: function(data){  
                var points = [];  
                if(data.paths && data.paths[0] && data.paths[0].steps){  
                  var steps = data.paths[0].steps;  
                  for(var i = 0; i < steps.length; i++){  
                    var poLen = steps[i].polyline.split(';');  
                    for(var j = 0;j < poLen.length; j++){  
                      points.push({  
                        longitude: parseFloat(poLen[j].split(',')[0]),  
                        latitude: parseFloat(poLen[j].split(',')[1])  
                      })  
                    }   
                  }  
                }  
                 self.poly.push({  
                     points:points,  
                     color: "#0091ff",  
                     width: 6  
                 })  
                 console.log("路线信息:"+JSON.stringify(data.paths[0]))  
                if(data.paths[0] && data.paths[0].distance){  
                  console.log("共计长度:"+data.paths[0].distance);  
                }  
                if(data.taxi_cost){  
                    console.log( '打车约' + parseInt(data.taxi_cost) + '元')  
                }  

              },  
              fail: function(info){  

              }  
            })  
        },  
        methods: {  
            getSystemInfo() {  
                return new Promise((res, rej) => {  
                    uni.getSystemInfo({  
                        success(data) {  
                            res(data);  
                        }  
                    })  
                })  
            }  
        }  
    }  
</script>  

<style>  

</style>  

刚接触uniapp,烦请各位指导一下 谢谢!!!
上面得vue文件内容
问题如下:

2019-04-25 15:30 负责人:无 分享
已邀请:
3***@qq.com

3***@qq.com (作者) - sheng

没有大佬路过指导一下么

  • 7***@qq.com

    大佬,我也是卡在这里了

    2019-04-26 17:17

  • 3***@qq.com (作者)

    回复 7***@qq.com: 你编译模式是自定义组件模式吗 我改成直接使用原生Map组件了 ,我看HbuilderX最新版好像说有更新这一问题 也没试

    2019-04-29 10:04

  • x***@qq.com

    uni.createMapContext("navmap").$getAppMap 这个问题解决了么?怎么解决的?谢谢

    2019-05-10 15:16

  • 虫子的辛苦

    回复 x***@qq.com: 在配置文件manifest中选择源码视图,然后在这里增加参数 "usingComponents" : true就好了,我的已经解决


    "app-plus" : {


    "usingComponents" : true,

    2019-06-15 11:55

  • snail_lt

    回复 虫子的辛苦: 大佬,我这样写了在h5还是报错。。在app模拟器可以调用

    2019-09-03 11:42

7***@qq.com

7***@qq.com

我的是这样

随便一个名字

随便一个名字

我也是这样了啊,有没有大佬帮帮忙

虫子的辛苦

虫子的辛苦 - 程序员

在manifest配置文件选择源码视图,增加一个参数配置就好了

  • snail_lt

    大佬,在h5还是不管用

    2019-09-03 11:47

ecoo

ecoo

提示 ’uni.createMapContext(...).$getAppMap is not a function‘
这个不知是咋回事

r***@outlook.com
3***@qq.com

3***@qq.com

$getAppMap不是方法,所以不用写(),这样写试下:
appMap=uni.createMapContext("map1",this).$getAppMap;

  • 3***@qq.com

    这样写:

    var appMap=uni.createMapContext("map1",this);

    console.log(appMap.pageVm.markers[0].iconPath)

    2020-02-17 20:20

l***@126.com

l***@126.com

this.mainmap=uni.createMapContext('begin_main_map',this);  
              this.mainmap.getCenterLocation({    
                                   success(res) {    
                                       console.log(JSON.stringify(res))    
                                   }    
                }) 

该问题目前已经被锁定, 无法添加新回复