【问题标题】:How do I create a Google Map using v3 API with clickable markers with custom html inside the marker?如何使用带有可点击标记的 v3 API 创建 Google 地图,标记内带有自定义 html?
【发布时间】:2009-11-03 21:21:12
【问题描述】:

我有一个数据库,里面有这些地点的地址和图片。注意:我没有纬度/经度。

我需要做什么:

编写一个函数,使用 Google API v3 在 Google 地图上列出其中一些地址,当您单击标记时,它会显示数据库中的地址和图片。这是一个页面插件,所以我无法编辑标题数据。我只能在显示的地方插入代码。

我已经通读了文档,但似乎所有内容都有很多不必要的代码和我的地理地图不需要的东西。我正在寻找最简单的解决方案,以便以后可以添加。

【问题讨论】:

    标签: php google-maps google-maps-api-3


    【解决方案1】:

    点击我在这里制作的地图上的红色标记:http://www.dougglover.com/samples/UOITMap/index.html

    这就是你要找的东西吗?

    【讨论】:

      【解决方案2】:

      也许你想试试 Gmapper (http://sourceforge.net/projects/gmapper/) 一个很好的 php 类来做谷歌地图。这是一种生成所有 javascript 的简单方法,它还可以查找地址。请注意,Google 限制查找地址的次数,您可能无法在一天内检索到您的数据库。

      【讨论】:

      • 上次更新是一年前。它在 api v3 上运行吗? V3 不需要 API 密钥,也没有 IIRC 限制。
      • 我不确定它是否使用 V3。我确实需要一把钥匙。看来这不是您的解决方案,抱歉。
      • 没问题。我希望其他人可以提供帮助:)
      • V3 看起来和这个工具创建的很相似,也许你可以适应它?
      【解决方案3】:

      我能否将您指向一个我所做的几乎完全一样的站点(除了当您悬停标记而不是单击它时它会更新;只需将代码移动到提供的空单击事件而不是悬停事件中)。本着真正编码的精神,希望你能适应我所做的!

      http://www.primrose-house.co.uk/localattractions

      【讨论】:

      • 单击“单击此处查看地图上的景点”链接 :) 抱歉,我忘记我们已将其更改为默认为非地图行为 :)
      【解决方案4】:
          Here is the full html with google map api v3, markers will be create on dragging the route and then each marker having custom html inside the infowindow.
      
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
      <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=places"></script>
      <script type="text/javascript">
      var rendererOptions = {
        draggable: true,
        suppressInfoWindows: true
        };
      var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
      var directionsService = new google.maps.DirectionsService();
      var infowindow = new google.maps.InfoWindow();
      var map;
      var total;
      var waypoint_markers = []
      
      var myOptions = {
          zoom: 6,
          center: new google.maps.LatLng(46.87916, -3.32910),
          mapTypeId: 'terrain'
      };
      var markers = [];
      
      function init() {
        map = new google.maps.Map(document.getElementById('map'),{'mapTypeId': google.maps.MapTypeId.ROADMAP});
      
        directionsDisplay.setMap(map);
        //directionsDisplay.setPanel($("#directionsPanel")[0]);
      
        google.maps.event.addListener(directionsDisplay, 'directions_changed', function() {
          watch_waypoints();
        });
        calcRoute(false);
      }
      
      function calcRoute(waypoints) {
        var selectedMode = "DRIVING"; //document.getElementById("mode").value;
        var ary;
        if(waypoints) {
          ary = waypoints.map(function(wpt) {return {location: wpt, stopover: false};});
        } else {
          ary = [];
        }
      
        var request = {
          origin: "Seattle, WA",
          destination: "Portland, OR",
          waypoints: ary,
          travelMode: google.maps.TravelMode[selectedMode],
          unitSystem: google.maps.UnitSystem["IMPERIAL"]
        };
        directionsService.route(request, function(response, status) {
          if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
          }
        });
      }
      
      function watch_waypoints() {
        clear_markers();
        var wpts = directionsDisplay.directions.routes[0].legs[0].via_waypoints;
        for(var i=0; i<wpts.length; i++) {
          var marker = new google.maps.Marker({
              map: map,
              //icon: "/images/blue_dot.png",
              position: new google.maps.LatLng(wpts[i].lat(), wpts[i].lng()),
              title: i.toString(),
              draggable :true
              });
          waypoint_markers.push(marker);
          var infowindow = new google.maps.InfoWindow({ 
          content: "<table>" +
          "<tr><td>Waypoint:</td> <td><input type='text' id='name' value=''/> </td> </tr>" +
          "<tr><td>Waypoint Description:</td> <td><input type='text' id='address' value=''/></td> </tr>" +
          "<tr><td><input type='hidden' value='"+marker.getPosition()+"'id='hiddenval'></td></tr>"+
          "<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData(<?php print_r(node_load(arg(1))->nid);?>)'/></td></tr>"
      });
          google.maps.event.addListener(marker, 'click', function() {
              infowindow.open(map,marker);
      
          });
          google.maps.event.addListener(marker, 'dblclick', function() {
              marker.setMap(null);
              wpts.splice(parseInt(this.title), 1);
              calcRoute(wpts);
              directionsDisplay.setOptions({ preserveViewport: true, draggable: true});
          });
        }
      }
      function clear_markers() {
        for(var i=0; i<waypoint_markers.length; i++){
          waypoint_markers[i].setMap(null);
        }
      }
      </script>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
      <title>Untitled Document</title>
      </head>
      
      <body onload="init()">
      <div id="directionsPanel"></div>
      <div id="map"></div>
      </body>
      </html>
      

      【讨论】:

        猜你喜欢
        • 2012-05-09
        • 2012-03-24
        • 1970-01-01
        • 2020-01-22
        • 2011-10-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多