【问题标题】:Meteor: Uncaught ReferenceError: calcRoute is not definedMeteor: Uncaught ReferenceError: calcRoute is not defined
【发布时间】:2015-09-17 21:56:10
【问题描述】:

我是 Meteor 和 Javascript 的新手。我正在创建一张地图,它提供从您当前位置到地图上标记的方向。除了我似乎无法正确调用 calcRoute() 函数之外,一切似乎都正常。或者它可能是在错误的地方定义的。

我想我需要一些关于模板助手的学习。请告诉我哪里出错了。谢谢。

    var gmaps = {

    // map object
    map: null,

    //direction services object
    directionsService: null,

    //direction services object
    directionsDisplay: null,

    //direction services object
    stepDisplay: null,

    markerArray: []
} 
Template.map.helpers({
    mapOptions: function() {
        if (GoogleMaps.loaded()) {

            if (!Geolocation.error()) {                
                pos = Geolocation.latLng();
            } 
            return {
                //center: new google.maps.LatLng(-25.2743, 133.7751),
                center: new google.maps.LatLng(pos.lat, pos.lng),
                zoom: 12,
                scaleControl: false,                
                zoomControl: false,
                mapTypeControl: false,
                panControl: false,
                rotateControl: true,
                overviewMapControl: false, 
                streetViewControl: false,


            };
        }
    },

     calcRoute: function() {


        //clear markers before calculating function   
        gmaps.clearMarkers();    

        console.log(this.markerArray);
        // Retrieve the start and end locations and create
        // a DirectionsRequest using BICYCLING directions.
        var start = marker3.getPosition(); 
        var end = document.getElementById('marketName').value;
        var request = {
            origin: start,
            destination: end,
            travelMode: google.maps.TravelMode.BICYCLING
        };

        // Route the directions and pass the response to a
        // function to create markers for each step.
        this.directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
            var warnings = document.getElementById('warnings_panel');
            warnings.innerHTML = '<b>' + response.routes[0].warnings + '</b>';
            this.directionsDisplay.setDirections(response);
            gmaps.showSteps(response);
            }
        });
        },

    showSteps: function(directionResult) {
        // For each step, place a marker, and add the text to the marker's
        // info window. Also attach the marker to an array so we
        // can keep track of it and remove it when calculating new
        // routes.
        var myRoute = directionResult.routes[0].legs[0];

        for (var i = 0; i < myRoute.steps.length; i++) {
            var marker = new google.maps.Marker({
                position: myRoute.steps[i].start_location,
                map: map.instance
            });
            gmaps.attachInstructionText(marker, myRoute.steps[i].instructions);
            this.markerArray[i] = marker;
        }
        },

         attachInstructionText: function(marker, text) {

        // Instantiate an info window to hold step text.
        var stepDisplay = new google.maps.InfoWindow();
            google.maps.event.addListener(marker, 'mouseover', function() {
            // Open an info window when the marker is clicked on,
            // containing the text of the step.
            stepDisplay.setContent(text);
            stepDisplay.open(map.instance, marker);

        })

            google.maps.event.addListener(marker, 'click', function() {
                map.instance.setZoom(14);
                map.instance.setCenter(marker.getPosition());
                stepDisplay.open(map.instance, marker);
            })

        },

        clearMarkers: function() {

              // First, remove any existing markers from the map.
        for (var i = 0; i < this.markerArray.length; i++) {

            this.markerArray[i].setMap(null);
        }

        // Now, clear the array itself.
        this.markerArray = [];

        }



});

Template.map.onCreated(function() {
    GoogleMaps.ready('map', function(map) {



        var bikeLayer = new google.maps.BicyclingLayer();
        bikeLayer.setMap(map.instance);



        var marker1 = new google.maps.Marker({
            position: new google.maps.LatLng(29.71739, -95.40183),
            map: map.instance,
            title: 'Rice U Farmers Market'            
        });        
        var infowindow1 = new google.maps.InfoWindow({
              content: ''
          });

        google.maps.event.addListener(marker1, 'click', function() {
            infowindow1.setContent( '<p>Farmers Market at Rice U </p>' +'<button onclick="Meteor.call(calcRoute());">Directions from my Location</button>');
            infowindow1.open(map.instance, marker1);
        });

        var marker2 = new google.maps.Marker({
            position: new google.maps.LatLng(29.81063, -95.37999),
            map: map.instance,
            title: 'Canino\'s Produce'            
        });
        var infowindow2 = new google.maps.InfoWindow({
              content: 'Canino\'s Produce'
          });

        google.maps.event.addListener(marker2, 'click', function() {
            infowindow2.open(map.instance, marker2);
        });

        var image = '/img/app/flag1.png'
        var marker3 = new google.maps.Marker({
            position: new google.maps.LatLng(pos.lat, pos.lng),
            map: map.instance,
            title: 'You are here',
            icon: image
        });

        var rendererOptions = {
            map: map.instance
        }

        this.directionsService = new google.maps.DirectionsService();

        directionsDisplay = new                                                         google.maps.DirectionsRenderer(rendererOptions);

        // global flag saying we intialized already
        Session.set('map', true);
    })


});

【问题讨论】:

  • 你的 calcRoute 函数是在哪里定义的?从其中的代码看来,它可能在客户端上,在这种情况下,不需要 Meteor.call()。您只需要将它与调用者放在同一个文件中,然后直接以calcRoute() 调用它,或者您可以创建一个全局函数。
  • 它在 Template.map.helpers({}) 中定义,并在 Template.map.onCreated({}) 中调用。我以为我在正确的地方定义了它,但很可能我没有。我还能在哪里定义它?

标签: javascript google-maps meteor meteor-helper


【解决方案1】:

您必须将 Meteor 将调用的方法的名称作为字符串传递;

替换:

'<p>Farmers Market at Rice U </p>' +'<button onclick="Meteor.call(calcRoute());">Directions from my Location</button>');
        infowindow1.open(map.instance, marker1);

与:

'<p>Farmers Market at Rice U </p>' +'<button onclick="Meteor.call(\'calcRoute\');">Directions from my Location</button>');
        infowindow1.open(map.instance, marker1);

【讨论】:

  • 我收到了这个错误:错误调用方法'calcRoute':找不到方法[404]....所以我猜我在错误的地方定义了它。它现在在 Template.map.helper({}) 函数中
  • 啊,你是对的。将 calcRoute 的定义移到 Template.map.methods 中,它应该可以工作。
  • “Template.map.methods 不是函数”是我在控制台中得到的:/
  • 对不起。我的错。实际上方法是直接添加到 Meteor 中的。使用 Meteor.methods({calcRoute: function(){}})
猜你喜欢
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 2019-01-22
  • 2019-12-31
  • 2020-05-24
  • 2020-05-10
相关资源
最近更新 更多