ionic3 跳转第三方地图并根据关键字搜索

708 阅读1分钟

一、首先引入用到的3个插件

1.检查app是否存在:

$ ionic cordova plugin add cordova-plugin-appavailability
$ npm install --save @ionic-native/app-availability

import { AppAvailability } from '@ionic-native/app-availability';

2.判断iOS还是安卓:

import { Platform } from 'ionic-angular';

3.跳转第三方app:

$ ionic cordova plugin add https://github.com/lampaa/com.lampa.startapp.git

declare let startApp: any;

二、实现过程

1.先判断是iOS平台还是安卓平台:

this.platform.is('ios');

this.platform.is('android');

2.如果是iOS平台,检查是否安装了第三方app,如果安装就通过urlsheme打开:

this.appAvailability.check(checkUrl)
      .then(
        (yes: boolean) => {
          var sApp = startApp.set(urlSheme);
          sApp.start(function () {
            console.log("sApp.start succeed");
          }, function (error) {
            alert("error---" + error);
          });
        },
        (no: boolean) => {
          alert("没有在本机检测到百度地图");
        });

checkUrl:百度--baidumap://

               高德--iosamap://

urlSheme:百度--baidumap://map/geocoder?address=xxx

                高德--iosamap://poi?sourceApplication=applicationName&name=xxx

注意:xxx如果为中文需要进行转码encodeURI(xxx),否则跳转app方法会进error。

如果是安卓平台:

  this.appAvailability.check(pkg)
      .then(
        (yes: boolean) => {
          var sApp = startApp.set({
            "action": "ACTION_VIEW",
            "category": "CATEGORY_DEFAULT",
            "type": "text/css",
            "package": pkg,
            "uri": urlSheme,
            "flags": ["FLAG_ACTIVITY_CLEAR_TOP", "FLAG_ACTIVITY_CLEAR_TASK"],
            "intentstart": "startActivity",
          }, {
              "EXTRA_STREAM": "extraValue1",
              "extraKey2": "extraValue2"
            });
          sApp.start(function () {
            console.log("sApp.start succeed");
          }, function (error) {
            alert("error---" + error);
          });
        },
        (no: boolean) => {
          alert("没有在本机检测到百度地图");
        });

pkg:百度--com.baidu.BaiduMap

        高德--com.autonavi.minimap

urlSheme:百度--baidumap://map/geocoder?address=xxx

                高德--androidamap://poi?sourceApplication=applicationName&keywords=xxx

xxx如果为中文依旧需要encodeURI(xxx)转码。