Google maps api 3 替代路线的淘汰点击事件Google地图API

Google maps api 3 替代路线的淘汰点击事件Google地图API,google-maps-api-3,knockout.js,Google Maps Api 3,Knockout.js,任何能帮我的人 我能够显示从源头到目的地的备选路线。现在,我想突出显示我单击的路线,更改其颜色,并在拖动该路线时显示其路径。如果我单击另一条管线,较早的管线应返回其正常颜色,并且所选管线应在拖动时给出其路径。我已经在淘汰赛中实现了这一点 <!DOCTYPE html> <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <met

任何能帮我的人

我能够显示从源头到目的地的备选路线。现在,我想突出显示我单击的路线,更改其颜色,并在拖动该路线时显示其路径。如果我单击另一条管线,较早的管线应返回其正常颜色,并且所选管线应在拖动时给出其路径。我已经在淘汰赛中实现了这一点

<!DOCTYPE html>
<html>
 <head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Directions service</title>
<style>
html, body, #mapCanvas, #RouteSingerMap {
    height: 100%;
    margin: 0px;
    padding: 0px
}
#panel {
    margin: 0px auto;
    width: 980px;
    text-align:center;
    z-index: 5;
    background-color: #fff;
    padding: 10px;
}
.pac-container {
    z-index: 9999;
}
    </style>
   <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places">  </script>
   <script type='text/javascript' src="js/jquery.min.js"></script>
   <script type='text/javascript' src='js/knockout.js'></script>
   <script type='text/javascript' src='js/underscore.js'></script>
   <!--<script type='text/javascript' src='js/knockout.google.maps-0.2.0.debug.js'></script>-->
   <script type='text/javascript' src='js/ko-googlemap.js'></script>
   <script type="text/javascript">



var RouteSinger = ( function (RouteSinger) {

    // RouteSinger Google Map Init method
    RouteSinger.GMap = function(){
        var self = this;
        this.Init = function(container){
            self.gMapInstance = new RouteSinger.Viewmodel();
            ko.applyBindings(self.gMapInstance, $('#'+container)[0]);
            self.gMapInstance.applyData();
        };
    };
    // RouteSinger Google Map ViewModel
    RouteSinger.Viewmodel = function(){
        var that = this;

        this.countries = {
            'in': {
                center: new google.maps.LatLng(22.0, 77.0)
            }
        };

        this.getSource = ko.observable();           
        this.getDestination = ko.observable();          

        this.center = ko.observable(that.countries['in'].center);
        this.panCenter = ko.observable(true);
        this.zoom = ko.observable(8);
        this.mapTypeId = ko.observable(google.maps.MapTypeId.ROADMAP);
        this.bounds = ko.observable();
        this.panBounds = ko.observable(true);   

        this.MapInstance = ko.observable(); 

        // Computed Methods 
        this.onSourceDestinationChanged = ko.computed(function(){

            if(that.getSource()){
                // do something
            }

            if(that.getDestination()){

            }
        });


        // Main Methods
        this.ShowRoutes = function(){
            // do something
            var self = this;

            // Direction Service
            this.directionsService = new google.maps.DirectionsService();
            this.main_route = true;
            this.request = {
                origin: 'Bangalore, Karnataka, India',
                destination: 'Jodhpur, Rajasthan, India',
                provideRouteAlternatives: true, 
                travelMode: google.maps.TravelMode[$('#mode').val()],
                avoidHighways : false,
                avoidTolls: false
            };

            this.RouteSingerMapDirections = ko.observableArray();

            this.directionRenderer = function(result, routeToDisplay, map){
                var self = this;

                this.routeIndex = ko.observable(routeToDisplay);

                this.PolylineOption = {
                    _colour: '',
                    _strokeWeight: '',
                    _strokeOpacity: '',
                    _suppressMarkers: '',       
                    isClickable: true
                };
                this.isDraggable = true;
                this.isClicked = ko.observable(false);

                if(routeToDisplay==0){
                    self.PolylineOption._colour = '#00458E';
                    self.PolylineOption._strokeWeight = 6;
                    self.PolylineOption._strokeOpacity = 1.0;
                    self.PolylineOption._suppressMarkers = false;
                }else{
                    self.PolylineOption._colour = '#ED1C24';
                    self.PolylineOption._strokeWeight = 6;
                    self.PolylineOption._strokeOpacity = 0.7;
                    self.PolylineOption._suppressMarkers = false;
                }

                this.directionsRenderer = new google.maps.DirectionsRenderer({
                    draggable: self.isDraggable, 
                    suppressMarkers: self.PolylineOption._suppressMarkers, 
                    polylineOptions: { 
                        clickable: self.PolylineOption.isClickable,
                        strokeColor: self.PolylineOption._colour, 
                        strokeWeight: self.PolylineOption._strokeWeight, 
                        strokeOpacity: self.PolylineOption._strokeOpacity  
                    }
                });
                this.directionsRenderer.setMap(map);
                this.directionsRenderer.setDirections(result);
                this.directionsRenderer.setRouteIndex(routeToDisplay);

                google.maps.event.addListener(self.PolylineOption, 'click', function() {
                    alert(this.strokeColor);
                });

            };

            self.directionsService.route(self.request, function(response, status) {
                if (status == google.maps.DirectionsStatus.OK) {
                    if(self.main_route){
                        for (var i = 0; i < response.routes.length; i++){
                            console.log(i);
                            self.RouteSingerMapDirections.push(new self.directionRenderer(response, i, that.MapInstance()));
                        }
                    }else{
                        self.RouteSingerMapDirections.push(new self.directionRenderer(response, routeToDisplay, that.MapInstance()));
                    }
                }else{
                    alert("Unable to retrieve your route");
                }
            });

        };

        this.ClearRoutes = function(){
            // do something
        };

        this.applyData = function(){
            //that.rsMap();
        };
    };

    return RouteSinger;
}(RouteSinger || {}));



$(function(){       
    var gMap = new RouteSinger.GMap();      
    gMap.Init('RouteSingerMap');
});
    </script>
   </head>
    <body>
<div id="RouteSingerMap">
    <div id="panel">
        <b>Mode of Travel: </b>
        <select id="mode" onChange="calcRoute()">
            <option value="DRIVING" selected="selected">Driving</option>
            <option value="WALKING">Walking</option>
            <option value="BICYCLING">Bicycling</option>
            <option value="TRANSIT">Transit</option>
        </select>
        <b>Source: </b>
        <input placeholder="Enter Source" type="text" data-bind="Autocomplete: getSource" />
        <b>Destination: </b>
        <input placeholder="Enter Destination" type="text" data-bind="Autocomplete: getDestination" />
        <input type="button" id="Route" value="Show Routes" data-bind="click: ShowRoutes" />
        <input type="button" id="ClearRoute" value="Clear Route" data-bind="click: ClearRoutes" />
    </div>
    <div id="directionsPanel"></div>
    <div id="mapCanvas" data-bind="map: $data"></div>

</div>
  </body>
  </html>

以下是正在工作的JSFIDLE:

@MrUpsidown正在显示备选路线,但每个备选路线的单击事件都不工作。当我点击其中一条路线时。它应该给我它的routeIndex以及它的路径。如果我将drag eventlistener附加到directionRenderer,它总是将routeIndex设置为0。您是否在polylinePath上尝试过该侦听器?它肯定不在PolylineOption上。@MrUpsidown已尝试但不工作您尝试调试的是什么?尝试创建一个JSFIDLE。以这种方式浏览代码并不容易。我只是意识到在方向服务呈现的多段线上没有单击事件。如果要进行单击事件,则需要自己创建多段线。可能有用。