MVC Google 地图折线

MVC Google Map Polylines

我正在尝试创建一个 Google 地图,该地图将从我的 MVC 模型中的经纬度列表中绘制出赛道。我已经使用标记成功地完成了这项工作,但我需要使用多段线来创建课程路径。以下是我所拥有但无法显示的行。

有什么建议吗?

                                        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=false"></script>
                                    <script>

                                        function initialize() {
                                            var mapOptions = {
                                                zoom: 10,
                                                center: new google.maps.LatLng(@Model.Courses.Latitude, @Model.Courses.Longitude),
                                                mapTypeId: google.maps.MapTypeId.TERRAIN
                                            };

                                            var map = new google.maps.Map(document.getElementById('map-canvas'),
                                                mapOptions);

                                            @foreach (var item in Model.CourseRatings.ValuesList)
                                            {
                                                <text>addMarker(@item.Item2, @item.Item3)</text>
                                            }

                                                var flightPath = new google.maps.Polyline({
                                                    path: function addMarker(x, y) { new google.maps.LatLng(x, y); },
                                                    geodesic: true,
                                                    strokeColor: '#FF0000',
                                                    strokeOpacity: 1.0,
                                                    strokeWeight: 2
                                                });
                                                flightPath.setMap(map);

                                        }

                                        google.maps.event.addDomListener(window, 'load', initialize);

                                    </script>

谢谢!

对于 flightPath Polyline 中的 path,为其提供点数组,而不是对函数的引用。应该看起来像这样:

  var flightPlanCoordinates = [
    new google.maps.LatLng(37.772323, -122.214897),
    new google.maps.LatLng(21.291982, -157.821856),
    new google.maps.LatLng(-18.142599, 178.431),
    new google.maps.LatLng(-27.46758, 153.027892)
  ];
  var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    geodesic: true,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2
  });

还可以考虑使用 snap to road api 来平滑折线。

这对我有用,但我要尝试你提到的平滑:

                                        <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=false"></script>
                                    <script>

                                        function initialize() {
                                            var mapOptions = {
                                                zoom: 10,
                                                center: new google.maps.LatLng(@Model.Courses.Latitude, @Model.Courses.Longitude),
                                                mapTypeId: google.maps.MapTypeId.TERRAIN
                                            };

                                            var map = new google.maps.Map(document.getElementById('map-canvas'),
                                                mapOptions);

                                            var flightPathCoordinates =
                                            [
                                                @foreach (var item in Model.CourseRatings.ValuesList)
                                                {
                                                    <text>new google.maps.LatLng(@item.Item2, @item.Item3),</text>

                                                }
                                            ];

                                            var flightPath = new google.maps.Polyline({
                                                path: flightPathCoordinates,
                                                geodesic: true,
                                                strokeColor: '#FF0000',
                                                strokeOpacity: 1.0,
                                                strokeWeight: 2
                                            });
                                            flightPath.setMap(map);

                                        }

                                        google.maps.event.addDomListener(window, 'load', initialize);

                                   </script>