簡體   English   中英

如何在Google標記地圖中添加地址?

[英]How to add address in marker google map?

我的HTML代碼是這樣的:

<a href="#" class="button" data-latLng="53#separator#-1.33#separator#Address 1">Button 1</a>
<a href="#" class="button" data-latLng="-34.397#separator#150.644#separator#Address 2">Button 2</a>
<div id="map-canvas"></div>

我的Javascript代碼是這樣的:

$(document).on("click", ".button", function(e) {
    e.preventDefault();
    var latLng = $(this).attr("data-latLng");           
    initialize(latLng);

    function initialize(latLng) {
        console.log(latLng);
        latLng = latLng.split("#separator#");
        address = latLng[2];
        console.log(address);
        var map;        
        var myCenter=new google.maps.LatLng(latLng[0],latLng[1]);
        var marker=new google.maps.Marker({
            position:myCenter
        });

        var mapProp = {
            center: myCenter,
            zoom: 14,
            mapTypeId:google.maps.MapTypeId.ROADMAP
        };

        map=new google.maps.Map(document.getElementById("map-canvas"),mapProp);
        marker.setMap(map);

        ...
    };
    ...
});

演示是這樣的: https : //jsfiddle.net/oscar11/b0ahcLxw/

我想在標記Google地圖中顯示地址,但我仍然很困惑。 因此,單擊按鈕時,地圖會自動在標記上顯示地址。

有什么解決方案可以解決我的問題嗎?

非常感謝你

您沒有聲明infowindowcontentString變量。 因此,您需要像下面這樣聲明infowindow變量

var infowindow = new google.maps.InfoWindow();

並聲明contentString

var contentString ="Your Content String";

編輯:

// Add this line
var contentString ="Your Content String";
google.maps.event.addListener(marker, 'click', function() {
    // Add this line
    var infowindow = new google.maps.InfoWindow();
    infowindow.setContent(contentString);
    infowindow.open(map, marker);
}); 

JsFiddle示例
自動顯示infowindow JsFiddle演示

添加此行,並在hellooo處設置您的內容。

google.maps.event.addListener(marker, 'click', function() {
var infowindow = new google.maps.InfoWindow();
infowindow.setContent("hellooo");
infowindow.open(map, marker);

}); 

否則你可以在這里找到JsFiddle

為了從lat長期獲取地址,您可以使用反向地理編碼,您可以在此處找到帶有代碼的示例,請嘗試使用此反向地理編碼

暫無
暫無

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

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