2017-10-14 45 views
0

我需要打開谷歌地圖,當我點擊一個按鈕,並顯示從源位置和目標位置的方向。通過搜索很多,我想出了一個InAppBrowser的方式,但它不工作。離子2打開谷歌地圖與2個位置的方向

這裏是我找到的代碼:

this.geolocation.getCurrentPosition().then((data) => { 
     let lat = data.coords.latitude; 
     let lng = data.coords.longitude; 
     let bro = new InAppBrowser; 

     if (this.platform.is('ios')) { 
     bro.create('geo://?q=&saddr=' + data.coords.latitude + ',' + data.coords.longitude + '&daddr=' + this.dataitem['lat'] + ',' + this.dataitem['lng'], '_system'); 
     }; 
     if (this.platform.is('android')) { 
     bro.create('geo://?q=' + data.coords.latitude + ',' + data.coords.longitude + '&daddr=' + this.dataitem['lat'] + ',' + this.dataitem['lng'] + '', '_system'); 
     }; 
    }).catch((err) => { 
     console.log(JSON.stringify(err)); 
    }); 

有人能幫忙嗎?

+0

您是否嘗試過使用[IonicNative](https://ionicframework.com/docs/native/launch-navigator/)執行此任務?這可能比嘗試使用查詢字符串執行瀏覽器更好。 –

回答

0

安裝Launch Navigator插件:

  1. 安裝科爾多瓦和離子本地插件:

離子科爾多瓦插件添加 uk.co.workingedge.phonegap.plugin.launchnavigator

npm install --save @ ionic-native/launch-navigator

  • 添加LaunchNavigator到app.module.ts
  • 這段代碼添加到您的TS文件:

    source: any = [22.303894, 70.802160] // source lat,long 
    destination: any = [23.022505, 72.571362] // dest lat,long 
    
    navigate(){ 
    
    let options: LaunchNavigatorOptions = { 
        start: this.source 
    }; 
    
    this.launchnavigator.navigate(this.destination, options) 
        .then(
        success => alert('Launched navigator'), 
        error => alert('Error launching navigator: ' + error) 
    ); 
    } 
    
  • 完成!