Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/414.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将坐标转换为谷歌地图链接_Javascript_Regex_Hyperlink_Gps_Coordinates - Fatal编程技术网

Javascript 将坐标转换为谷歌地图链接

Javascript 将坐标转换为谷歌地图链接,javascript,regex,hyperlink,gps,coordinates,Javascript,Regex,Hyperlink,Gps,Coordinates,我想做的是创建一个用户脚本,将不同的GPS坐标转换为指向Google地图的超链接 示例: 我有一个页面,其中24.197611 N,120.780512 E为文本。我想能够点击它打开另一页,导致 由于坐标位于括号内(14.495569 N,9.139927 E),因此我使用了如下内容: $("div").html(function(i, html) { return html.replace(/\((.+?)\)/g, "<a href='http://maps.googl

我想做的是创建一个用户脚本,将不同的GPS坐标转换为指向Google地图的超链接

示例

我有一个页面,其中
24.197611 N,120.780512 E
为文本。我想能够点击它打开另一页,导致

由于坐标位于括号内(14.495569 N,9.139927 E),因此我使用了如下内容:

    $("div").html(function(i, html) {
    return html.replace(/\((.+?)\)/g, "<a href='http://maps.google.com/maps?q=#$1'>$1</a>");
});
$(“div”).html(函数(i,html){
返回html.replace(/\(.+?)\)/g,“”;
});

但是链接也有粗体部分。

我建议您使用以下正则表达式来匹配您使用的格式中的坐标:

\((<b>)?(\d+\.\d+ [NEWS], \d+\.\d+) [NEWS](<\/b>)?\)
\(()?(\d+\.\d+[NEWS],\d+\.\d+[NEWS]()?\)
在本例中,您有三个捕获组。捕获组#2与Google Maps hyperlink使用的格式中的坐标匹配

下面是它如何与您提供的代码配合使用:

$("div").html(function(i, html) {
return html.replace(/\((<b>)?(\d+\.\d+ [NEWS], \d+\.\d+) [NEWS](<\/b>)?\)/g, "<a href='http://maps.google.com/maps?q=#$2'>$2</a>");
});
$(“div”).html(函数(i,html){
返回html.replace(/\(()?(\d+\.\d+[NEWS],\d+\.\d+[NEWS]()?\)/g,“”;
});

所以,最后我用了一种像这样的东西

    function myFunction() {
    $("div").each(function(i, html) {
        var $this = $(this);
        $this.html($this.html().replace(/((\d+\.\d+) [N], (\d+\.\d+) [E])/g, "<a href='http://maps.google.com/maps?q=$1' target='_blank'>$1</a>"));
        });
  }
  $(document).ready(myFunction);
  setTimeout(myFunction, 2000);
函数myFunction(){
$(“div”)。每个(函数(i,html){
var$this=$(this);
$this.html($this.html();
});
}
$(文档).ready(myFunction);
设置超时(myFunction,2000);

即使它似乎减缓了速度

基本方向也没有问题,
http://maps.google.com/maps?q=24.197611 N、 120.780512 E
也可以使用,但如果使用粗体标记,则您的解决方案不起作用used@Kharg我更新了答案。现在它应该同时适用于
(14.495569 N,9.139927 E)
(14.495569 N,9.139927 E)
。我强烈建议使用创建和测试正则表达式。