国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

微信小程序 location API接口詳解及實(shí)例代碼

2019-11-20 08:46:08
字體:
供稿:網(wǎng)友

 微信小程序 location API 接口:

現(xiàn)在微信小程序火了 ,利用假期時(shí)間學(xué)習(xí)了下,微信小程序的基礎(chǔ)知識,嘿嘿!

以下是記錄學(xué)習(xí)微信小程序 location API接口,并且寫了一個(gè)小實(shí)例來記錄,如有錯(cuò)誤之處還請指正。

微信小程序的位置接口共有兩個(gè):

1、wx.getLocation(OBJECT)獲取當(dāng)前的地理位置、速度。
2、wx.openLocation(OBJECT) 使用微信內(nèi)置地圖查看位置

然后,根據(jù)object參數(shù)說明,結(jié)合module模塊化重寫了下兩個(gè)接口在暴露出來引用,讓項(xiàng)目更加靈活管理。具體代碼如下:

location.js::

/**  * 獲取當(dāng)前的地理位置、速度。  * 1、fType:     默認(rèn)為 wgs84 返回 gps 坐標(biāo),gcj02 返回可用于wx.openLocation的坐標(biāo)   選填  * 2、cbSuccessFun: 接口調(diào)用成功的回調(diào)函數(shù),返回內(nèi)容詳見返回參數(shù)說明。 必填  * 3、cbFailFun:  接口調(diào)用失敗的回調(diào)函數(shù) 選填  * 4、cbCompleteFun:接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行) 選填  */ function getLocationFun(fType, cbSuccessFun, cbFailFun, cbCompleteFun){   var getObj={};   getObj.type="wgs84";   if(fType){     getObj.type=fType;   }   getObj.success=function(res){     var _res=res;     if(cbSuccessFun){       cbSuccessFun(_res);     }   }   getObj.fail=function(res){     if(cbFailFun){       cbFailFun();     }else{       console.log("getLocation fail:"+res.errMsg);     }   }   getObj.complete=function(res){     if(cbCompleteFun){       cbCompleteFun();     }   }   wx.getLocation(getObj); }  /**  * 使用微信內(nèi)置地圖查看位置  * 1、latitude:   緯度,范圍為-90~90,負(fù)數(shù)表示南緯 必填  * 2、longitude:  經(jīng)度,范圍為-180~180,負(fù)數(shù)表示西經(jīng) 必填  * 3、scale:    縮放比例,范圍1~28,默認(rèn)為28 選填  * 4、name:     位置名 選填  * 5、address:   地址的詳細(xì)說明 選填  * 6、cbSuccessFun: 接口調(diào)用成功的回調(diào)函數(shù) 選填  * 7、cbFailFun:  接口調(diào)用失敗的回調(diào)函數(shù) 選填  * 8、cbCompleteFun:接口調(diào)用結(jié)束的回調(diào)函數(shù)(調(diào)用成功、失敗都會(huì)執(zhí)行) 選填  */ function openLocationFun(latitude, longitude, scale, name, address, cbSuccessFun, cbFailFun, cbCompleteFun){   var openObj={};   openObj.latitude=latitude;   openObj.longitude=longitude;   openObj.scale=15;   if(scale>0 && scale<29){     openObj.scale=scale;   }   if(name){     openObj.name=name;   }   if(address){     openObj.address=address;   }   openObj.success=function(res){     if(cbSuccessFun){       cbSuccessFun();     }   }   openObj.fail=function(res){     if(cbFailFun){       cbFailFun();     }else{       console.log("openLocation fail:"+res.errMsg);     }   }   openObj.complete=function(res){     if(cbCompleteFun){       cbCompleteFun();     }   }   wx.openLocation(openObj); }  module.exports={   getLocationFun: getLocationFun,   openLocationFun: openLocationFun } 

demo.js::

var comm = require( "../../common/common.js" ); var location=require('../../common/location.js'); Page( {  data: {   uploadImgUrls: [],   title: ""  },  getlocation: function( e ) {   location.getLocationFun(    'gcj02',     function(cb){     console.log(cb);     var _latitude=cb.latitude;     var _longitude=cb.longitude;     location.openLocationFun(      _latitude,      _longitude,      null,      "廈門觀音山",      "廈門觀音山匹克大廈",      null,      null,      null     )    }   )  },  onLoad: function( options ) {   var _title = "ddd";   if( options.title ) {    _title = options.title;   }   this.setData( {    title: _title   })   console.log("load")   console.log( comm.formatDateFun( new Date(), 1 ) );  },  onShow:function(e){   console.log("show");  },  onHide: function(e){   console.log("hide");  },  onUnload:function(e){   console.log("unload");  }  // onReady: function(){  //  wx.setNavigationBarTitle({  //   title: this.data.title  //  });  // } }) 

經(jīng)調(diào)試發(fā)現(xiàn)getLocation接口的type不管是傳遞wgs84還是gcj02返回的參數(shù)都是只有經(jīng)緯度,并沒有文檔上提到的速度和位置的精確度兩個(gè)參數(shù)

然后我在點(diǎn)擊“去這里”頁面跳轉(zhuǎn)后,發(fā)現(xiàn)每次都是提示定位失敗,不曉得是不是因?yàn)閣eb開發(fā)工具的原因。而且好像經(jīng)緯度有差距,和本人實(shí)際距離不一致。還有定義了name和address兩個(gè)參數(shù)并沒有發(fā)現(xiàn)有啥變化,最后比較嚴(yán)重的問題是我點(diǎn)擊返回后提示page route錯(cuò)誤,再次點(diǎn)擊按鈕,提示錯(cuò)誤了,不能點(diǎn)擊。不知道什么原因?要怎么解決!


目前針對這個(gè)接口學(xué)習(xí)到這里,后續(xù)有其他發(fā)現(xiàn)或者解決辦法在來更新。

==============================================================================================

今天,微信發(fā)布新版本了【最新版本 0.10.101100】,對于位置接口也有進(jìn)一步的更新,

1、打開地圖接口在返回不會(huì)提示page route錯(cuò)誤了

2、wx.openLocation接口傳遞自定義的name和address參數(shù)后,可以在地圖描述框,顯示出來了,不過經(jīng)緯度依然不夠準(zhǔn)確。點(diǎn)擊“去這里”依然是定位失敗。 


感謝閱讀,希望能幫助到大家,謝謝對本站的支持!

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 黄大仙区| 荔波县| 象山县| 建宁县| 视频| 偏关县| 正宁县| 北海市| 沂源县| 红桥区| 文水县| 偃师市| 潜山县| 延津县| 榆林市| 老河口市| 镇原县| 古丈县| 金山区| 镇康县| 宝应县| 吴忠市| 平阳县| 布尔津县| 普格县| 肃南| 乐昌市| 宜城市| 临猗县| 西吉县| 江北区| 广河县| 康马县| 泗水县| 兰坪| 南城县| 太白县| 静乐县| 普兰店市| 县级市| 石渠县|