First step, autonavi as an example, go to Autonavi to register the corresponding key of Android and ios. The results are as follows
The second step is to set the parameters in the manifest.json of Hbuilderx,
The third step, code, Android and ios are not the same, need to judge the mobile phone platform
switch (uni.getSystemInfoSync().platform) {
case "android":
break;
case "ios":
break;
default:
break;
Copy the code
}
Uni.getsysteminfosync ().platform Is used to get the system platform of the device and returns a string (‘android’)/(‘ios’)
Step 4: Writing Android code
case “android”:
var packageName = "com.autonavi.minimap"; var main = plus.android.runtimeMainActivity(); var packageManager = main.getPackageManager(); var PackageManager = plus.android.importClass(packageManager); var packageInfo = packageManager.getPackageInfo( packageName, PackageManager.GET_ACTIVITIES ); if (packageInfo) { var Uri = plus.android.importClass("android.net.Uri"); var url = "androidamap://route? SourceApplication = AMap "+ "& SID =A&slat=" + thate.latitude + "&slon=" + thate.longitude + "&sname= current location" + "&did=B&dlat=" + slat + "&dlon=" + slon + "&dname=" + address + "&dev=0&t=2"; var Intent = plus.android.importClass("android.content.Intent"); var intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.addCategory(Intent.CATEGORY_DEFAULT); var uri = Uri.parse(url); // Pass the Scheme function as a URI to data Intent.setData (URI); intent.setPackage("com.autonavi.minimap"); var main = plus.android.runtimeMainActivity(); main.startActivity(intent); } else { uni.hideLoading(); Uni.showtoast ({title: "Please install Amap to complete navigation ", Duration: 2000, icon:" None "}); } break;Copy the code
Step 5: Writing ios code:
case “ios”:
var urlStr = "iosamap://path? sourceApplication=amap&backScheme=iosamap&sid=slat=" + that.latitude + "&slon=" + that.longitude + "& sname & did = = my position & dlat =" + "& dlon =" + + slat slon + "& dname =" + address + "& dev = 0 & t = 0". var UIApplication = plus.ios.import("UIApplication"); var NSURL = plus.ios.import("NSURL"); var encodeString = encodeURI(urlStr); var dbScheme = NSURL.URLWithString(encodeString); var application = UIApplication.sharedApplication(); var a = application.openURL(dbScheme); plus.ios.deleteObject(application); uni.hideLoading(); if (! a) { let appUrl = "itms-apps://itunes.apple.com/cn/app/id461703208?mt=8"; plus.runtime.openURL(appUrl, function(err) {}); Uni.showtoast ({title: "Please install and authorize Amap first ", Duration: 2000, icon:" None "}); } break;Copy the code
The openURL method in ios returns a Boolean that opens the connection based on whether the corresponding app is installed on the current device.
itms-apps://itunes.apple.com/cn/app/idxxxx?mt=8 jump ios app store, it is to download, XXXX for corresponding AppID, just with the ios, android is slightly different