I am trying to open google maps or maps in my react-native
application.
When I run the app on my iPhone/simulator, I receive this error
"Don't know how to open URI:...".
What I am doing wrong?
My code:
openGps() {
var url = 'geo:37.484847,-122.148386'
this.openExternalApp(url)
}
openExternalApp(url) {
Linking.canOpenURL(url).then(supported => {
if (supported) {
Linking.openURL(url);
} else {
console.log('Don\'t know how to open URI: ' + url);
}
});
}
To open url with custom label ios/android:
const scheme = Platform.select({ ios: 'maps:0,0?q=', android: 'geo:0,0?q=' });
const latLng = `${lat},${lng}`;
const label = 'Custom Label';
const url = Platform.select({
ios: `${scheme}${label}@${latLng}`,
android: `${scheme}${latLng}(${label})`
});
Linking.openURL(url);
This is because iOS does not yet have support for geo:
yet as mentioned in this SO answer. What you can do is detect the OS and:
geo:
on Androidmaps:
as it will open up Apple Maps, though I'm unsure how to properly send the coordinates to it. Or maybe append it to a google maps HTTP URL and open it in the browser.For example, your openGps
function could look like this:
openGps = (lat, lng) => {
var scheme = Platform.OS === 'ios' ? 'maps:' : 'geo:';
var url = scheme + `${lat},${lng}`;
Linking.openURL(url);
}
const url = Platform.select({
ios: `maps:0,0?q=${fullAddress}`,
android: `geo:0,0?q=${fullAddress}`,
})
Linking.openURL(url)
you can do like this:
Android:
<TouchableOpacity onPress={() => Linking.openURL('google.navigation:q=100+101')}>
where q
is the destination lat + long
IOS:
<TouchableOpacity onPress={() => Linking.openURL('maps://app?saddr=100+101&daddr=100+102')}>
where saddr
is start address and daddr
is destination address lat+long
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With