【问题标题】:Ad a url to a google map marker?将网址添加到谷歌地图标记?
【发布时间】:2015-09-12 09:51:12
【问题描述】:

我想在谷歌地图上绘制一个标记列表。我使用 Python 生成了一些 Javascript 代码。我读到这个:google map api: open url by clicking at marker 并尝试调整我的代码,但它仍然不起作用:我能够绘制的只是第一个制造商,而且 url 甚至看起来都不起作用。

这是我从那时起制作的内容 - 我试图让它尽可能简单。

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)

这将返回一个 HTML 文件,我可以在其中看到我感兴趣的地方及其链接。但它不起作用。我不得不说,如果我不把:

google.maps.event.addListener(marker, 'click',   function() {
        window.location.href = this.url;
        });

然后我可以在我的地图上看到所有带有标题的地方。

提前感谢您的帮助

【问题讨论】:

    标签: javascript python-2.7 url google-maps-markers


    【解决方案1】:

    看起来您只需更改一行代码即可使其正常工作!

    尝试使用window.open(marker.url);而不是window.location.href = marker.url;

    查看示例here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 2018-10-11
      • 1970-01-01
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 2014-12-08
      相关资源
      最近更新 更多