0

我正在加载我的谷歌地图并想显示我的路线。当我将听众放在地图下方时,它工作得很好。

{
                xtype: 'map',
                flex: 1,
                mapOptions: {
                    zoom: 13,
                    mapTypeId: google.maps.MapTypeId.ROADMAP,
                },
                listeners: {
                    maprender : function(comp, map){;
                        var directionsDisplay = new google.maps.DirectionsRenderer();
                        var directionsService = new google.maps.DirectionsService();
                        directionsDisplay.setMap(map);

                        var start = 'New York';
                        var end = 'Chicago';
                        var request = {
                            origin:start, 
                            destination:end,
                            travelMode: google.maps.DirectionsTravelMode.DRIVING
                        };
                        directionsService.route(request, function(response, status) {
                            if (status == google.maps.DirectionsStatus.OK) {
                                directionsDisplay.setDirections(response);
                            }
                        });
                    }
                }

当我将侦听器移动到 updateRecord 以便我可以从我的记录中放置开始和结束时,我的 maprender 侦听器不会启动。

updateRecord: function(newRecord) {
        if (newRecord) {
            var record = newRecord;
            this.down('map').setListeners({
                maprender : function(comp, map){
                    var tripsStore= Ext.getStore('Trips');
                    var directionsDisplay = new google.maps.DirectionsRenderer();
                    var directionsService = new google.maps.DirectionsService();
                    directionsDisplay.setMap(map);
                    //alert("ok pressed");
                    console.log
                    var start = record.data.from;
                    var end = record.data.to;
                    var request = {
                        origin:start, 
                        destination:end,
                        travelMode: google.maps.DirectionsTravelMode.DRIVING
                    };
                    directionsService.route(request, function(response, status) {
                        if (status == google.maps.DirectionsStatus.OK) {
                            directionsDisplay.setDirections(response);
                        }
                    });
                }
            });
        }
    },

有什么想法可以解决这个问题吗?

4

1 回答 1

0

我将整个地图渲染内容添加到更新记录中,现在可以正常工作。

updateRecord: function(newRecord) {
    if (newRecord) {
        this.down('#content').setData(newRecord.data);
        this.down('map').setData(newRecord.data);
    };
    //GET MAP
    var map = this.down('map').getMap();
    //console.log(map);
    var directionsDisplay = new google.maps.DirectionsRenderer();
    var directionsService = new google.maps.DirectionsService();
    //SET map
    directionsDisplay.setMap(map);
    //get data
    var mapData = this.down('map').getData();
    //SET DATA
    var start = mapData.from;
    var end = mapData.to;
    var request = {
        origin:start, 
        destination:end,
        travelMode: google.maps.DirectionsTravelMode.DRIVING,
    };
    directionsService.route(request, function(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            var miles = Ext.getCmp('miles'),
            distance = Math.floor(response.routes[0].legs[0].distance.value / 1000),
            minutes = Math.floor(response.routes[0].legs[0].duration.value / 60);
            miles.setHtml("Distance: " + distance + " kilometers.<br/> Time: " + minutes + " minutes.");
        }
    });
    Ext.device.Geolocation.getCurrentPosition({
        success: function(position) {
            var long = position.coords.longitude,
                lat = position.coords.latitude;
            var marker = new google.maps.Marker({
                position: new google.maps.LatLng(lat,long),
                title : 'Me',
                map: map
            });
        },
        failure: function() {
            console.log('something went wrong!');
        }
    });
},
于 2013-02-14T14:23:07.563 回答