1

我正在尝试用 cakephp 实现谷歌地图。我希望html->link在我的视图中点击打开谷歌地图。以下是我的代码:

<?php echo $this->Html->link($this->Html->image("../images/route.png", array("alt" => "altText")),
                     array('controller' => 'ctrllerName', 'action' => 'actionName'),
                    array('escape' => false)); ?>

我需要在链接点击时调用的功能:

 <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAgrj58PbXr2YriiRDqbnL1RSqrCjdkglBijPNIIYrqkVvD1R4QxRl47Yh2D_0C1l5KXQJGrbkSDvXFA"
      type="text/javascript"></script>
    <script type="text/javascript">

 function load() {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
        var center = new GLatLng(21.19500,  72.81944);
        map.setCenter(center, 15);
        geocoder = new GClientGeocoder();
        var marker = new GMarker(center, {draggable: true});  
        map.addOverlay(marker);
        document.getElementById("lat").innerHTML = center.lat().toFixed(5);
        document.getElementById("lng").innerHTML = center.lng().toFixed(5);

      GEvent.addListener(marker, "dragend", function() {
       var point = marker.getPoint();
          map.panTo(point);
       document.getElementById("lat").innerHTML = point.lat().toFixed(5);
       document.getElementById("lng").innerHTML = point.lng().toFixed(5);

        });


     GEvent.addListener(map, "moveend", function() {
          map.clearOverlays();
    var center = map.getCenter();
          var marker = new GMarker(center, {draggable: true});
          map.addOverlay(marker);
          document.getElementById("lat").innerHTML = center.lat().toFixed(5);
       document.getElementById("lng").innerHTML = center.lng().toFixed(5);


     GEvent.addListener(marker, "dragend", function() {
      var point =marker.getPoint();
         map.panTo(point);
      document.getElementById("lat").innerHTML = point.lat().toFixed(5);
         document.getElementById("lng").innerHTML = point.lng().toFixed(5);

        });

        });

      }
    }
</script>  

如何在 cakephp 中单击链接时调用此函数以显示谷歌地图?

4

1 回答 1

0

假设您的目标是在用户单击链接时调用 load() 方法,代码如下:

<?php echo $this->Html->link(
     $this->Html->image(../images/route.php),
     array('controller' =>'ctrllerName', 'action' => 'actionName'),
     array('escape' => false, 'onclick' => 'load()')
); ?>

但是,我假设如果您想在页面上调用一段 JavaScript,您可能不想转到其他页面,在这种情况下,您可以将代码更改为以下内容:

<?php echo $this->Html->link(
     $this->Html->image(../images/route.php),
     '#',
     array('escape' => false, 'onclick' => 'load()')
); ?>
于 2013-04-13T15:07:10.107 回答