时光机
时光机
  • 发布:2016-03-11 09:58
  • 更新:2017-02-27 13:07
  • 阅读:2551

ios上百度地图无法标记marker,安卓上可以标记,求好心人帮忙看看啊

分类:MUI
/*!  
 * =====================================================  
 * 基于Mui v2.4.0  
 * =====================================================  
 */  
/**  
 * 地图插件  
 */  

(function($, window, utry) {  

    /**  
     *   
     * {  
     *  id  显示地图的dom Id  
     *  zoom 地图缩放比 默认16  
     *  centerPoint 地图中心点  
     * }  
     */  
    var UtryMap = function( options ) {  
        this.options = $.extend( true,{  
            zoom:16  
        }, options);  

        var self = this;  

        this.pointList = new Array();  
        this.centerPoint = this.options.centerPoint;  
        this.zoom = this.options.zoom;  
        this.mark = "";  
        this.bubble = "";  
        this.defaultCenter = this.options.defaultCenter;  
        var map = new plus.maps.Map( this.options.id );  
        //设置中心点  
        if( this.centerPoint ){  
            map.setCenter( this.centerPoint );  
            reverseGeocode( point,function(address,point){  
                sysLog.printLog( "地图中心点:"+address+",",point );  
            });  
            this.centerPoint = map.getCenter();  
        }else{  
            map.hide()  
            /**  
             * 不设置延迟  导致加载标识 或者 地图不正确  
             */  
            setTimeout( function(){ setCenterByCurrentLocalHost( map,self ) } , 85);   
        }  
        //设置缩放比  
        if( this.zoom ){  
            map.setZoom( this.zoom );  
        }  
        this.map = map  
    };  

    /**  
     * 设置地图位置通过当前用户位置  
     * @param {Object} map 地图对象  
     * @param {Object} thisObj UtryMap对象  
     */  
    function setCenterByCurrentLocalHost( map,thisObj ){  
        var obtainSuccess = map.getUserLocation( function ( state, point ){  
            if( 0 == state ){  
                thisObj.centerPoint = map.getCenter();  
                map.setCenter( point )  
                map.show()  
                reverseGeocode( point,function(address,point){  
                    sysLog.printLog( "地图中心点:"+address+",",point );  
                });  
            }  
            });  
        if( !obtainSuccess ){  
            thisObj.centerPoint = map.getCenter();  
            map.setCenter( thisObj.defaultCenter );  
        }  
    }  

    /**  
     * 经纬度转换为地址  
     * @param {Object} point  
     */  
    function reverseGeocode( _point,seccessCB,errorCB ){  
        plus.maps.Map.reverseGeocode( _point,{},function(event){  
            var address = event.address;  // 转换后的地理位置  
            var point = event.coord;  // 转换后的坐标信息  
            var coordType = event.coordType;    // 转换后的坐标系类型  
            if( seccessCB ){  
                seccessCB( address,point,coordType );  
            };  
        },function(e){  
            if( errorCB ){  
                errorCB( );  
            };  
        });  
    }  

    /**  
     *   
     * @param {Object} point point 表示经纬度的点  
     * @param {Object} M_B_Obj 包含marker对象和bubble对象的数据  
     * @param {Object} actionFn 事件函数  
     */  
    function createMarker( point,M_B_Obj,actionFn ){  
        var marker = new plus.maps.Marker(point);  
        marker.setIcon( M_B_Obj.marker.icon );  
        marker.setLabel( M_B_Obj.marker.label );  
        if( actionFn ){  
            marker.onclick = function( m ){  
                actionFn.onMarkerClick( m,M_B_Obj.marker );  
            };  
        }  

        var bubble = new plus.maps.Bubble( M_B_Obj.bubble.label );  
        if( actionFn ){  
            bubble.onclick = function( b ){  
                actionFn.onBubbleClick( b,M_B_Obj.bubble );  
            };  
        }  
        marker.setBubble(bubble);  

        return marker;  
    }  

    /**  
     *   
     * @param {Object} pointList  
        [{  
            lng:"经度",  
            lat:"纬度",  
            marker:{  
                icon:"/logo.png",  
                label:"hbuid"  
            },  
            bubble:{  
                icon:"/logo.png",  
                label:"打造最好的HTML5移动开发工具"  
            }  
        }]  
     */  

    /**  
     * 产生随机长度的数据  
     * @param {Object} min  
     * @param {Object} max  
     */  
    function createRandom(min, max){  
        var r = Math.random() * (max - min);  
        r = Math.floor( r );  
        var re = r<10 ? "0"+r:r;  
        return re;  
     }  

    /**  
     * 判断经度是否重复  
     * @param {Object} pointList 标点集合  
     * @param {Object} lng 经度  
     */  
    function isRepeatLng( pointList, lng ){  

        for( var i=0;i<pointList.length;i++ ){  
            var t = pointList[i].getLng();  
            t = sub2Bit( t );  
            if( t == lng ){  
                return true;  
            }  
        }  

        return false;  
    }  

    /**  
     * 截取目标两位  
     * @param {Object} target  
     */  
    function sub2Bit( target ){  

        return  Math.floor(target*100)/100;  
    }  

    /**  
     *   
     * @param {Object} pointList  标识集合  
     * @param {Object} actionFn  操作函数  
     * @param {Object} _basePoint  基准点  
     */  
    UtryMap.prototype.addMarker = function( pointList,actionFn,_basePoint) {  

        var self = this;  
        var len;  
        if( pointList ){  
            len = pointList.length;  
        }  
        var basePoint = "";  
        if( _basePoint ){  
            basePoint = _basePoint;  
        }  
        //选择marker基准点  
        if( basePoint ){  
            //经度截取到小数点两位  
            basePoint.lng = sub2Bit( basePoint.getLng());  
            //纬度截取到小数点两位  
            basePoint.lat = sub2Bit( basePoint.getLat() );   

            sysDebugLog.printLog("基础经纬度坐标"+"{"+ basePoint.lng+","+ basePoint.lat +"}");  
            setMark ();  
        }else{  
            //经度截取到小数点两位  
            self.map.getUserLocation( function ( state, point ){  
                if( 0 == state ){  
                    self.centerPoint = point;  
                    basePoint = {};  
                    basePoint.lng = sub2Bit( self.centerPoint.getLng() );  
                    //纬度截取到小数点两位  
                    basePoint.lat = sub2Bit( self.centerPoint.getLat() );   
                    setMark ();  
                }  
            });  
        }  

        function setMark (){  
            var point ;  
            for( var i=0;i<len;i++ ){  
                //经纬度为空  
                if( !pointList[i].lng || !pointList[i].lat ){  
                    //创建经纬度  
                    var cr = createRandom(1, 100);  
                    while( isRepeatLng( self.pointList, basePoint.lng+ "" + cr ) ){  
                        cr = createRandom(1, 100);  
                    }  
                    var _lng = basePoint.lng+ "" + cr +""+createRandom(1, 100);  
                    var _lat = basePoint.lat +"" + createRandom(1, 100) +""+createRandom(1, 100);  
                    point = new plus.maps.Point( _lng,_lat );  
                }else{  
                    point = new plus.maps.Point( pointList[i].lng,pointList[i].lat );  
                    sysLog.printLog("经纬度坐标"+"{"+ pointList[i].lng+","+ pointList[i].lat +"}");  
                }  
                self.pointList.push( point )  
                var overlay = createMarker( point,pointList[i],actionFn );  
                console.log(overlay.getLabel());  
                self.map.addOverlay( overlay );  
            }  
        }  

    };  
    /**  
     *   
     * @param {Object} point  
     * {  
     *  address:"天际大厦",  
     *  lng:"",经度  
     *  lat:"",纬度  
     * }  
     */  
    UtryMap.prototype.setCenterPoint = function( point,errorCB ) {  

        var self = this;  
        var _point = "";  
        if( point ){  
            if( point.lng && point.lat ){  
                _point = new plus.maps.Point( point.lng,point.lat);  
                self.map.setCenter( _point );  
            }else if( point.address ){  
                if( !point.city ){  
                    point.city = "";  
                }  
                plus.maps.Map.geocode( point.address ,{city:point.city },function(event){  
                    self.map.setCenter( event.coord );  
                },function(e){  
                    if( errorCB ){  
                        errorCB();  
                    };  
                });  
            }  
        }  

    };  

    /**  
     * 获取用户位置  
     */  
    UtryMap.prototype.getUserPoint = function( cb ) {  
        var self = this;  
        return self.map.getUserLocation( function ( state, point ){  
                if( 0 == state ){  
                    cb( point );  
                }  
            });;  
    };  

    window.utry = $.extend(true, window.utry, {  
        map: {  
            createMap: function(options) {  

                /**  
                 * * {  
                 *  id  显示地图的dom Id  
                 *  zoom 地图缩放比 默认16  
                 *  centerPointLng 地图的中心经度  
                 *  centerPointLat 地图的中心纬度  
                 *  centerPoint 地图中心点  
                 * }  
                 */  
                if( !options.id ){  
                    throw "地图容器不能为空";  
                }  
                //  经纬度  > centerPoint  
                if( options.centerPointLng && options.centerPointLat ){  
                    //创建centerPoint  
                    options.centerPoint = new plus.maps.Point( options.centerPointLng, options.centerPointLat );  
                }  
                /**  
                 * 由于定位有加载未完成问题,采用设置默认的中心,通过定时器延时加载解决  
                 */  
//              options.defaultCenter = new plus.maps.Point( 120.132065, 30.276827 );  //天际大厦  
//              if( !options.centerPoint ){  
//                  options.centerPoint = options.defaultCenter;  
//              }  
                return new UtryMap( options );  
            },  
            geocode:function( address, options, successCallback, errorCallback ){  
                plus.maps.Map.geocode( address, options, successCallback, errorCallback );  
            }  
        }  
    });  

})(mui, window, utry);
2016-03-11 09:58 负责人:无 分享
已邀请:
云钦

云钦 - 学习中

能写闭包还这么低调

DCloud_IOS_XTY

DCloud_IOS_XTY

请提供个可以运行的demo

604923581@qq.com

604923581@qq.com

请问你这个解决了吗? 我现在也遇到这个问题了

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