[英]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地圖中顯示地址,但我仍然很困惑。 因此,單擊按鈕時,地圖會自動在標記上顯示地址。
有什么解決方案可以解決我的問題嗎?
非常感謝你
您沒有聲明infowindow
和contentString
變量。 因此,您需要像下面這樣聲明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);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.