Reputation: 678
I need to open google maps when i click a button and show directions from a source location and destination location. By searching a lot i figured out a way with InAppBrowser, but it doesnt work.
Here is the code i found:
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));
});
Can someone help?
Upvotes: 2
Views: 4081
Reputation: 111
Use this to open maps, make sure In App Browser plugin is installed.
let destination = latitude + ',' + longitude;
if(this.platform.is('ios')){
window.open('maps://?q=' + destination, '_system');
} else {
let label = encodeURI('My Label');
window.open('geo:0,0?q=' + destination + '(' + label + ')', '_system');
}
Upvotes: 1
Reputation: 9697
Install Launch Navigator plugins:
ionic cordova plugin add uk.co.workingedge.phonegap.plugin.launchnavigator
npm install --save @ionic-native/launch-navigator
add LaunchNavigator into app.module.ts
Add this code into your ts file :
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)
);
}
Done!
Upvotes: 4