我目前正在使用地图api和asp.net mvc5。 我的控制器将位置数据传递给我的视图,如下所示:
this.context.history
在视图中我正在调用我的js文件并将Viewbags的值传递给它。
ViewBag.Origin = JsonConvert.SerializeObject(origin);
ViewBag.Destination = JsonConvert.SerializeObject(destination);
ViewBag.PositionEntries = JsonConvert.SerializeObject(mapsPositions);
javascript文件:
<script>
var origin = @Html.Raw(ViewBag.Origin)
var destination = @Html.Raw(ViewBag.Destination)
var mapsPositions = @Html.Raw(ViewBag.PositionEntries)
</script>
<script>
initMap(origin, destination, mapsPositions);
</script>
Json-object:
var initMap = (function (origin, destination, mapsPositions) {
var directionsService = new google.maps.DirectionsService;
var directionsDisplay = new google.maps.DirectionsRenderer;
var origin = origin;
var destination = destination;
var mapsPositions = mapsPositions;
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: { lat: 41.85, lng: -87.65 }
});
directionsDisplay.setMap(map);
calculateAndDisplayRoute(directionsService, directionsDisplay, origin, destination, mapsPositions);})
function calculateAndDisplayRoute(directionsService, directionsDisplay, origin, destination, mapsPositions) {
var waypts = mapsPositions;
directionsService.route({
origin: origin,
destination: destination,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: 'DRIVING'
}, function (response, status) {
if (status === 'OK') {
directionsDisplay.setDirections(response);
var route = response.routes[0];
var summaryPanel = document.getElementById('directions-panel');
summaryPanel.innerHTML = '';
// For each route, display summary information.
for (var i = 0; i < route.legs.length; i++) {
var routeSegment = i + 1;
summaryPanel.innerHTML += '<b>Route Segment: ' + routeSegment +
'</b><br>';
summaryPanel.innerHTML += route.legs[i].start_address + ' to ';
summaryPanel.innerHTML += route.legs[i].end_address + '<br>';
summaryPanel.innerHTML += route.legs[i].distance.text + '<br><br>';
}
}
else {
window.alert('Directions request failed due to ' + status);
}
});
};
谷歌正在说:
[{"lat":54.1766472,"lng":9.092599,"stopover":false},{"lat":54.1700745,"lng":9.090693,"stopover":false},{"lat":54.1973152,"lng":9.045031,"stopover":false},{"lat":54.27517,"lng":9.00978851,"stopover":false},{"lat":54.4802246,"lng":9.087988,"stopover":false}]
现在我收到了错误: InvalidValueError:在属性waypoints中:在索引0处:未知属性lat。 如果财产是引号,这是否重要?或者我错过了什么? 谢谢你的帮助。
答案 0 :(得分:4)
属性waypoints
应包含具有此结构的对象数组:
{
location: {lat: .., lng: ..},
....
}
您当前的对象如下所示:
{
lat: ...,
lng: ...,
....
}
您可以在DOCUMENTATION
中找到有关此内容的更多信息