How to convert an address into a Google Maps Link (NOT MAP)

前端 未结 14 1706
借酒劲吻你
借酒劲吻你 2020-12-02 03:38

After looking (Googling) on the web for a while, I can find nothing that takes an address like:

1200 Pennsylvania Ave SE, Washington, District of Columb

相关标签:
14条回答
  • 2020-12-02 04:16

    Also, anyone wanting to manually URLENCODE the address: http://code.google.com/apis/maps/documentation/webservices/index.html#BuildingURLs

    You can use that to create specific rules that meet GM standards.

    0 讨论(0)
  • 2020-12-02 04:17

    Borrowing from Michael Jasper's and Jon Hendershot's solutions, I offer the following:

    $('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);
    });
    

    This solution offers the following benefits over solutions previously offered:

    • It will not remove HTML tags (e.g. <br> tags) within <address>, so formatting is preserved
    • It properly encodes the URL
    • It squashes extra spaces so that the generated URL is shorter and cleaner and human-readable after encoding
    • It produces valid markup (Mr.Hendershot's solution creates <a><address></address></a> which is invalid because block-level elements such as <address> are not permitted within inline elements such as <a>.

    Caveat: If your <address> tag contains block-level elements like <p> or <div>, then this JavaScript code will produce in invalid markup (because the <a> tag will contain those block-level elements). But if you're just doing stuff like this:

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

    Then it'll work just fine.

    0 讨论(0)
  • 2020-12-02 04:23

    The C# Replace method usually works for me:

    foo = "http://maps.google.com/?q=" + address.Text.Replace(" ","+");
    
    0 讨论(0)
  • 2020-12-02 04:24

    If you have latitude and longitude, you can use any part or all of bellow URL

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

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

    0 讨论(0)
  • 2020-12-02 04:26

    I had a similar issue where I needed to accomplish this for every address on the site (each wrapped in an address tag). This bit of jQuery worked for me. It'll grab each <address> tag and wrap it in a google maps link with the address the tag contains contains!

    $("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>');
    
    }); 
    

    Working example --> https://jsfiddle.net/f3kx6mzz/1/

    0 讨论(0)
  • 2020-12-02 04:28

    This is what I found from one of the Google Maps articles:

    1. Open Google Maps.
    2. Make sure the map or Street View image you'd like to embed shows up on the map.
    3. In the top left corner, click the main menu ​☰.
    4. Click Share or embed map.
    5. At the top of the box that appears, choose the Embed map tab.
    6. Choose the size you want, then copy the code and paste it into the source code of your website or blog.

    Note: If you're using Maps in Lite mode, you won't be able to embed a map. Keep in mind that traffic information and some other Maps info might not be available in the embedded map.

    0 讨论(0)
提交回复
热议问题