MVC谷歌地图折线

时间:2015-06-30 19:59:31

标签: asp.net-mvc google-maps razor model-view-controller google-polyline

我正在尝试创建一个Google地图,它将从lat&列表中抽出一个赛道。在我的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>

谢谢!

2 个答案:

答案 0 :(得分:0)

对于path中的flightPath Polyline,请为其提供一系列积分,而不是对函数的引用。哪个应该是这样的:

  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来平滑折线。

答案 1 :(得分:0)

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

                                        <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>