Hbuider hybrid app开发之地图操作方法

var map = null;
var zoom = 13;//缩放值
function creatMap(data,ws){//创建地图---data:一些地图设置参数 如图标、中心点经纬度
    if (!ws) {return};
    wo = ws.opener();
    map = new plus.maps.Map("map");
    if(!data){
        userLocation();//定位当前用户位置
        console.log("userLocation");
    }else{
        pcenter = new plus.maps.Point(data.pcenter_long,data.pcenter_lin);
        console.log("pcenter---"+JSON.stringify(pcenter));
        var marker = new plus.maps.Marker(pcenter);
        marker.setIcon(data.Icon);
        marker.setLabel(data.Lable);
        var bubble = new plus.maps.Bubble(data.Bubble);
        marker.setBubble(bubble);
        map.addOverlay(marker);
        map.centerAndZoom(pcenter,data.zoom || zoom);
    }
}
function showPoints(points) {//显示位置点
    if(!map){return}
    for(var i=0;i<points.length;i++){
        var point = new plus.maps.Point(points[i].lon,points[i].lin);
        var marker = new plus.maps.Marker(point);
        console.log(points[i].lon);
        marker.setIcon(points[i].Icon);
        marker.setLabel(points[i].Lable);
        var bubble = new plus.maps.Bubble(points[i].Bubble);
        marker.setBubble(bubble);
        map.addOverlay(marker);
    }
}
function showLine(pointsJson){//创建一条折线
    if(!map){return}
    var polylineObj = null,points = new Array();
    for(var i=0;i<pointsJson.length;i++){
        points[i] = new plus.maps.Point(pointsJson[i].lon,pointsJson[i].lin);
    }
    polylineObj = new plus.maps.Polyline(points);
    polylineObj.setStrokeOpacity( 0.6 ); // 设置折线为半透明
    polylineObj.setLineWidth(5);// 设置折线宽度
    console.log(JSON.stringify(polylineObj));
    map.addOverlay(polylineObj);
}
function showGon(pointsJson){//创建一个多边形对象
    var polygonObj = null,points = new Array();
    for(var i=0;i<pointsJson.length;i++){
        points[i] = new plus.maps.Point(pointsJson[i].lon,pointsJson[i].lat);
    }
    polygonObj = new plus.maps.Polygon(points);
    map.addOverlay(polygonObj);
}
function navigation(point){//导航  调用第三方
    var point = {"des":"导航描述","dstlon":116.39131928,"dstlin":39.90793074,"srclon":116.335,"srclin":39.966};
    var dst = new plus.maps.Point(point.dstlon,point.dstlin);
    var src = new plus.maps.Point(point.srclon,point.srclin);
    plus.maps.openSysMap(dst,point.des,src);// 调用系统地图显示 
}
function drivingSearch(points){//驾车路线检索
    var searchObj = new plus.maps.Search(map);
    var startP = new plus.maps.Point(points.startLon,points.startLat);
    var endP = new plus.maps.Point(points.endLon,points.endLat);
    var startC = points.startCity,endC = points.endCity;
    searchObj.onRouteSearchComplete = function (state,result) {
        console.log("onRouteSearchComplete: "+state+" , "+result.routeNumber);
        if ( state == 0 ) {
            if ( result.routeNumber <= 0 ) {alert( "没有检索到结果" );}
            for(var i=0; i<result.routeNumber; i++){ 
                if(i==1){
                    map.addOverlay(result.getRoute(i));
                    console.log(result.getRoute(i).distance);
                    return;
                }}
        } else {alert( "检索失败" );}
    }
    searchObj.setDrivingPolicy(plus.maps.SearchPolicy.DRIVING_DIS_FIRST);//驾车策略  距离最短
    searchObj.drivingSearch(startP,startC,endP,endC);
}
function userLocation() {//当前用户定位
    if(null == map){return};
    map.showUserLocation(true);
    map.getUserLocation(function(state, pos) {
        if (0 == state) {
            map.setZoom(zoom);
            console.log(JSON.stringify(pos));
            map.setCenter(pos);
        } 
    });
}
function resetMap(map) {//重置地图
    //map.centerAndZoom(pcenter,12);
    map.reset();
}
/**
 * @description 城市中关键词搜索 并显示在地图上
 * */ 
function searchAndShowOnMap(city,key){
    var address = null;
    if(!map){return}
    var searchObj = new plus.maps.Search(map);
    console.log("search.."+searchObj);
    searchObj.poiSearchInCity(city,key);
    searchObj.onPoiSearchComplete = function(state,result){
        console.log("onPoiSearchComplete: "+state+" , "+result.currentNumber);
        if ( state == 0 ) {
            if ( result.currentNumber <= 0 ) {
                mui.alert("没有检索到结果");
            }
            for(var i=0; i<result.currentNumber; i++){// 将检索到的第一条信息作为标点添加到地图中
                var pos = result.getPosition(0);
                console.log(JSON.stringify(pos.address));
                address = pos.address;
                var marker = new plus.maps.Marker( pos.point );
                map.setCenter(pos.point);
                marker.setLabel(pos.name);
                map.addOverlay(marker);
                break;
            }
        }else{
            mui.alert( "检索失败" ); 
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值