0
我想繪製谷歌地圖上的標記列表。我使用Python生成一些Javascript代碼。我讀了這個:google map api: open url by clicking at marker並嘗試調整我的代碼,但它仍然沒有工作:我能夠繪製的所有圖形都是第一個製造商,而且它看起來並不像它正在工作。將網址添加到Google地圖標記?
這是我從那時起生產的 - 我儘量保持簡單。
from __future__ import print_function
class Mappy(object):
def __init__(self,listCord=[]):
self.listCord = listCord
def __str__(self):
initLat = sum((x[0] for x in self.listCord))/len(self.listCord)
initLon = sum((x[1] for x in self.listCord))/len(self.listCord)
markersCode = "\n".join(
[ """new google.maps.Marker({
position: new google.maps.LatLng(%s,%s),
map: map,
title : '%s',
url : '%s'
});
google.maps.event.addListener(marker, 'click', function() {
window.location.href = this.url;
});
"""%(x[0], x[1],x[2],x[2])
for x in self.listCord])
print (markersCode)
return """
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<div id="map-canvas" style="height: 100%; width: 100%"></div>
<script type="text/javascript">
var map;
function show_map() {{
map = new google.maps.Map(document.getElementById("map-canvas"), {{
zoom: 8,
center: new google.maps.LatLng({centerLat}, {centerLon})
}});
{markersCode}
}}
google.maps.event.addDomListener(window, 'load', show_map);
</script>
""".format(centerLat=initLat, centerLon=initLon,
markersCode=markersCode)
現在該類謂會產生標記建立的名單,這是我的主:
if __name__ == "__main__":
listCord = [[51.5248,-0.133427,'Old Trafford','http://www.manutd.com'],
[51.5145,-0.157687,'Stamford Bridge','http://www.chelseafc.com'],
[51.5264,-0.13892,'Anfield','http://www.liverpoolfc.com']]
mapper = Mappy(listCord)
with open('stadium.html','w') as out :
print(mapper,file=out)
這將返回在那裏我suposed看到我景點HTML文件與他們的鏈接。但它不起作用。我不得不說,如果我不把:
google.maps.event.addListener(marker, 'click', function() {
window.location.href = this.url;
});
我可以看到我的地圖上的所有地方與他們的標題。
在此先感謝您的幫助