简体   繁体   English

调用jquery中定义的javascript方法

[英]call javascript method defined inside jquery

I am trying to draw a circle on google maps-v3. 我想在谷歌地图-v3上绘制一个圆圈。 Unable to call oldDrawHandler() method onClick event from html. 无法从html调用oldDrawHandler()方法onClick事件。 How to Call a javascript method on onclick event, defined inside a jquery();. 如何在onclick事件上调用javascript方法,在jquery();中定义。 Please help!!! 请帮忙!!!

<head>
<script type="text/javascript">
 var init = function() {

 $.getJSON('web_services/latlong.php?option=2&id='+id, function(json) {

           $.each(json.result,function(i,gmap){
    latitude=gmap.latitude;
    longitude=gmap.longitude;
        var image = 'vw-beetle-icon.png'
            var latlng= new google.maps.LatLng(latitude, longitude)
            var opts = {
                zoom: 16,
                center:latlng , // London
                mapTypeId: google.maps.MapTypeId.ROADMAP,
        icon: image
                       };
             var map = new google.maps.Map(document.getElementById('map'), opts);
         var Marker = new google.maps.Marker({
                      position: latlng,
                      map: map,
                      icon: image
                          });


        var getPoints = function(lat, lng, radius, earth){
            // Returns an array of GLatLng instances representing the points of the   radius circle
            var lat = (lat * Math.PI) / 180; //rad

            var lon = (lng * Math.PI) / 180; //rad
            var d = parseFloat(radius) / earth; // d = angular distance covered on earth's surface
            var points = [];
            for (x = 0; x <= 360; x++) 
            { 
                brng = x * Math.PI / 180; //rad

                var destLat = Math.asin(Math.sin(lat)*Math.cos(d) + Math.cos(lat)*Math.sin(d)*Math.cos(brng));
                var destLng = ((lon + Math.atan2(Math.sin(brng)*Math.sin(d)*Math.cos(lat), Math.cos(d)-Math.sin(lat)*Math.sin(destLat))) * 180) / Math.PI;
                destLat = (destLat * 180) / Math.PI;
                points.push(new google.maps.LatLng(destLat, destLng));
            }
            return points;
        }

        var polygonDestructionHandler = function() {
           this.setMap(null);
    //marker.setMap(null);
        }

        var polygonDrawHandler = function(e) {
            // Get the desired radius + units
            var select = document.getElementById('unitSelector');
            var unitKey = select.getElementsByTagName('option')[select.selectedIndex].value;

            var earth = earthRadiuses[unitKey];
            var radius = parseFloat(document.getElementById('radiusInput').value);
            // Draw the polygon
            var points = getPoints(e.latLng.lat(), e.latLng.lng(), radius, earth);
    //alert(e.latLng.lat());

            var polygon = new google.maps.Polygon({
                paths: points,
                strokeColor: '#004de8',
                strokeWeight: 1,
                strokeOpacity: 0.62,
                fillColor: '#004de8',
                fillOpacity: 0.27,
                geodesic: true,
                map: map
            });
    //alert(radius);

            google.maps.event.addListener(polygon, 'rightclick', polygonDestructionHandler);

        }


    var oldDrawHandler = function() {
    [select.selectedIndex].value;
    var unitKey = 'mt';
            var earth = earthRadiuses[unitKey];

    var radius = 2000;
            lt=13.0497548596428;
        ln=77.6202746243287;
            var points = getPoints(lt, ln, radius, earth);
    //alert(e.latLng.lat());

            var polygon = new google.maps.Polygon({
                paths: points,
                strokeColor: '#004de8',
                strokeWeight: 1,
                strokeOpacity: 0.62,
                fillColor: '#004de8',
                fillOpacity: 0.27,
                geodesic: true,
                map: map
            });



    google.maps.event.addListener(polygon, 'rightclick', polygonDestructionHandler);


        }



});

});


    }/* end of init()*/

</script>
 </head>

<body onload="init()">

            <a href="javascript:void(0)" onclick="oldDrawHandler();">GeoFence</a>
</body>

Why do you put that function into the var init() ? 为什么要将该函数放入var init()?
You could run the same code by using jQuery and execute it when the DOM is loaded which would be faster ! 您可以使用jQuery运行相同的代码,并在加载DOM时执行它,这将更快!

$(document).ready(function() {
   // put all your jQuery goodness in here.
});

I think you want this: I added the click to the link inside the ready 我想你想要这个:我将点击添加到准备好的链接里面

<html>
<head>
<script type="text/javascript">
 $(document).ready(function() {
   $.getJSON('web_services/latlong.php?option=2&id='+id, function(json) {
     $.each(json.result,function(i,gmap){
       latitude=gmap.latitude;
       longitude=gmap.longitude;
       var image = 'vw-beetle-icon.png';
       var latlng= new google.maps.LatLng(latitude, longitude);
       var opts = {
         zoom: 16,
         center:latlng , // London
         mapTypeId: google.maps.MapTypeId.ROADMAP,
         icon: image
       };
       var map = new google.maps.Map(document.getElementById('map'), opts);
       var Marker = new google.maps.Marker({
         position: latlng,
         map: map,
         icon: image
       });
       var getPoints = function(lat, lng, radius, earth){
         // Returns an array of GLatLng instances representing the points of the   radius circle
         var lat = (lat * Math.PI) / 180; //rad
         var lon = (lng * Math.PI) / 180; //rad
         var d = parseFloat(radius) / earth; // d = angular distance covered on earth's surface
         var points = [];
         for (x = 0; x <= 360; x++) { 
           brng = x * Math.PI / 180; //rad
           var destLat = Math.asin(Math.sin(lat)*Math.cos(d) + Math.cos(lat)*Math.sin(d)*Math.cos(brng));
           var destLng = ((lon + Math.atan2(Math.sin(brng)*Math.sin(d)*Math.cos(lat), Math.cos(d)-Math.sin(lat)*Math.sin(destLat))) * 180) / Math.PI;
           destLat = (destLat * 180) / Math.PI;
           points.push(new google.maps.LatLng(destLat, destLng));
         }
         return points;
       }
     }); // each
   }); // getJSON

   var polygonDestructionHandler = function() {
     this.setMap(null);
     //marker.setMap(null);
   }

   var polygonDrawHandler = function(e) {
     // Get the desired radius + units
     var select = document.getElementById('unitSelector');
     var unitKey = select.getElementsByTagName('option')[select.selectedIndex].value;
     var earth = earthRadiuses[unitKey];
     var radius = parseFloat(document.getElementById('radiusInput').value);
     // Draw the polygon
     var points = getPoints(e.latLng.lat(), e.latLng.lng(), radius, earth);
     //alert(e.latLng.lat());
     var polygon = new google.maps.Polygon({
       paths: points,
       strokeColor: '#004de8',
       strokeWeight: 1,
       strokeOpacity: 0.62,
       fillColor: '#004de8',
       fillOpacity: 0.27,
       geodesic: true,
       map: map
     });
     //alert(radius);
    }

    $("#geoFenceLink").click(function() {
      var unitKey = 'mt';
      var earth = earthRadiuses[unitKey];
      var radius = 2000,lt=13.0497548596428,ln=77.6202746243287;
      var points = getPoints(lt, ln, radius, earth);
      //alert(e.latLng.lat());
      var polygon = new google.maps.Polygon({
        paths: points,
        strokeColor: '#004de8',
        strokeWeight: 1,
        strokeOpacity: 0.62,
        fillColor: '#004de8',
        fillOpacity: 0.27,
        geodesic: true,
        map: map
      });
    });
    google.maps.event.addListener(polygon, 'rightclick', polygonDestructionHandler);
 }); // document.ready
</script>
</head>
<body>
  <a id="geoFenceLink" href="#">GeoFence</a>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM