gpt4 book ai didi

javascript - 如何在标记谷歌地图中添加地址?

转载 作者:太空宇宙 更新时间:2023-11-04 16:07:50 27 4
gpt4 key购买 nike

我的 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/

我想在标记谷歌地图中显示地址,但我仍然很困惑。所以当点击按钮时, map 会自动在标记上显示地址。

有什么办法可以解决我的问题吗?

非常感谢

最佳答案

您没有声明 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 Example
自动显示信息窗口 JsFiddle Demo

关于javascript - 如何在标记谷歌地图中添加地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36685170/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com