You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apple MapKit JS中仅当url2非空时添加标注弹窗链接?

解决MapKit JS标注弹窗仅在url2非空时显示contact链接的问题

只需要在创建contact链接和换行元素前加一个条件判断,检查url2是否不为空即可。修改后的代码如下:

// Landmark annotation callout delegate
var calloutDelegate = {
    // Return a div element and populate it with information from the
    // annotation, including a link to a review site.
    calloutContentForAnnotation: function(annotation) {
        var element = document.createElement("div");
        element.className = "review-callout-content";

        var link = element.appendChild(document.createElement("a"));
        link.href = annotation.landmark.url;
        link.textContent = "website";

        // 仅当url2不为空时,添加换行和contact链接
        if (annotation.landmark.url2.trim()) {
            var addLine = element.appendChild(document.createElement("br"));
            var contact = element.appendChild(document.createElement("a"));
            contact.href = annotation.landmark.url2;
            contact.textContent = "contact";
        }

        // Add more content.
        element.style.width = "240px";
        element.style.height = "100px";
        return element;
    }
};

关键说明:

  • 使用trim()是为了排除url2为空白字符串的情况(比如只有空格),确保只有真正有效的链接才会显示。
  • 把换行标签<br>和contact链接的创建逻辑都放到条件判断里,避免url2为空时出现多余的换行。

内容的提问来源于stack exchange,提问作者user982124

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 10:12:54