ionic1 添加百度地图插件 cordova-plugin-baidumaplocation

cordova-plugin-baidumaplocation 这个插件返回的数据是 json 格式的  可以直接获取  android 和 ios 都可用

1.先去百度地图平台去创建应用  获取访问应用AK  android 和 ios 是分开的 不同的

   http://lbsyun.baidu.com/

2.创建的时候需要发布版SHA1和开发板SHA1

  参考链接:https://blog.csdn.net/lhg_55/article/details/52139277  (亲测有效)

3.添加插件 

  " " 里面直接写值  不需要 < > 括号

cordova plugin add cordova-plugin-baidumaplocation --variable ANDROID_KEY="<API_KEY_ANDROID>" --variable IOS_KEY="<API_KEY_IOS>"

4.添加相应代码

  js页面

// 进行定位
    baidumap_location.getCurrentPosition(function(result) {
        var longitude = result.longitude; //经度
        var latitude = result.latitude; //纬度
       myaddr(longitude, latitude);
     }, function(error) {

     });
//调用函数 myaddr(longitude, latitude);

//根据定位得到的经纬度对地址进行解析 function myaddr(longitude, latitude) { //alert("我的地址是:" + longitude + "," + latitude); var map = new BMap.Map('location'); //与html的id名对应 var point = new BMap.Point(longitude, latitude); map.centerAndZoom(point, 12); var marker = new BMap.Marker(point); // 创建标注 map.addOverlay(marker); // 将标注添加到地图中 //把地址在地图上标出来 var geoc = new BMap.Geocoder(); geoc.getLocation(point, function(rs) { var addrmsg = rs.address; var addComp = rs.addressComponents; //详细的分省市县街道的信息 alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber); var opts = { width: 600, // 信息窗口宽度 height: 50, // 信息窗口高度 } var infoWindow = new BMap.InfoWindow("地址:" + addrmsg, opts); //创建信息窗口对象 map.openInfoWindow(infoWindow, point); //开启信息窗口 }); //移除监听事件 function showInfo(e) { alert(e.point.lng + ", " + e.point.lat); map.removeEventListener("click", showInfo); }
//对地图点击时间进行监听 map.addEventListener("click", showInfo); }

  html页面

<div id="location" style="width:100%;height:100%;"></div>

5.我做的过程中参考的其中一个链接:https://blog.csdn.net/zuoyiran520081/article/details/72771654?locationNum=12&fps=1

   但是他说的引入js文件 我并没有  也可以成功

6.过程中可能遇到的问题

   http://bbs.lbsyun.baidu.com/forum.php?mod=viewthread&tid=8955

   https://blog.csdn.net/liyanlei5858/article/details/41284589

猜你喜欢

转载自www.cnblogs.com/luffyc/p/8991705.html