2017-12-20 3 views
0

現在、マップapiとasp.net mvc5の組み合わせで作業しています。 私のコントローラは、このように私の見解に位置データを渡している。Googleマップのウェイポイント:InvalidValueError:プロパティウェイポイント:インデックス0:不明プロパティラフ

ViewBag.Origin = JsonConvert.SerializeObject(origin); 
ViewBag.Destination = JsonConvert.SerializeObject(destination); 
ViewBag.PositionEntries = JsonConvert.SerializeObject(mapsPositions); 

私は私のjsファイルを呼び出し、それにViewbagsの値を渡していビュー内。

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

ジャバスクリプトファイル:

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); 
    } 
}); 
}; 

JSONオブジェクト:

[{"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}] 

だからGoogleは言っている:

// The below line is equivalent to writing: 
// position: new google.maps.LatLng(-34.397, 150.644) 
position: {lat: -34.397, lng: 150.644}, 

は今、私はエラーを取得しています: InvalidValueError:in prop ertyウェイポイント:インデックス0:未知のプロパティlat。 プロパティが引用符で囲まれていてもかまいませんか?または私は何かを逃していますか? ご協力いただきありがとうございます。

答えて

1

プロパティwaypointsは、このような構造を持つオブジェクトの配列が含まれている必要があります。

{ 
    location: {lat: .., lng: ..}, 
    .... 
} 

は、あなたの現在のオブジェクトは、次のようになります。

{ 
    lat: ..., 
    lng: ..., 
    .... 
} 

あなたはDOCUMENTATION

にこのについての詳細を調べることができます
+0

Ah。ありがとうございました。時々、木々でいっぱいの森を見ることができません。 – ad0R

+0

@ ad0Rはい、この場合のエラーメッセージは役に立ちません。 – Titus

関連する問題