- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个网站,用户可以在该网站上为列表添加其他链接。我正在尝试将其设置为当用户在 Google 上添加指向某个地点的链接时(例如从 Google map 获取共享链接:https://www.google.com/maps/place/Twin+Elephant+Brewing+Company/@40.7290336,-74.3828372,17z/data=!4m5!3m4!1s0x89c3afa1b597fe49:0x890cb024fe77e7b6!8m2!3d40.7290336!4d-74.3806485),然后我可以使用 Google Places API 获取其他信息。
但是,Google Places API 需要您指定地点 ID,我想知道是否有办法从提供的 map URL 中获取此 ID?我知道您可以从这里手动获取它:https://developers.google.com/maps/documentation/javascript/examples/places-placeid-finder但是是否可以通过 map URL 以某种方式获取地点 ID?
谢谢!
最佳答案
可能有点晚了,但我想试一试......
背景信息:
This answer建议您可以通过传递地点 cid
来获取地点详细信息(与 placeId
不同)到 Places API Web Service .
这使用了一个未记录的查询参数,因此,使用它需要您自担风险。这可能会在没有警告的情况下随时停止工作!
This article建议您可以提取 cid
来自 map URL。
然后我找到了this code snippet这有助于解析 data
Google map URL 中的属性。
不幸的是,您不能将 Places API Web 服务与 AJAX 调用一起使用,因为您会遇到 CORS 问题,因此您需要使用另一种方法。而且您不能使用 Google Maps Javascript API 的地点库,因为它需要您通过 placeId
并且不会接受 cid
.这就是为什么我必须使用 PHP 从 Web 服务中获取结果。
要运行下面的代码,您还需要 jQuery,否则您需要替换 $.ajax(...)
函数,你需要替换 <your_api_key>
在 Web 服务请求(PHP 代码)中使用您自己的 API key 。
所以我们开始...这是 Javascript:
function mapsUrlToPlaceDetails(url) {
var re = /data=(.*)/;
var data = url.match(re);
var str = data[1];
var parts = str.split('!').filter(function (s) {
return s.length > 0;
}),
root = [], // Root elemet
curr = root, // Current array element being appended to
m_stack = [root,], // Stack of "m" elements
m_count = [parts.length,]; // Number of elements to put under each level
parts.forEach(function (el) {
var m = /^(\d+)(\w)(.*)$/.exec(el),
idx = m[1],
kind = m[2],
value = m[3];
// Decrement all the m_counts
for (var i = 0; i < m_count.length; i++) {
m_count[i]--;
}
if (kind === 'm') { // Add a new array to capture coming values
var new_arr = [];
m_count.push(value);
curr.push(new_arr);
m_stack.push(new_arr);
curr = new_arr;
} else {
if (kind == 'b') { // Assuming these are boolean
curr.push(value == '1');
} else if (kind == 'd' || kind == 'f') { // Float or double
curr.push(parseFloat(value));
} else if (kind == 'i' || kind == 'u' || kind == 'e') { // Integer, unsigned or enum as int
curr.push(parseInt(value));
} else { // Store anything else as a string
curr.push(value);
}
}
// Pop off all the arrays that have their values already
while (m_count[m_count.length - 1] === 0) {
m_stack.pop();
m_count.pop();
curr = m_stack[m_stack.length - 1];
}
});
var enc = root[0][0][0];
// Find the CID with a regex
re = /:0x(.*)/;
var cMatch = enc.match(re);
// We have the place CID as hexadecimal, we convert it to decimal in our PHP script
var cid = cMatch[1];
var serviceData = false;
// Query our PHP page with our CID
$.ajax({
type: 'GET',
url: 'get_place_details.php',
data: {
cid: cid
},
success: function (data) {
if (data) {
serviceData = data;
}
},
async: false,
contentType: "application/json",
dataType: 'json',
});
return serviceData;
}
var url = 'https://www.google.com/maps/place/Twin+Elephant+Brewing+Company/@40.7290336,-74.3828372,17z/data=!4m5!3m4!1s0x89c3afa1b597fe49:0x890cb024fe77e7b6!8m2!3d40.7290336!4d-74.3806485';
console.log(mapsUrlToPlaceDetails(url));
这里是 get_place_details.php
页:
<?php
header('Content-Type: application/json');
// Get the hexadecimal CID
$hexString = $_GET['cid'];
// Convert it to decimal
$decString = bchexdec($hexString);
// Query the web service
$url = 'https://maps.googleapis.com/maps/api/place/details/json?cid=' . $decString . '&key=<your_api_key>';
$json = file_get_contents($url);
// Print results
print $json;
// Convert hexadecimal CID to decimal
function bchexdec($hex) {
$len = strlen($hex);
for ($i = 1; $i <= $len; $i++)
$dec = bcadd($dec, bcmul(strval(hexdec($hex[$i - 1])), bcpow('16', strval($len - $i))));
return $dec;
}
这是您问题中示例 URL 的输出:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "13",
"short_name" : "13",
"types" : [ "street_number" ]
},
{
"long_name" : "Watchung Avenue",
"short_name" : "Watchung Ave",
"types" : [ "route" ]
},
{
"long_name" : "Chatham",
"short_name" : "Chatham",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Morris County",
"short_name" : "Morris County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New Jersey",
"short_name" : "NJ",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "07928",
"short_name" : "07928",
"types" : [ "postal_code" ]
}
],
"adr_address" : "\u003cspan class=\"street-address\"\u003e13 Watchung Ave\u003c/span\u003e, \u003cspan class=\"locality\"\u003eChatham\u003c/span\u003e, \u003cspan class=\"region\"\u003eNJ\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e07928\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eUSA\u003c/span\u003e",
"formatted_address" : "13 Watchung Ave, Chatham, NJ 07928, USA",
"formatted_phone_number" : "(973) 507-9862",
"geometry" : {
"location" : {
"lat" : 40.7290336,
"lng" : -74.38064849999999
},
"viewport" : {
"northeast" : {
"lat" : 40.7305861302915,
"lng" : -74.37907051970849
},
"southwest" : {
"lat" : 40.7278881697085,
"lng" : -74.3817684802915
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "033d042f48225906fe15f77f5b3a3e7b6aeeab89",
"international_phone_number" : "+1 973-507-9862",
"name" : "Twin Elephant Brewing Company",
"opening_hours" : {
"open_now" : false,
"periods" : [
{
"close" : {
"day" : 4,
"time" : "2100"
},
"open" : {
"day" : 4,
"time" : "1600"
}
},
{
"close" : {
"day" : 5,
"time" : "2200"
},
"open" : {
"day" : 5,
"time" : "1600"
}
},
{
"close" : {
"day" : 6,
"time" : "2200"
},
"open" : {
"day" : 6,
"time" : "1200"
}
}
],
"weekday_text" : [
"Monday: Closed",
"Tuesday: Closed",
"Wednesday: Closed",
"Thursday: 4:00 – 9:00 PM",
"Friday: 4:00 – 10:00 PM",
"Saturday: 12:00 – 10:00 PM",
"Sunday: Closed"
]
},
"photos" : [
{
"height" : 2448,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/116322776679362173973/photos\"\u003eJohn Cisco\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAA5OjbmTwkvKGIODbDIq7iqRmY936SHMmM8Lp-ao-tZz9TjysnV1iLo8TU7c3vaOqKEEFp9sjgdQXaryTXmkayEgbRf1R_kHTUMALncnIbJgUUNWUQfVZZZFYwAVSNT3xrEhBoDze6Deo-OY-R4QBsJSGUGhRBTr8UU62fuUe7uQ8GC_eGZVy-0Q",
"width" : 3264
},
{
"height" : 640,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/108115407768700900550/photos\"\u003eTwin Elephant Brewing Company\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAkseOCiYmA_QuZtUtpLL32udgqGJHWUxmQPH646Ml16SjCwRv1gS-ynYkWM5n54m36S2uUstVZ0ld4nDlosYblDeQxvq8_KxRrVyKtavY8cQvY9l2cidOWgF7XBYKVzSMEhDO3A3SJPio3XhenCtDgkhUGhTDMYIMOvpFmkQXy4dMYQD19-_VkQ",
"width" : 480
},
{
"height" : 1080,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/105468313205986835052/photos\"\u003eU. Minocha\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAFY3e-92GOB5gZ02gRnRisBWhqIr-_bCxDhNff5kYX7XRtcDY-3Q4d1eRN71fouXOJWQH02EUXXmsDaSk9nw91ajUMjdZXdUdgnWsLtWci_QwBmTs604TDtD9IBOUgdsGEhDGUo8VR6VxAz5dn58v2K5EGhTyypm6Z3dw8VRo7ARzeY0B33jpHA",
"width" : 1920
},
{
"height" : 2988,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/106472583958367361068/photos\"\u003eSteven Grillo\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAA_toJLI4dqBnPcYWU3xcg74OTOguLrFcYuobfrefJKzMI1YvzLC5su3qG5ePhwwCNPhbuf2yRrf1xAwNyb786f9W6SUcPqm1Ixys7kdtLRtq2PCGGR7uJPNy7diIxltAFEhDJ1iv7zV9Wy-L2LmMK6awaGhS8lS-vGVLbyuoBvnLUHMoo7G9P-g",
"width" : 5312
},
{
"height" : 3024,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/102366024100729755902/photos\"\u003eGiri Sonty\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAxUDQSpdmVLqwEhkmglkZCgHrgF8pu5fQuEwQqJGdPX-LF4HFfVTW3P-sCORrFZToktnujwI5_qsC6s7lYbdaXt3-zyUZ5Y1cG-Qsf8iyPdfLFKbmqzQYePDyESBbotZKEhDkJqPi01x3SBsMfB9F7ozpGhSuBaRTN8WuFjKLFnhHmQBBhjCpFg",
"width" : 4032
},
{
"height" : 1920,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/105155780654274802017/photos\"\u003eTim Besecker\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAnYSYhIWKK5g6iw5YuE3W6rcrcsMjLkkAh7WOwrK7byCBH5mXOVEukvboWIEBR_03igd52Nv2j65HkaOZtqEOt5wIg_-jUxyGUoiYWSgx5lE0Bfr7lfr_hLIL0F1Ih5QgEhC2T7UwbXzWyMntI1hjYwimGhQuwFneOqYQFlkXRVk2djZMH8MukQ",
"width" : 1536
},
{
"height" : 4048,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/111388773475383063875/photos\"\u003eChris Bryant\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAALgkt5N4lbOd9KKbodX6e85cr53AMmTCHtUcpY4A8GstXaYQ0A3ZxVy3HidMeRsq6ttB335aY3caGghlzlGn6ZKTh3UYqhBIWDEtbAP55Sfmn_ejguBmUwXGvYPXwXnJ0EhAMxT0nVxswsz3t4vKMXNYlGhS61J6w9kwPE78p48JziVu6BuXpOQ",
"width" : 3036
},
{
"height" : 3904,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/113301757065540994941/photos\"\u003eBrian Davies\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAAwBpwY85PAJA8kMCwHfFa5AjfKhWKt9tWr24dW4c3jg7OKgem6Fiy3ujkgJn3I2qjdnhaQ_doPoh7nOFC3pMyk0lhg3PBrEmlV4I-WV_b-ijbSCyNsc6XSgWCa33lJCrVEhCiuzN8SVwykAsKYnaqr5wyGhRBmlmH4UF3sli--wgW3NFt-JCnaQ",
"width" : 2928
},
{
"height" : 3006,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/104479106479135628391/photos\"\u003eMark Burgos\u003c/a\u003e"
],
"photo_reference" : "CmRaAAAA_eEr3xWGNj_0Z9ZV7kCtLoShbGyV_Lm7fWPgqzoI-XkZDXEpKN6lyEka5DdbHPa_UKCZFJc2NHqcCHcO2wqohW5kOgev6KJBPowo-dwzweebBOHqjHYOIDTzCuNI-e3JEhDlQWANhY1vjamISDqmerQDGhSyKw-BY2sF2z2PJVP5Ebxn4XyUOw",
"width" : 4442
},
{
"height" : 2160,
"html_attributions" : [
"\u003ca href=\"https://maps.google.com/maps/contrib/101684835260872241844/photos\"\u003eJohn Worobetz\u003c/a\u003e"
],
"photo_reference" : "CmRZAAAAaxND_m7EEmb_H-AcjE11kV9TSpDNmqSp_yb9rvoeMfXli-YpkNl40ynUbFbIdBlvzQABml_g8QyVRVjO1xX_22tLDGC2BjRYjUVU2jSQ3lh8vpBQXpVS-QqgsRiKs5MjEhDPSv331G9tFTL4i9ZRxrTtGhQiu6Y77QuIF-yDjC5K7l03RHX0RQ",
"width" : 2880
}
],
"place_id" : "ChIJSf6XtaGvw4kRtud3_iSwDIk",
"rating" : 4.6,
"reference" : "CmRSAAAA81Dn88T7QUFqgafk6p_RmBF2QwxF4ciZ1RiAl2FX_FnH_6V8xhBut-wwn55PA5RqzR1lnASt8IU220bIcnhK-X5LaOZfiRA-Ev4xqFIwD_5Bih40Zp0_r90rujcd0aDtEhBmmicu6C2zft-mfZaKRM44GhS9vO5jT0yZ21uPyvkNkU47rrHSUQ",
"reviews" : [
{
"author_name" : "Chris Hall",
"author_url" : "https://www.google.com/maps/contrib/105472690796059859093/reviews",
"language" : "en",
"profile_photo_url" : "https://lh6.googleusercontent.com/-c2FE0ic_69g/AAAAAAAAAAI/AAAAAAAAAJE/JkiTdWGipz8/s128-c0x00000000-cc-rp-mo-ba2/photo.jpg",
"rating" : 5,
"relative_time_description" : "a month ago",
"text" : "Great local brewery with a great selection of beers. The outdoor, indoor decor gives it a great vibe and is a happening place for families and friends of all (legal) ages. I wold definitely recommend the flight tasters. Only one downfall, they're always out of growlers, so I never get to take my favorite taster home. Great spot overall!",
"time" : 1522938350
},
{
"author_name" : "U. Minocha",
"author_url" : "https://www.google.com/maps/contrib/105468313205986835052/reviews",
"language" : "en",
"profile_photo_url" : "https://lh5.googleusercontent.com/-DvbLy6Lfe1Y/AAAAAAAAAAI/AAAAAAAAcok/XtWUnfo600c/s128-c0x00000000-cc-rp-mo-ba5/photo.jpg",
"rating" : 4,
"relative_time_description" : "2 months ago",
"text" : "Place is hard to find and parking is limited but worth it. They have 3 oz 5-beer flights, 8 and 16 oz pours. Good beers - different styles; not a mono-style place. Place was packed at 3:30 pm on Sat afternoon (imagine that) but the lines move quick and staff is friendly. Great rustic decor.",
"time" : 1518901110
},
{
"author_name" : "Chris D'Anna",
"author_url" : "https://www.google.com/maps/contrib/109252609008737722129/reviews",
"language" : "en",
"profile_photo_url" : "https://lh6.googleusercontent.com/-XoBlrw7wJN4/AAAAAAAAAAI/AAAAAAAAFhk/ksUvusFcOk8/s128-c0x00000000-cc-rp-mo-ba3/photo.jpg",
"rating" : 5,
"relative_time_description" : "3 weeks ago",
"text" : "Great fast service and very knowledgeable. And the beer was delicious. My favorite part is they offer many for takeout growler. I will definitely be back very soon.",
"time" : 1524017924
},
{
"author_name" : "Eric Larsen Larsen",
"author_url" : "https://www.google.com/maps/contrib/104887350051046383743/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-EH-6GOaD73U/AAAAAAAAAAI/AAAAAAAAAAA/AIcfdXA8ntBCFqZ6ozlr3xDuN5In7Gemmg/s128-c0x00000000-cc-rp-mo/photo.jpg",
"rating" : 5,
"relative_time_description" : "a week ago",
"text" : "I have been stopping in for 9 months now. Awesome employees. Great Beer and descent selection. My favorite to date Weepel and Lil Shimmy",
"time" : 1525543191
},
{
"author_name" : "Pete Antoniewicz",
"author_url" : "https://www.google.com/maps/contrib/108718838193824181246/reviews",
"language" : "en",
"profile_photo_url" : "https://lh4.googleusercontent.com/-59_xOCRQPDI/AAAAAAAAAAI/AAAAAAAAAAA/l-PiY85YX5g/s128-c0x00000000-cc-rp-mo-ba5/photo.jpg",
"rating" : 3,
"relative_time_description" : "2 months ago",
"text" : "Nice place, beers ok. A little crowded though there was a big birthday party taking up a lot of room. Garbage cans right by the entrance. Hopefully working out the kinks. A great addition to the area.",
"time" : 1520108642
}
],
"scope" : "GOOGLE",
"types" : [ "food", "point_of_interest", "establishment" ],
"url" : "https://maps.google.com/?cid=9875461755851237302",
"utc_offset" : -240,
"vicinity" : "13 Watchung Avenue, Chatham",
"website" : "http://www.twinelephant.com/"
},
"status" : "OK"
}
再次希望这有帮助:使用风险自负!
关于javascript - 如何从 Google map URL 获取 Google 地点 ID (placeId),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48778154/
$query = "SELECT Name, CountryCode FROM City ORDER BY Name"; if ($result = $mysqli->query($query)) {
我是 GWT 的新手,我的英语不太好,请见谅。 我需要使用 GWT 创建一个网站。该网站具有层次结构:主要的全局标题和几个按钮。每个按钮都会打开应用程序的新部分,该部分有自己的子选项卡。每个子选项卡都
我正在尝试使用此网站 https://www.timeanddate.com/weather/通过打开如下 URL 使用 BeautifulSoup4 抓取天气数据: quote_page=r"htt
由于我要在移动应用中使用谷歌地图和地点,因此我不希望任何东西减慢应用加载速度。 我发现在边缘网络上,我的应用程序启动速度非常慢。我所有的js都是本地的,所以只有谷歌地图和地点需要很长时间才能加载(如果
我可以通过带有位置 ID 的图形 API 发送查询来返回位置的详细信息,但是我希望实现相反的结果 - 通过发送包含位置名称(城市,州等)。这可能吗? 最佳答案 这在几种不同的方法下是可能的。您可以使用
我正在尝试返回仅限于某种区域的自动完成结果 - the (regions) type collection instructs the Places service to return any res
我有一个多语言网站... 我在 visual studio 中为语言(en + 我的语言)制作了两个文件夹,并为每个文件夹制作了我的网站! 对于一种语言的网站,我有一门类(class)可以根据我的文化
我使用 this向FS请求获取 field 但有些场馆与要求的位置之间的距离超过 500 米。换句话说,ll 和 radius 参数没有像我预期的那样工作。因为我希望拥有所请求区域的所有 field
如果我有一段文本,例如包含一篇加泰罗尼亚语报纸文章,我如何从该文本中找到所有城市? 我一直在查看 python 的 nltk 包,我已经下载了加泰罗尼亚语的语料库 (nltk.corpus.cess_
R 的帮助系统很棒,我喜欢几乎所有函数的帮助页面都包含一个示例,但有时我希望有一种方法可以查看示例代码的结果,而无需复制并粘贴到我当前的 session 中。这对于图形功能来说是双重的。 有没有办法执
我想实现类似的效果,其中引脚被分组在一起,然后当您接近点时用动画分开。当您选择带有地理标记应用的地点时,会在“照片”应用中看到此效果 最佳答案 似乎没有,但有一个第三方库提供了此功能: http://
我在报表生成器/设计模式中看到,有一个“位置”和“位置”属性,可让您设置表格应显示在距顶部或左侧的距离。 我的问题是,有没有办法在同一位置设置 2 个或更多表格(即页面顶部,从顶部开始 0,从左边开始
所以,我有一个 Spring MVC 应用程序,我想要一种方法来确定资源何时不支持特定媒体类型。我正在考虑使用自定义 View 解析器来执行此操作...如果没有一个 View 解析器找到 View 名
有了这个类 import UIKit import CoreLocation private let geometryKey = "geometry" private let locationKey
我正在构建一个系统,其中应该有一个包含组织、位置、部门和用户的数据库。它们按照您的预期连接起来: 每个组织可以包含一个或多个地点 每个地点(建筑物)可以包含一个或多个部门 每个部门工作一名或多名用户
我一直在旧项目上使用 Google place API,但在那些项目中我必须选择一个地方,然后 API 返回给我相关信息地方... 问题是使用相同的 API 我可以请求一个地方,例如 Restaura
这里我有一个脚本,它通过 google places api 显示对象的开放时间。 http://jsbin.com/ATaZEXE/4 现在我想将 .label 类的背景颜色更改为“红色”,如果 p
我正在开发一款 Google map 应用,目标用户可能需要关于特定位置的路线,这些位置只能通过应用中的经纬度获知。 问题是方向需要解析为人类可读的名称,最好是任何位置都可以相对于另一个附近的位置进行
我正在开发一款应用程序,我相信它会对学生有所帮助。我想根据位置包括天气(仅温度)。例如,如果我要从一个城市前往另一个城市,我希望它能针对新城市自动更新。我该怎么做?对于我的第二个问题,我还想将日期(即
我正在使用上游模块来平衡我的网站。出于某种原因,我必须将一些请求重定向到指定的服务器。简而言之:我想将 domain.com 重定向到指定的服务器,domain.com/anything 可以由上游模
我是一名优秀的程序员,十分优秀!