使用Linking.canOpenURL(url) 打开Google地图时的问题
我在用React Native做应用开发,需要通过我的应用打开地图。
if (Platform.OS === 'ios' && mapProvider === 'apple') {
// Apple Maps doesn't support Google Place IDs, so we just use the encoded address string
const url = `http://maps.apple.com/?saddr=${sourceLat},${sourceLng}&daddr=${encodedAddress}&dirflg=d`;
Linking.openURL(url);
} else {
// Default Google Maps URL (Works on both Android and iOS)
let url = `https://www.google.com/maps/dir/?api=1&origin=${sourceLat},${sourceLng}&destination=${encodedAddress}&travelmode=driving`;
if (googlePlaceId) {
url += `&destination_place_id=${googlePlaceId}`;
}
Linking.canOpenURL(url).then((supported) => {
if (supported) {
Linking.openURL(url);
} else {
Toast.show({
type: "error",
text1: "Google Maps app is not installed",
});
}
}).catch(() => {
Toast.show({
type: "error",
text1: "Failed to open map",
});
});
我一直在使用这段代码来实现这个目的,但现在突然在某些Android设备上,用户报告收到一个错误的Toast提示,如下所示:
Google Maps app is not installed
他们表示他们刚刚更新了地图应用。
那么,真正的问题到底是什么?
解决方案
下面是一个示例,明确声明你要查询的scheme(URL方案)或应用包的Intent可见性:
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<data android:scheme="geo" />
</intent>
<package android:name="com.google.android.apps.maps" />
</queries>
如果自Android 11(API级别30)起没有添加这些条目,canOpenURL 将返回false。
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。