打开谷歌地图上的html->链接点击cakephp通过javascript

时间:2013-02-13 10:44:18

标签: cakephp google-maps-api-3

我正在尝试用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调用此功能,以便显示谷歌地图?

1 个答案:

答案 0 :(得分:0)

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

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

但是,我假设如果你想在页面上调用一段JavaScript,你可能不想去另一个页面,在这种情况下你可以将代码更改为以下内容: / p>

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