簡體   English   中英

如何在Google Maps InfoWindow中創建鏈接?

[英]How do you create a link in a Google Maps InfoWindow?

序言

我想創建一個Google地圖標記列表,該列表在InfoWindows中填充這些特定位置,並在InfoWindow內部提供指向該位置的Wikipedia文章的鏈接。 我正在此google.maps.event.addListener(place.marker, 'click', function() {} click函數內進行Ajax調用,此函數位於self.allPlaces().forEach(function(place) {}函數僅遍歷我的所有位置。

問題

該鏈接填充在InfoWindows中, 但是只有在您右鍵單擊並在新選項卡中打開后,該鏈接才會打開,而我希望只單擊鼠標左鍵即可打開。

function ajax() {
            return $.ajax({
                type: 'GET',
                url: wikiURL,
                dataType: "jsonp",
                prop: 'pageimages'
            });
        }

        ajax().done(function(response) {
            clearTimeout(self.apiTimeout);
            var articleList = response[1];
            console.log(response);
            if (articleList.length > 0) {
                for (var i = 0; i < articleList.length; i++) {
                    var url = response[3]; // response[3] gives back the wiki URL
                    content = '<div class="infoWindow"><strong>' + place.title + '</strong><br>' +
                                '<p>' + place.formatted_address + '</p>' +
                                '<p>' + response[2] + '</p>' + // response[2] for more modern response
                                '<a href="' + url + '" target="_blank">' +
                                "View full Wikipedia article" + '</a>' +
                    '</div>';
                    infoWindow.setContent(content);
                }
            }

我認為您的實現可能會對錯誤有所幫助。 檢查創建的content變量是否看起來像代碼示例contentString變量

嘗試並遵循鏈接中的示例代碼。

代碼示例:

var contentString = '<div id="content">'+
'<div id="siteNotice">'+
'</div>'+
'<h1 id="firstHeading" class="firstHeading">Uluru</h1>'+
'<div id="bodyContent">'+
'<p><b>Uluru</b>, also referred to as <b>Ayers Rock</b>, is a large ' +
'sandstone rock formation in the southern part of the '+
'Northern Territory, central Australia. It lies 335&#160;km (208&#160;mi) '+
'south west of the nearest large town, Alice Springs; 450&#160;km '+
'(280&#160;mi) by road. Kata Tjuta and Uluru are the two major '+
'features of the Uluru - Kata Tjuta National Park. Uluru is '+
'sacred to the Pitjantjatjara and Yankunytjatjara, the '+
'Aboriginal people of the area. It has many springs, waterholes, '+
'rock caves and ancient paintings. Uluru is listed as a World '+
'Heritage Site.</p>'+
'<p>Attribution: Uluru, <a href="https://en.wikipedia.org/w/index.php?title=Uluru&oldid=297882194">'+
'https://en.wikipedia.org/w/index.php?title=Uluru</a> '+
'(last visited June 22, 2009).</p>'+
'</div>'+
'</div>';
var infowindow = new google.maps.InfoWindow({
content: contentString
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM