在React中显示Google Static Maps API响应

huangapple go评论84阅读模式
英文:

Display Google Static maps API response in React

问题

目前正在尝试使用Google的静态地图API从折线生成图像。该折线来自Strava的API。

这可以通过以下代码完成:

const response = await axios(`https://maps.googleapis.com/maps/api/staticmap?path=enc:${props.obj.map.summary_polyline}&size=400x400&key=${import.meta.env.VITE_GOOGLE_MAPS_API_KEY}}`, {
                        method: 'GET',
                    })

此API调用以200状态响应,返回一个图像,我不知道如何显示。我可以从网络选项卡中看到它是一张图像。

示例响应:
在React中显示Google Static Maps API响应

来自网络选项卡的图像:
在React中显示Google Static Maps API响应

到目前为止,我已经尝试将响应中的数据放入图像的src标签中,正如API文档建议的那样。

下面的图像显示了数据嵌入到HTML中,应该在白色框的位置显示:

在React中显示Google Static Maps API响应

在React中显示Google Static Maps API响应

英文:

Currently trying to use googles statics maps api to generate an image from a polyline. The polyline is from Strava's API's.

This can be done with the following code:

const response = await axios(`https://maps.googleapis.com/maps/api/staticmap?path=enc:${props.obj.map.summary_polyline}&size=400x400&key=${import.meta.env.VITE_GOOGLE_MAPS_API_KEY}}`, {
                        method: 'GET',
                    })

This api call is responding with a 200 status an image that I have no idea how to display. I can see its an image from the network tab

example response:
在React中显示Google Static Maps API响应

Image from network tab:
在React中显示Google Static Maps API响应

So far, I have tried to take the data from the response and put it inside of a src tag on an image, as the api docs suggest.

The image below shows that the data is baked into the html, it should be displaying where the white box is:

在React中显示Google Static Maps API响应

在React中显示Google Static Maps API响应

答案1

得分: 0

以下是您提供的内容的中文翻译:

您在<img/>标签的src中使用request而不是response

官方文档明确指出:

>“Maps Static API 图像嵌入在<img>标签的src属性中,或者在其他编程语言中的等效方式。”

话虽如此,由于您在使用React,我们可以这样做。

要重现您期望的输出,您可以在组件内部声明一个变量,用于存储路径、API密钥和静态地图请求,如下所示:

const path =
  "weight:3%7Ccolor:red%7Cenc:_fisIp~u%7CU}%7Ca@pytA_~b@hhCyhS~hResU%7C%7Cx@oig@rwg@amUfbjA}f[roaAynd@%7CvXxiAt{ZwdUfbjAewYrqGchH~vXkqnAria@c_o@inc@k{g@i`]o%7CF}vXaj\\h`]ovs@?yi_@rcAgtO%7Cj_AyaJren@nzQrst@zuYh`]v%7CGbldEuzd@%7C%7Cx@spD%7CtrAzwP%7Cd_@yiB~vXmlWhdPez\\_{Km_`@~re@ew^rcAeu_@zhyByjPrst@ttGren@aeNhoFemKrvdAuvVidPwbVr~j@or@f_z@ftHr{ZlwBrvdAmtHrmT{rOt{Zz}E%7Cc%7C@o%7CLpn~AgfRpxqBfoVz_iAocAhrVjr@rh~@jzKhjp@``NrfQpcHrb^k%7CDh_z@nwB%7Ckb@a{R%7Cyh@uyZ%7CllByuZpzw@wbd@rh~@%7C%7CFhqs@teTztrAupHhyY%7D]huf@e%7CFria@o}GfezAkdW%7C}[ocMt_Neq@ren@e~Ika@pgE%7Ci%7CAfiQ%7C`l@uoJrvdAgq@fppAsjGhg`@%7ChQpg{Ai_V%7C%7Cx@mkHhyYsdP%7CxeA~gF%7C}[mv`@t_NitSfjp@c}Mhg`@sbChyYq}e@rwg@atFff}@ghN~zKybk@fl}A}cPftcAite@tmT__Lha@u~DrfQi}MhkSqyWivIumCria@ciO_tHifm@fl}A{rc@fbjAqvg@rrqAcjCf%7Ci@mqJtb^s%7C@fbjA{wDfs`BmvEfqs@umWt_Nwn^pen@qiBr`xAcvMr{Zidg@dtjDkbM%7Cd_@";
const APIKey = "YOUR_API_KEY";
const staticMapsUrl =
  "https://maps.googleapis.com/maps/api/staticmap" +
  "?size=400x400&center=59.900503,-135.478011&zoom=4" +
  `&path=${path}` +
  `&key=${APIKey}`;

请注意,这只是我为了更好的可读性而采用的方法。在您的情况下,只要填写请求所需的参数,您可以以任何方式完成这个任务。

然后,您的return应该类似于这样:

return (
  <div>
    <p>React中的静态地图</p>
    <img src={staticMapsUrl} alt="map" />
  </div>
);

有了这一切,输出应该像这样:

在React中显示Google Static Maps API响应

以下是组件的完整代码:

import React from "react";

export default function Map() {
  const path =
    "weight:3%7Ccolor:red%7Cenc:_fisIp~u%7CU}%7Ca@pytA_~b@hhCyhS~hResU%7C%7Cx@oig@rwg@amUfbjA}f[roaAynd@%7CvXxiAt{ZwdUfbjAewYrqGchH~vXkqnAria@c_o@inc@k{g@i`]o%7CF}vXaj\\h`]ovs@?yi_@rcAgtO%7Cj_AyaJren@nzQrst@zuYh`]v%7CGbldEuzd@%7C%7Cx@spD%7CtrAzwP%7Cd_@yiB~vXmlWhdPez\\_{Km_`@~re@ew^rcAeu_@zhyByjPrst@ttGren@aeNhoFemKrvdAuvVidPwbVr~j@or@f_z@ftHr{ZlwBrvdAmtHrmT{rOt{Zz}E%7Cc%7C@o%7CLpn~AgfRpxqBfoVz_iAocAhrVjr@rh~@jzKhjp@``NrfQpcHrb^k%7CDh_z@nwB%7Ckb@a{R%7Cyh@uyZ%7CllByuZpzw@wbd@rh~@%7C%7CFhqs@teTztrAupHhyY}t]huf@e%7CFria@o}GfezAkdW%7C}[ocMt_Neq@ren@e~Ika@pgE%7Ci%7CAfiQ%7C`l@uoJrvdAgq@fppAsjGhg`@%7ChQpg{Ai_V%7C%7Cx@mkHhyYsdP%7CxeA~gF%7C}[mv`@t_NitSfjp@c}Mhg`@sbChyYq}e@rwg@atFff}@ghN~zKybk@fl}A}cPftcAite@tmT__Lha@u~DrfQi}

<details>
<summary>英文:</summary>

## You use the `request` in the `src` of an `&lt;img /&gt;` tag instead of the `response` ##

The official documentation specifically says that:

&gt;_&quot;A Maps Static API image is embedded within an `&lt;img&gt;` tag&#39;s `src` attribute, or its equivalent in other programming languages.&quot;_

With that said, since you are using React, we can do it this way.

To reproduce your expected output, you can declare a variable for your path, API key, and Static Maps Request inside your component like so:

    const path =
      &quot;weight:3%7Ccolor:red%7Cenc:_fisIp~u%7CU}%7Ca@pytA_~b@hhCyhS~hResU%7C%7Cx@oig@rwg@amUfbjA}f[roaAynd@%7CvXxiAt{ZwdUfbjAewYrqGchH~vXkqnAria@c_o@inc@k{g@i`]o%7CF}vXaj\\h`]ovs@?yi_@rcAgtO%7Cj_AyaJren@nzQrst@zuYh`]v%7CGbldEuzd@%7C%7Cx@spD%7CtrAzwP%7Cd_@yiB~vXmlWhdPez\\_{Km_`@~re@ew^rcAeu_@zhyByjPrst@ttGren@aeNhoFemKrvdAuvVidPwbVr~j@or@f_z@ftHr{ZlwBrvdAmtHrmT{rOt{Zz}E%7Cc%7C@o%7CLpn~AgfRpxqBfoVz_iAocAhrVjr@rh~@jzKhjp@``NrfQpcHrb^k%7CDh_z@nwB%7Ckb@a{R%7Cyh@uyZ%7CllByuZpzw@wbd@rh~@%7C%7CFhqs@teTztrAupHhyY}t]huf@e%7CFria@o}GfezAkdW%7C}[ocMt_Neq@ren@e~Ika@pgE%7Ci%7CAfiQ%7C`l@uoJrvdAgq@fppAsjGhg`@%7ChQpg{Ai_V%7C%7Cx@mkHhyYsdP%7CxeA~gF%7C}[mv`@t_NitSfjp@c}Mhg`@sbChyYq}e@rwg@atFff}@ghN~zKybk@fl}A}cPftcAite@tmT__Lha@u~DrfQi}MhkSqyWivIumCria@ciO_tHifm@fl}A{rc@fbjAqvg@rrqAcjCf%7Ci@mqJtb^s%7C@fbjA{wDfs`BmvEfqs@umWt_Nwn^pen@qiBr`xAcvMr{Zidg@dtjDkbM%7Cd_@&quot;;
    const APIKey = &quot;YOUR_API_KEY&quot;;
    const staticMapsUrl =
      &quot;https://maps.googleapis.com/maps/api/staticmap&quot; +
      &quot;?size=400x400&amp;center=59.900503,-135.478011&amp;zoom=4&quot; +
      `&amp;path=${path}` +
      `&amp;key=${APIKey}`;

Please do note that this is just my way of doing this thing for better readability. So in your case, you can do whatever way you want here as long as your filling up the required parameters for the request to be successful.

Then your `return` should look something like this:

    return (
      &lt;div&gt;
        &lt;p&gt;Static Map In React&lt;/p&gt;
        &lt;img src={staticMapsUrl} alt=&quot;map&quot; /&gt;
      &lt;/div&gt;
    );

And with all that, the output should be like this:

[![sample output][1]][1]

Here&#39;s the full code of the component:

    import React from &quot;react&quot;;
    
    export default function Map() {
      const path =
        &quot;weight:3%7Ccolor:red%7Cenc:_fisIp~u%7CU}%7Ca@pytA_~b@hhCyhS~hResU%7C%7Cx@oig@rwg@amUfbjA}f[roaAynd@%7CvXxiAt{ZwdUfbjAewYrqGchH~vXkqnAria@c_o@inc@k{g@i`]o%7CF}vXaj\\h`]ovs@?yi_@rcAgtO%7Cj_AyaJren@nzQrst@zuYh`]v%7CGbldEuzd@%7C%7Cx@spD%7CtrAzwP%7Cd_@yiB~vXmlWhdPez\\_{Km_`@~re@ew^rcAeu_@zhyByjPrst@ttGren@aeNhoFemKrvdAuvVidPwbVr~j@or@f_z@ftHr{ZlwBrvdAmtHrmT{rOt{Zz}E%7Cc%7C@o%7CLpn~AgfRpxqBfoVz_iAocAhrVjr@rh~@jzKhjp@``NrfQpcHrb^k%7CDh_z@nwB%7Ckb@a{R%7Cyh@uyZ%7CllByuZpzw@wbd@rh~@%7C%7CFhqs@teTztrAupHhyY}t]huf@e%7CFria@o}GfezAkdW%7C}[ocMt_Neq@ren@e~Ika@pgE%7Ci%7CAfiQ%7C`l@uoJrvdAgq@fppAsjGhg`@%7ChQpg{Ai_V%7C%7Cx@mkHhyYsdP%7CxeA~gF%7C}[mv`@t_NitSfjp@c}Mhg`@sbChyYq}e@rwg@atFff}@ghN~zKybk@fl}A}cPftcAite@tmT__Lha@u~DrfQi}MhkSqyWivIumCria@ciO_tHifm@fl}A{rc@fbjAqvg@rrqAcjCf%7Ci@mqJtb^s%7C@fbjA{wDfs`BmvEfqs@umWt_Nwn^pen@qiBr`xAcvMr{Zidg@dtjDkbM%7Cd_@&quot;;
      const APIKey = &quot;YOUR_API_KEY&quot;;
      const staticMapsUrl =
        &quot;https://maps.googleapis.com/maps/api/staticmap&quot; +
        &quot;?size=400x400&amp;center=59.900503,-135.478011&amp;zoom=4&quot; +
        `&amp;path=${path}` +
        `&amp;key=${APIKey}`;
    
      return (
        &lt;div&gt;
          &lt;p&gt;Static Map In React&lt;/p&gt;
          &lt;img src={staticMapsUrl} alt=&quot;map&quot; /&gt;
        &lt;/div&gt;
      );
    }

Here&#39;s also a proof of concept sandbox that you can check and play around with: https://codesandbox.io/s/static-maps-in-react-6k6hmp?file=/src/Map.js

I hope this helps!

  [1]: https://i.stack.imgur.com/z5eYW.png

</details>



huangapple
  • 本文由 发表于 2023年6月9日 05:05:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76435686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定