如何将Google地图网址转换为适合iframe的网址 [英] How to turn google maps url into an iframe-friendly url
问题描述
这是一个示例网址:
这显然不起作用:
< iframe
width =600
height =450
frameborder =0style =border:0
src =https://www.google.co.uk/maps/place/Barnes+%26 +贵金属+卖书+第86 +%26 +列克星敦/ @ 40.7747314,-73.9653734,15z /数据=!4M5!1平方米!2M1!1sBarnes +%26 +贵金属,+ 5 +大道+新+纽约,纽约+,+美国+ States!3m1!1s0x0:0xdec8ade72ab827a7
< / iframe>
如何将示例网址转换为适用于iframe的网址?我甚至不需要使用iframe。我只想通过使用上面列出的格式提供的网址来整合谷歌地图。
我使用PHP,如果有问题的话。
鉴于您提供的网址格式,我会尝试将其转换为 Google Maps Embed API URL。这需要一个API密钥。您有以下网址:
Barnes +%26 + Noble +书商+ 86th +%26 +列克星敦
!!!!STRONG> / @ 40.7747314,-73.9653734,15z /数据= 4M51平方米2M1 1sBarnes +%26 +贵金属,+ 5 +大道+新+纽约,纽约+,+美国3M1 1s0x0:!0xdec8ade72ab827a7
将其转换为嵌入网址只需要提取已有网址的地址部分。结果将是:
https://www.google.com/maps/embed/v1/place?key=API_KEY&q= Barnes +%26 + Noble +书商+ 86th +%26 +列克星敦 要收到的网址没有特定的地方,只是一个视图,它可能看起来像这样:
https://www.google.co.uk/maps/@ 40.7777212,-73.9567045 , 15z
在此示例中,您必须提取纬度,经度和缩放级别( 15z),并创建一个这样的URL:
读取API有两种可以类似方式处理的模式(搜索和方向),但不太可能用于你的情况。
至于提取,我可能会使用正则表达式或搜索和子字符串,具体取决于您接收到的URL的变化。
This is a sample url:
This clearly does not work:
<iframe width="600" height="450" frameborder="0" style="border:0" src="https://www.google.co.uk/maps/place/Barnes+%26+Noble+Booksellers+86th+%26+Lexington/@40.7747314,-73.9653734,15z/data=!4m5!1m2!2m1!1sBarnes+%26+Noble,+5th+Avenue,+New+York,+NY,+United+States!3m1!1s0x0:0xdec8ade72ab827a7" </iframe>
How to turn the sample url into a url that works for the iframe? I don't even have to use iframe. I just want to integrate google maps by using urls supplied in the format listed above.
I'm using PHP, if that matters.
解决方案Given the URL format you provided I would try to transform it to Google Maps Embed API URL. This requires a API key. You had the following URL:
Transforming it to an embed URL only requires you to extract the address part of the URL you already have. The result will be:
If you alternatively were to receive the URL with no specific place, just a view, it might look like this:
In this example you have to extract the latitude, longitude and zoom level (15z), and create a URL like this:
https://www.google.com/maps/embed/v1/view?key=API_KEY¢er=40.7777212,-73.9567045&zoom=15
Reading the API there are two more modes (search and direction) that can be handled in a similar way, but aren't as likely to be used in your case.
As for the extracting I would probably use regex or search and substring, depending on how varying the URLs you receive are.
这篇关于如何将Google地图网址转换为适合iframe的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!