Google Wallet通用卡片问题:谷歌地图位置与网站URL不显示
Google Wallet通用卡片地图位置与URL无法显示问题排查
我在使用Google Wallet通用卡片时遇到异常:谷歌地图位置和网站URL无法正常显示。已确认卡片内的数据输入正确,但内容仍未按预期展示。相关实现代码如下:
if ($passData->locations) { foreach ($passData->locations as $key => $location) { if ($location->status) { $latitude = $location->latitude; $longitude = $location->longitude; $googleMapsUrl = "http://maps.google.com/?q={$latitude},{$longitude}"; $links[] = new Uri([ 'uri' => $googleMapsUrl, 'description' => $location->description, 'id' => 'map_' . $key ]); } } }
foreach ($passData->locations as $key => $location) { if ($location->status) { $latitude = $location->latitude; $longitude = $location->longitude; $googleMapsUrl = "http://maps.google.com/?q={$latitude},{$longitude}"; $links[] = new Uri([ 'uri' => $googleMapsUrl, 'description' => $location->description, 'id' => 'map_' . $key ]); } } }
问题分析与修正方案
1. 核心错误:位置信息添加方式不符合规范
Google Wallet通用卡片的地图位置不需要手动构造URL添加到links,而是要直接通过locations字段传入经纬度对象,Wallet会自动渲染原生地图入口。当前代码的实现逻辑错误,导致地图位置无法被正确识别展示。
2. 次要问题:代码冗余与协议兼容性
- 两段代码逻辑完全重复,需合并避免冗余。
- 使用
http协议的地图URL可能被Google Wallet拦截,建议替换为HTTPS协议的标准地图链接。 Uri对象缺少type字段指定链接类型,可能导致Wallet无法正确识别链接用途。
修正后的代码示例
// 合并重复逻辑,正确处理位置信息 if ($passData->locations) { foreach ($passData->locations as $key => $location) { if ($location->status && is_numeric($location->latitude) && is_numeric($location->longitude)) { // 方式1:添加原生地图位置(推荐,Wallet自动渲染地图入口) $card->addLocation([ 'latitude' => floatval($location->latitude), 'longitude' => floatval($location->longitude), 'displayName' => $location->description ]); // 方式2:添加地图链接到卡片操作区(可选,作为补充入口) $googleMapsUrl = "https://www.google.com/maps/search/?api=1&query={$location->latitude},{$location->longitude}"; $links[] = new Uri([ 'uri' => $googleMapsUrl, 'description' => $location->description, 'id' => 'map_' . $key, 'type' => 'MAP' // 指定链接类型为地图,帮助Wallet识别 ]); } } }
额外验证项
- 确认经纬度数值有效:纬度范围-9090,经度范围-180180,非数值会导致位置不渲染。
- 检查卡片的
actions字段是否正确关联了links数组,确保URL能显示在卡片操作区域。 - 确保使用的Google Wallet SDK/API为最新版本,旧版本可能存在字段兼容性问题。
内容的提问来源于stack exchange,提问作者Jinshaj P
相关产品推荐
相关产品推荐

