Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/429.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 向路径的起点和终点添加标记Google Maps API v3_Javascript_Html_Google Maps_Google Maps Api 3 - Fatal编程技术网

Javascript 向路径的起点和终点添加标记Google Maps API v3

Javascript 向路径的起点和终点添加标记Google Maps API v3,javascript,html,google-maps,google-maps-api-3,Javascript,Html,Google Maps,Google Maps Api 3,我正试图在谷歌地图上实现一些自定义方向服务。我有一堆预先制作的路径,根据从两个下拉框中的选择隐藏和显示。到目前为止效果很好。但是,我想在路径的起点和终点添加标记,我该如何做呢 这是一个工作样本 html{ 身高:100% } 身体{ 身高:100%; 保证金:0; 填充:0 } #地图画布{ 身高:100% } 开始: 1号楼 2号楼 3号楼 4号楼 5号楼 6号楼 7号楼 8号楼 结束: 1号楼 2号楼 3号楼 4号楼 5号楼 6号楼 7号楼 8号楼 变量映射选项={ mapTypeId:

我正试图在谷歌地图上实现一些自定义方向服务。我有一堆预先制作的路径,根据从两个下拉框中的选择隐藏和显示。到目前为止效果很好。但是,我想在路径的起点和终点添加标记,我该如何做呢

这是一个工作样本


html{
身高:100%
}
身体{
身高:100%;
保证金:0;
填充:0
}
#地图画布{
身高:100%
}
开始:
1号楼
2号楼
3号楼
4号楼
5号楼
6号楼
7号楼
8号楼
结束:
1号楼
2号楼
3号楼
4号楼
5号楼
6号楼
7号楼
8号楼
变量映射选项={
mapTypeId:google.maps.mapTypeId.ROADMAP,
中心:新google.maps.LatLng(51.507684,0.063686),
缩放:17
};
var map=new google.maps.map(document.getElementById(“map”)、mapOptions);
//预定义所有路径
var路径=[];
路径['1_到_2']=新的google.maps.Polyline({
路径:[
新的google.maps.LatLng(51.508445,0.063944),
新的google.maps.LatLng(51.507474,0.063877),
新google.maps.LatLng(51.507457,0.064019)],
strokeColor:“#FF0000”
});
路径['1_到_3']=新的google.maps.Polyline({
路径:[
新的google.maps.LatLng(51.508445,0.063944),
新的google.maps.LatLng(51.507474,0.063877),
新的google.maps.LatLng(51.507494,0.063399)],
strokeColor:“#FF0000”
});
路径['1_到_4']=新的google.maps.Polyline({
路径:[
新的google.maps.LatLng(51.508445,0.063944),
新的google.maps.LatLng(51.507474,0.063877),
新google.maps.LatLng(51.507709,0.063301)],
strokeColor:“#FF0000”
});
路径['1_至_5']=新的google.maps.Polyline({
路径:[
新的google.maps.LatLng(51.508445,0.063944),
新的google.maps.LatLng(51.507474,0.063877),
新的google.maps.LatLng(51.507287,0.063839),
新的google.maps.LatLng(51.507293,0.062681),
新的google.maps.LatLng(51.50747,0.062659),
新google.maps.LatLng(51.507501,0.062467)],
strokeColor:“#FF0000”
});
路径['1_至_6']=新的google.maps.Polyline({
路径:[
新的google.maps.LatLng(51.508445,0.063944),
新的google.maps.LatLng(51.507474,0.063877),
新的google.maps.LatLng(51.507287,0.063839),
新的google.maps.LatLng(51.507293,0.062681),
新google.maps.LatLng(51.507275,0.062455)],
strokeColor:“#FF0000”
});
路径['1_至_7']=新的google.maps.Polyline({
路径:[
新的google.maps.LatLng(51.508445,0.063944),
新的google.maps.LatLng(51.507474,0.063877),
新的google.maps.LatLng(51.507287,0.063839),
新的google.maps.LatLng(51.507293,0.062681),
新google.maps.LatLng(51.507275,0.062455)],
strokeColor:“#FF0000”
});
路径['1_至_8']=新的google.maps.Polyline({
路径:[
新的google.maps.LatLng(51.508445,0.063944),
新的google.maps.LatLng(51.507474,0.063877),
新的google.maps.LatLng(51.507287,0.063839),
新的google.maps.LatLng(51.507248,0.065797),
新的google.maps.LatLng(51.507449,0.065805),
新google.maps.LatLng(51.507442,0.066004)],
strokeColor:“#FF0000”
});
//---------------------------------------------------------
路径['2_至_3']=新的google.maps.Polyline({
路径:[
新的google.maps.LatLng(51.507457,0.064019),
新的google.maps.LatLng(51.507474,0.063877),
新的google.maps.LatLng(51.507494,0.063399)],
strokeColor:“#FF0000”
});
路径['2_至_4']=新的google.maps.Polyline({
路径:[
新的google.maps.LatLng(51.507457,0.064019),
新的google.maps.LatLng(51.507474,0.063877),
新google.maps.LatLng(51.507709,0.063301)],
strokeColor:“#FF0000”
});
路径['2_至_5']=新的google.maps.Polyline({
路径:[
新的google.maps.LatLng(51.507457,0.064019),
新的google.maps.LatLng(51.507474,0.063877),
新的google.maps.LatLng(51.507287,0.063839),
新的google.maps.LatLng(51.507293,0.062681),
新的google.maps.LatLng(51.50747,0.062659),
新google.maps.LatLng(51.507501,0.062467)],
strokeColor:“#FF0000”
});
路径['2_至_6']=新的google.maps.Polyline({
路径:[
新的google.maps.LatLng(51.507457,0.064019),
新的google.maps.LatLng(51.507474,0.063877),
新google.maps.LatLng(51.507287,0.063839)
<!DOCTYPE html>
<html>

    <head>
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <style type="text/css">
            html {
                height: 100%
            }
            body {
                height: 100%;
                margin: 0;
                padding: 0
            }
            #map_canvas {
                height: 100%
            }
        </style>
        <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyD9W-wEHa5KzvcntFzGgyTwWtx4wvrs0os&sensor=true">

        </script>
    </head>

    <body>
        <div><strong>Start: </strong>

            <select id="start">
                <option>Building 1</option>
                <option>Building 2</option>
                <option>Building 3</option>
                <option>Building 4</option>
                <option>Building 5</option>
                <option>Building 6</option>
                <option>Building 7</option>
                <option>Building 8</option>
            </select> <strong>End:</strong> 
            <select id="end">
                <option>Building 1</option>
                <option>Building 2</option>
                <option>Building 3</option>
                <option>Building 4</option>
                <option>Building 5</option>
                <option>Building 6</option>
                <option>Building 7</option>
                <option>Building 8</option>
            </select>
            <input type="button" onclick="drawDirections();" value="GO" />
        </div>
        <div id="map" style="width: 100%; height: 100%"></div>
        <script type="text/javascript">
            var mapOptions = {
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                center: new google.maps.LatLng(51.507684, 0.063686),
                zoom: 17
            };
            var map = new google.maps.Map(document.getElementById("map"), mapOptions);

            // Predefine all the paths
            var paths = [];

            paths['1_to_2'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.508445, 0.063944),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507457, 0.064019)],
                strokeColor: '#FF0000'
            });
            paths['1_to_3'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.508445, 0.063944),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507494, 0.063399)],
                strokeColor: '#FF0000'
            });
            paths['1_to_4'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.508445, 0.063944),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507709, 0.063301)],
                strokeColor: '#FF0000'
            });
            paths['1_to_5'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.508445, 0.063944),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.50747, 0.062659),
                new google.maps.LatLng(51.507501, 0.062467)],
                strokeColor: '#FF0000'
            });
            paths['1_to_6'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.508445, 0.063944),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507275, 0.062455)],
                strokeColor: '#FF0000'
            });
            paths['1_to_7'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.508445, 0.063944),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507275, 0.062455)],
                strokeColor: '#FF0000'
            });
            paths['1_to_8'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.508445, 0.063944),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507248, 0.065797),
                new google.maps.LatLng(51.507449, 0.065805),
                new google.maps.LatLng(51.507442, 0.066004)],
                strokeColor: '#FF0000'
            });

            //---------------------------------------------------------
            paths['2_to_3'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507457, 0.064019),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507494, 0.063399)],
                strokeColor: '#FF0000'
            });
            paths['2_to_4'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507457, 0.064019),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507709, 0.063301)],
                strokeColor: '#FF0000'
            });
            paths['2_to_5'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507457, 0.064019),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.50747, 0.062659),
                new google.maps.LatLng(51.507501, 0.062467)],
                strokeColor: '#FF0000'
            });
            paths['2_to_6'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507457, 0.064019),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507275, 0.062455)],
                strokeColor: '#FF0000'
            });
            paths['2_to_7'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507457, 0.064019),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507275, 0.062455)],
                strokeColor: '#FF0000'
            });
            paths['2_to_8'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507457, 0.064019),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507248, 0.065797),
                new google.maps.LatLng(51.507449, 0.065805),
                new google.maps.LatLng(51.507442, 0.066004)],
                strokeColor: '#FF0000'
            });

            //---------------------------------------------------------
            paths['3_to_4'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507494, 0.063399),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507709, 0.063301)],
                strokeColor: '#FF0000'
            });
            paths['3_to_5'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507494, 0.063399),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.50747, 0.062659),
                new google.maps.LatLng(51.507501, 0.062467)],
                strokeColor: '#FF0000'
            });
            paths['3_to_6'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507494, 0.063399),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507275, 0.062455)],
                strokeColor: '#FF0000'
            });
            paths['3_to_7'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507494, 0.063399),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507275, 0.062455)],
                strokeColor: '#FF0000'
            });
            paths['3_to_8'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507494, 0.063399),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507248, 0.065797),
                new google.maps.LatLng(51.507449, 0.065805),
                new google.maps.LatLng(51.507442, 0.066004)],
                strokeColor: '#FF0000'
            });

            //-------------------------------------------------------------
            paths['4_to_5'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507709, 0.063301),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.50747, 0.062659),
                new google.maps.LatLng(51.507501, 0.062467)],
                strokeColor: '#FF0000'
            });
            paths['4_to_6'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507709, 0.063301),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507275, 0.062455)],
                strokeColor: '#FF0000'
            });
            paths['4_to_7'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507709, 0.063301),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507275, 0.062455)],
                strokeColor: '#FF0000'
            });
            paths['4_to_8'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507709, 0.063301),
                new google.maps.LatLng(51.507474, 0.063877),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507248, 0.065797),
                new google.maps.LatLng(51.507449, 0.065805),
                new google.maps.LatLng(51.507442, 0.066004)],
                strokeColor: '#FF0000'
            });

            //---------------------------------------------------------------
            paths['5_to_6'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507501, 0.062467),
                new google.maps.LatLng(51.50747, 0.062659),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507275, 0.062455)],
                strokeColor: '#FF0000'
            });
            paths['5_to_7'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507501, 0.062467),
                new google.maps.LatLng(51.50747, 0.062659),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507275, 0.062455)],
                strokeColor: '#FF0000'
            });
            paths['5_to_8'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507501, 0.062467),
                new google.maps.LatLng(51.50747, 0.062659),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507248, 0.065797),
                new google.maps.LatLng(51.507449, 0.065805),
                new google.maps.LatLng(51.507442, 0.066004)],
                strokeColor: '#FF0000'
            });

            //----------------------------------------------------
            paths['6_to_7'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507275, 0.062455),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507275, 0.062455)],
                strokeColor: '#FF0000'
            });
            paths['6_to_8'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507275, 0.062455),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507248, 0.065797),
                new google.maps.LatLng(51.507449, 0.065805),
                new google.maps.LatLng(51.507442, 0.066004)],
                strokeColor: '#FF0000'
            });

            //--------------------------------------------------------
            paths['7_to_8'] = new google.maps.Polyline({
                path: [
                new google.maps.LatLng(51.507275, 0.062455),
                new google.maps.LatLng(51.507293, 0.062681),
                new google.maps.LatLng(51.507287, 0.063839),
                new google.maps.LatLng(51.507248, 0.065797),
                new google.maps.LatLng(51.507449, 0.065805),
                new google.maps.LatLng(51.507442, 0.066004)],
                strokeColor: '#FF0000'
            });

            function drawDirections() {
                var start = 1 + document.getElementById('start').selectedIndex;
                var end = 1 + document.getElementById('end').selectedIndex;
                var i;

                if (start === end) {
                    alert('Please choose different buildings');
                } else {
                    // Hide all polylines
                    for (i in paths) {
                        paths[i].setOptions({
                            map: null
                        });
                    }

                    // Show the route
                    if (typeof paths['' + start + '_to_' + end] !== 'undefined') {
                        paths['' + start + '_to_' + end].setOptions({
                            map: map
                        });
                    } else if (typeof paths['' + end + '_to_' + start] !== 'undefined') {
                        paths['' + end + '_to_' + start].setOptions({
                            map: map
                        });
                    }

                    var laLatLng = new google.maps.LatLng(51.507684, 0.063686);
                    map.panTo(laLatLng);
                    map.setZoom(17);
                    //panTo(latLng:LatLng)
                }
            }
        </script>
    </body>

</html>
  paths['' + start + '_to_' + end].setOptions({
      map: map
  });
  // first point of polyline
  createMarker(paths['' + start + '_to_' + end].getPath().getAt(0), 'start', 'start', 'green'); 
  // last point of polyline
  createMarker(paths['' + start + '_to_' + end].getPath().getAt(paths['' + start + '_to_' + end].getPath().getLength()-1), 'end', 'end', 'red');