首页 文章

如何将地址转换为Google Maps Link(非MAP)

提问于
浏览
256

在网上寻找(谷歌搜索)一段时间后,我找不到任何需要地址的地址:

1200 Pennsylvania Ave SE, Washington, District of Columbia, 20003

并将其转换为可点击的链接:

http://maps.google.com/maps?f=q&source=s_q&hl=en&q=1200+Pennsylvania+Ave+SE,+Washington,+District+of+Columbia,+20003&sll=37.0625,-95.677068&sspn=44.118686,114.169922&ie=UTF8&cd=1&geocode=FT5MUQIdIDlp-w&split=0&ll=38.882147,-76.99017&spn=0.01064,0.027874&z=16&iwloc=A

jQuery或PHP首选或只有任何有用的信息 .

14 回答

  • 602

    C#Replace方法通常适用于我:

    foo = "http://maps.google.com/?q=" + address.Text.Replace(" ","+");
    
  • 1

    此外,任何人想要手动URLENCODE地址:http://code.google.com/apis/maps/documentation/webservices/index.html#BuildingURLs

    您可以使用它来创建符合GM标准的特定规则 .

  • 1

    我刚发现这个并喜欢分享..

    • 在maps.google.com上搜索您的地址

    • 点击右下方的齿轮图标

    • 点击"shared or embed map"

    • 点击短网址复选框并将结果粘贴到href ..

  • 2
  • 1

    我有一个类似的问题,我需要为网站上的每个地址完成此操作(每个地址都包含在地址标记中) . 这个jQuery对我有用 . 它将获取每个 <address> 标记并将其包含在带有标记包含的地址的谷歌 Map 链接中!

    $("address").each(function(){
    
        var address = $(this).text().replace(/\,/g, '');
        var url = address.replace(/\ /g, '%20');
    
        $(this).wrap('<a href="http://maps.google.com/maps?q=' + url +'"></a>');
    
    });
    

    工作示例 - > https://jsfiddle.net/f3kx6mzz/1/

  • 0

    这个怎么样?

    https://maps.google.com/?q=1200 Pennsylvania Ave SE, Washington, District of Columbia, 20003

    https://maps.google.com/?q=term
    

    如果您有lat-long,请使用以下网址

    https://maps.google.com/?ll=latitude,longitude
    

    示例:maps.google.com/?ll=38.882147,-76.99017

    UPDATE

    截至2017年,Google现在可以通过官方方式创建跨平台的Google Map 网址:

    https://developers.google.com/maps/documentation/urls/guide

    你可以使用像这样的链接

    https://www.google.com/maps/search/?api=1&query=1200%20Pennsylvania%20Ave%20SE%2C%20Washington%2C%20District%20of%20Columbia%2C%2020003

  • 3

    这是我从其中一篇Google Map 文章中找到的内容:

    打开谷歌 Map . 确保您要嵌入的 Map 或街景图像显示在 Map 上 . 在左上角,单击主菜单☰ . 单击“共享”或“嵌入 Map ” . 在显示的框顶部,选择“嵌入 Map ”选项卡 . 选择所需的大小,然后复制代码并将其粘贴到您的网站或博客的源代码中 . 注意:如果您在精简模式下使用 Map ,则无法嵌入 Map . 请注意,嵌入式 Map 中可能无法提供交通信息和其他一些 Map 信息 .

    enter image description here

  • 1

    http://www.labnol.org/internet/tools/short-urls-for-google-maps/6604/上,他们显示了一个非常好的短网址

    谷歌 Map 网址非常笨拙,特别是在通过即时消息,推文或电子邮件发送时 . 只需将位置地址指定为搜索参数,MapOf.it即可为您提供链接到Google Map 的快捷方式 .

    http://mapof.it/

    我将它用于我设计的一些应用程序,它就像一个魅力 .

  • 0

    最好的方法是使用这一行:

    var mapUrl = "http://maps.google.com/maps?f=q&amp;source=s_q&amp;hl=en&amp;geocode=&amp;q=16900+North+Bay+Road,+Sunny+Isles+Beach,+FL+33160&amp;aq=0&amp;sll=37.0625,-95.677068&amp;sspn=61.282355,146.513672&amp;ie=UTF8&amp;hq=&amp;hnear=16900+North+Bay+Road,+Sunny+Isles+Beach,+FL+33160&amp;spn=0.01628,0.025663&amp;z=14&amp;iwloc=A&amp;output=embed"
    

    请记住在必要时替换第一个和第二个地址 .

    You can look at work sample

  • 1

    如果您有纬度和经度,则可以使用以下URL的任何部分或全部

    https://www.google.com/maps/@LATITUDE,LONGITUDE,ZOOMNUMBERz?hl=LANGUAGE
    

    例如:https://www.google.com/maps/@31.839472,54.361167,18z?hl=en

  • 10

    我知道我为后人的缘故做出了贡献 .

    我写了一个简短的jQuery函数,它会自动将任何 <address> 标签转换为Google Map 链接 .

    See a demo here.

    $(document).ready(function () {
       //Convert address tags to google map links - Michael Jasper 2012
       $('address').each(function () {
          var link = "<a href='http://maps.google.com/maps?q=" + encodeURIComponent( $(this).text() ) + "' target='_blank'>" + $(this).text() + "</a>";
          $(this).html(link);
       });
    });
    

    奖金:

    我还遇到了一种情况,要求从链接生成嵌入式 Map ,虽然我会与未来的旅行者分享:

    View a full demo

    $(document).ready(function(){
        $("address").each(function(){                         
            var embed ="<iframe width='425' height='350' frameborder='0' scrolling='no'  marginheight='0' marginwidth='0' src='https://maps.google.com/maps?&amp;q="+ encodeURIComponent( $(this).text() ) +"&amp;output=embed'></iframe>";
            $(this).html(embed);             
        });
    });
    
  • 64

    借用Michael Jasper和Jon Hendershot的解决方案,我提供以下内容:

    $('address').each(function() {
        var text = $(this).text();
    
        var q    = $.trim(text).replace(/\r?\n/, ',').replace(/\s+/g, ' ');
        var link = '<a href="http://maps.google.com/maps?q=' + encodeURIComponent(q) + '" target="_blank"></a>';
    
        return $(this).wrapInner(link);
    });
    

    与以前提供的解决方案相比,此解决方案具有以

    • 它不会删除 <address> 中的HTML标记(例如 <br> 标记),因此会保留格式

    • 它正确编码了URL

    • 它会占用额外的空格,以便生成的URL在编码后更短,更清晰,人性化

    • 它产生有效的标记(Mr.Hendershot的解决方案创建 <a><address></address></a> ,这是无效的,因为在 <a> 等内联元素中不允许使用块级元素,例如 <address> .

    Caveat :如果您的 <address> 标记包含块级元素(如 <p><div> ),则此JavaScript代码将生成无效标记(因为 <a> 标记将包含这些块级元素) . 但如果你只是做这样的事情:

    <address>
      The White House
      <br>
      1600 Pennsylvania Ave NW
      <br>
      Washington, D.C.  20500
    </address>
    

    然后它会工作得很好 .

  • 0

    2016年2月:

    我需要根据客户输入的数据库值并且没有lat / long生成器来执行此操作 . 谷歌这些天真的很喜欢拉/长 . 这是我学到的:

    1 链接的开头如下所示:https://www.google.com/maps/place/

    2 然后你把你的地址:

    • 使用而不是任何空格 .

    • 在城市的前后放一个逗号 .

    • 包括邮政/邮政和省/州 .

    • 用任何东西替换任何# .

    • 替换任何或单个

    3 把地址放在地方之后/

    4 然后在最后放一个斜线 .

    注意:最后的斜线很重要 . 用户点击该链接后,Google继续向该网址添加更多内容,并在此斜杠后执行此操作 .

    Working example for this question:

    https://www.google.ca/maps/place/1200+Pennsylvania+Ave+SE,+Washington,+DC+20003/

    我希望有所帮助 .

  • 0
    http://maps.google.com/maps?q=<?php echo urlencode($address); ?>
    

    编码你转换并添加所有额外的元素,如空格和所有 . 因此,您可以轻松地从数据库中获取平面文本代码并使用它,而无需担心要添加的特殊字符

相关问题