Google地图方向在延长路线后拖动折线坐标重置

时间:2013-05-10 11:45:08

标签: google-maps google-maps-api-3 maps directions

嗨,大家好,这段代码的功能如下所述。

下面有一些预定义的函数,即getMapOption和其他


function initialize(){
    var divCalcDis = $('divCalcDis');
    var pdist = $('pdist');
    var pTimeTaken = $('pTimeTaken');
    var txtLatLon = $('divLatLon');
    var lblDistance = $('lblDistance');
    var mapOption = mapHandler.getMapOption(that.LonLatCoordinates[0], 15, "Default");
    map = mapHandler.getMap('map_canvas', mapOption);
    var renderOption = { draggable: true };
    directionsDisplay = new google.maps.DirectionsRenderer(renderOption);
    directionsDisplay.setMap(map);
    google.maps.event.addListener(directionsDisplay, 'directions_changed', function () {        for (i = 0; i < directionsDisplay.directions.routes.length; i++) {
        //getting latlon
    txtLatLon.innerHTML = "";
                console.log(directionsDisplay.directions.routes[i].overview_path.length);
                var latLng = directionsDisplay.directions.routes[i].overview_path[k];
            var latLng = directionsDisplay.directions.routes[i].overview_path[directionsDisplay.directions.routes[i].overview_path.length - 1].toString();
            latLng = latLng.split('(')[1];
            latLng = latLng.split(')')[0];
            latLng = latLng.split(' ');
            latLng = latLng[0] + latLng[1];
            txtLatLon.innerHTML += latLng;
        }
    });
    startMap();
}

function startMap() {
    var i=0;
    google.maps.event.addListener(map, 'click', function (event) {
        i++;
        if(i === 1){
            mapHandler.setMarker(event.latLng, map, "http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png", null, null, null, that.permanentMarkers, false);
            that.origin = event.latLng;      //comma seperated values of lat,lon
        }
        else if(i > 1){
            mapHandler.setMarker(event.latLng, map, "http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png", null, null, null, that.permanentMarkers, false);
            if (i === 2) {
                that.dest = event.latLng;      //comma seperated values of lat,lon
            }
            else if (i > 2) {
                that.wayPTs.push({
                    location: that.dest,
                    stopover: true
                });
                that.dest = event.latLng;
            }
            that.calcRoute();
        }});
};

function calcRoute() {
    var divCalcDis = stringHandler._id('divCalcDis');
    var pdist = stringHandler._id('pdist');
    var pTimeTaken = stringHandler._id('pTimeTaken');
    var txtLatLon = stringHandler._id('divLatLon');
    txtLatLon.innerHTML = "";
    if (!that.wayPTs.length > 1) {
        this.wayPTs = null;
    }
    var request = this.directionsRequest(this.origin,this.dest,google.maps.DirectionsTravelMode.DRIVING,this.wayPTs,false,true,true,google.maps.DirectionsUnitSystem.METRIC);

    that.directionsResponse.route(request, function (response, status) {
        //console.log(response);
        if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
        }
      });
 };



** 我正在使用Google Maps API V3 Directions库创建一个项目,当用户点击地图上的某个位置时,我正在创建一个路径
这是截图
** Image before dragging


现在,当我拖动方向绘制线时,它可以顺利运行并正确地给我经纬度 这是截图
Image when dragged


但问题是,当我点击地图上的任何其他地方(拖动后)时,航点刷新,我得到旧的没有拖拽路线的下一站,如下所示 这是一个截图


Image after clicking on next stop


如何保存航路点的latLon,以便在创建新积分后可以使用它们.Shx

1 个答案:

答案 0 :(得分:1)

您需要将坐标推入路径数组,以便它们始终可用。因此,当您单击时拖动和推动时按下。可this可能对您有所帮助。祝你好运。