同志們好,我又迴歸了,原本是想分享Flutter相關的內容,可是很久不寫文章,感受生疏了很多,不知道從何處下筆了,全部就把草稿箱躺了快一年的內容整理一下,分享分享。這篇文章是使用最簡單的方式實現地圖導航的需求,那就是經過喚起手機中的導航類軟件進行導航,因此你想在應用內實現導航以及定製導航路線,那就能夠到此爲止了(若是有應用內導航需求或者線路規劃需求的,留言,我能夠寫個底層的插件(android端,此時須要有個寫ios底層合做的了)。 不少人都認爲喚起導航軟件進行導航,須要底層去寫實現,其實大可沒必要,經過ReactNative自帶的一些Api就能夠實現咱們想要的效果了。php
Linking是RN中APP之間跳轉交互的Api接口,而且能解析其中攜帶的參數數據。html
componentDidMount() {
Linking.getInitialURL().then((url) => {
if (url) {
//能夠根據url寫跳轉到相應的頁面的邏輯和參數
console.log('Initial url is: ' + url);
}
}).catch(err => console.error('An error occurred', err));
}
複製代碼
Linking.openURL(url).catch(err => console.error('An error occurred', err));
//如打開瀏覽器訪問
Linking.openURL("https://github.com/xiehui999")
//撥打電話
Linking.openURL("tel:1008611")
//發送短信
Linking.openURL("smsto:10086")
複製代碼
須要注意的是,在ios9.0及以上須要配置LSApplicationQueriesSchemes,不然調用無效。react
baidumap://map/direction?origin=latlng:34.264642646862,108.95108518068|name:我家&destination=大雁塔&mode=driving®ion=西安&output=html&src=webapp.baidu.openAPIdemo
//調起百度PC或Web地圖,展現"西安市"從(lat:34.264642646862,lng:108.95108518068 )"我家"到"大雁塔"的駕車路線。
複製代碼
origin是必填項,後更導航起始點經緯度,name是導航地圖顯示的marker提示信息。destination是導航終點的經緯度或者名稱。 mode指定導航路線的方法包括可選transit(公交)、driving(駕車)、walking(步行)和riding(騎行)(ios多個navigation)。根據本身的需求使用 其它具體參數可查看百度官方文檔android
喚起高德地圖規劃線路的url前綴略有不一樣,基本內容仍是同樣的ios
//Android
amapuri://route/plan/?
//iOS
iosamap://path?
//後面追加內容
sourceApplication=test&slat=39.92848272&slon=116.39560823&sname=A&dlat=39.98848272&dlon=116.47560823&dname=B&dev=0&t=0
複製代碼
s開通的表示起點信息,不填默認當前位置,d開頭的爲終點信息,t爲導航規劃路線的方式 t = 0(駕車)= 1(公交)= 2(步行)= 3(騎行)= 4(火車)= 5(長途客車)git
qqmap://map/routeplan?type=drive&from=清華&fromcoord=39.994745,116.247282&to=怡和世家&tocoord=39.867192,116.493187&referer=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77
複製代碼
type路徑規劃方式,路線規劃方式參數:分別爲公交:bus ,駕車:drive ,步行:walk ,騎行:bike,from 開頭的爲起點信息,to開頭的爲終點信息,須要注意的是referer參數,官方文檔說該參數爲申請的騰訊地圖的開發key.github
經過上面的分析,咱們就能夠實現導航了,下面咱們就簡單的封裝一下一個導航的工具類,包括三個判斷是否安裝相應地圖軟件的方法,和相應打開地圖導航的方法web
static isInstallAmap = () => {
return new Promise((resolve, reject) => {
Linking.canOpenURL(Platform.OS === "android" ? "amapuri://route/plan/" : "iosamap://path").then(supported => {
resolve(supported)
}).catch(resolve(false))
})
}
/**
* 打開高德地圖導航
* @param {String} data.sname - 起點名字.
* @param {String|number} data.slon - 起點經度.
* @param {String|number} data.slat - 起點緯度.
* @param {String} data.dname - 終點名字.
* @param {String|number} data.dlon - 終點經度.
* @param {String|number} data.dlat - 終點緯度.
* @param{Mode} data.mode 導航類型
* @param data
*/
static openAmap = (data = {}) => {
let base = Platform.OS === "android" ? "amapuri://route/plan/?" : "iosamap://path?"
return new Promise((resolve, reject) => {
//起點經緯度不傳,則自動將用戶當前位置設爲起點
if (!data.dlat || !data.dlon) {
resolve("須要終點經緯度")
} else {
if (data.slon && data.slat) {
base += `&slat=${data.slat}&slon=${data.slon}`
}
if (data.sname) {
base += `&sname=${data.sname}`
}
if (data.dname) {
base += `&dname=${data.dname}`
}
base += `&dlat=${data.dlat}&dlon=${data.dlon}&dev=0&t=${data.mode ? (data.mode.amap || 0) : 0}`
Linking.openURL(base).then(res => {
}).catch(err => {
reject("暫無安裝高德地圖")
})
}
});
}
複製代碼
上面我貼出了一個判斷安裝和一個喚起軟件導航的方法,其它幾種按此實現便可,固然我也把整理好的文件上傳到GitHub了,具體細節能夠前往RoutePlan文件,使用demo可前往RoutePlanExample文件,能夠直接下載demo。react-native