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

使用document.createElement为MapKit JS标注弹窗添加前缀文本问题

解决MapKit JS标注弹窗电话号码前缀问题

直接通过字符串拼接的方式,给电话号码添加"Phone: "前缀即可,修改代码中设置电话号码文本的行:

修改前:

phone.textContent = annotation.landmark.phone;

修改后(两种写法任选其一):

  1. 传统字符串拼接:
phone.textContent = "Phone: " + annotation.landmark.phone;
  1. ES6模板字符串(更简洁):
phone.textContent = `Phone: ${annotation.landmark.phone}`;

完整修改后的代码片段:

// 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";


    var phone = element.appendChild(document.createElement("p"));
    // 这里是修改后的代码
    phone.textContent = `Phone: ${annotation.landmark.phone}`;

    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;
}
}

额外提示(可选):

如果存在电话号码为空的场景,可添加判断避免显示空的"Phone: ":

phone.textContent = annotation.landmark.phone ? `Phone: ${annotation.landmark.phone}` : '';

内容的提问来源于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:25:17