【问题标题】:Google static maps icons only working for a portion of the markers谷歌静态地图图标仅适用于部分标记
【发布时间】:2020-12-28 17:25:31
【问题描述】:

我正在尝试在 Google 静态地图上绘制一系列标记。由于静态地图 API 不支持大于 10 的标签,因此我使用了数字图像。

由于某种原因,静态地图标记仅使用自定义图像更新部分标记,其余部分为默认标记。我想用自定义图像替换所有默认标记。

这是我删除了 API 密钥和敏感数据的代码。

const realtor = require("realtorca");
const unirest = require("unirest");
const nodemailer = require("nodemailer");
const { staticMapUrl } = require("static-google-map");

//https://rapidapi.com/apidojo/api/realtor-canadian-real-estate?endpoint=apiendpoint_bc4d93dd-15c5-47de-b04e-854b2e6176be

var req = unirest(
  "GET",
  "https://realtor-canadian-real-estate.p.rapidapi.com/properties/list-residential"
);

req.query({
  LongitudeMin: -79.44438,
  LongitudeMax: -79.404,
  LatitudeMin: 43.75135,
  LatitudeMax: 43.78448,
  PriceMax: 1500000,
  ZoomLevel: 15,
  BuildingTypeId: 1,
  //NOTE: if there are more than 50 records I need to figure out how to traverse through the pages
  RecordsPerPage: 50,
});

req.headers({
  "x-rapidapi-key": "secret",
  "x-rapidapi-host": "realtor-canadian-real-estate.p.rapidapi.com",
  useQueryString: true,
});

let resultsArray = [];

req.end(function (res) {
  if (res.error) throw new Error(res.error);

  let totalResults = res.body.Paging.TotalRecords;

  let mapObject = {};

  let addMarkers = async function () {
    for (i = 0; i < totalResults; i++) {
      mapObject.markerGroups.push({
        iconURL: `https%3A%2F%2Fapi.geoapify.com%2Fv1%2Ficon%2F%3Ftype%3Dmaterial%26color%3Dred%26icon%3Dcloud%26iconType%3Dawesome%26text%3D${i}%26apiKey%secret`,
        markers: [
          {
            location: {
              lat: res.body.Pins[i].latitude,
              lng: res.body.Pins[i].longitude,
            },
          },
        ],
      });
    }
  };

  mapObject = {
    key: "secret",
    scale: 2,
    size: "1000x1000",
    format: "png",
    markerGroups: [],
  };

  addMarkers();

  let url = staticMapUrl(mapObject);

  //LOGGING TO SEE WHAT HAPPENS
  console.log(totalResults);
  console.log(mapObject);
  console.log(url);

  const forLoop = async (_) => {
    for (let i = 0; i < totalResults; i++) {
      await resultsArray.push(
        "<p>",
        res.body.Results[i].Property.Address.AddressText + "<br />",
        res.body.Results[i].Property.Price + "<br />",
        "Number of bathrooms: " +
          res.body.Results[i].Building.BathroomTotal +
          "<br />",
        "Number of bedrooms: " +
          res.body.Results[i].Building.Bedrooms +
          "<br />",
        "http://www.realtor.ca" +
          res.body.Results[i].RelativeDetailsURL +
          "<br />",
        "Notes: " + res.body.Results[i].PublicRemarks + "</p>"
      );
    }
    console.log(resultsArray);
    console.log(totalResults);

    let transporter = nodemailer.createTransport({
      host: "giow1039.siteground.us",
      port: 587,
      secure: false, // true for 465, false for other ports
      auth: {
        user: "notifications@danielpuiatti.com",
        pass: "secret",
      },
    });

    // send mail with defined transport object
    let info = {
      from: "notifications@danielpuiatti.com", // sender address
      to: "dpuiatti@gmail.com", // list of receivers
      subject: "Hello ✔", // Subject line
      html:
        "<h1>Your daily Realtor.ca update</h1>" +
        String(resultsArray).replace(/,/g, "").replace(/\|/g, " | ") +
        '<img src="' +
        url +
        '" width="800" height="600"/>',
    };

    transporter.sendMail(info, function (err, info) {
      if (err) {
        console.log(err);
        return;
      }
      console.log(info.response);
    });
  };

  forLoop();
});

【问题讨论】:

  • 过去我发现传递给谷歌静态地图的字符串长度是有限的,所以最终使用bit.ly 将标记的实际网址转换为更短的版本
  • 您尝试使用多少个自定义图标?最多允许五个 (developers.google.com/maps/documentation/maps-static/…)。再多也行不通。

标签: javascript node.js google-maps google-maps-api-3 google-static-maps


【解决方案1】:

由于 5 个自定义图标的限制,图标没有改变。我改用字母字符来标记每个点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 2023-04-11
    相关资源
    最近更新 更多