问题
I'm new to Google maps and I'm trying to set up a basic directions display. I basically do:
var Map = new google.maps.Map( Canvas[ 0 ],
{
center: { lat: -34.397, lng: 150.644 },
zoom: 8
} );
var DService = new google.maps.DirectionsService();
var DDisplay = new google.maps.DirectionsRenderer();
DDisplay.setMap( Map );
As soon as I do DDisplay.setMap( Map ); Chrome shows an error in the console.
The error is:
Uncaught TypeError: Cannot read property 'stack' of undefined
I have tried including the script using the <script async defer>
with a callback, using JQuery $.getScript and currently I have it in the head section of the web page with no callback, all give the same error.
The full code follows (Please ignore the pre-processor commands):
<div data-googleMap {if $ID}id="{$ID}"{/if}>
<h3 class="ui-widget-header">{TEXT}Map{/TEXT}</h3>
<div data-googleMapCanvas></div>
</div>
<script>
$( document ).ready( function()
{
'use strict';
// {if $ID}
var GoogleMap = $( '#{$ID}' );
// {else}
var GoogleMap = $( 'div[data-googleMap]' );
// {/if}
var Map = null;
var DService = null;
var DDisplay = null;
GoogleMap.resizable( { animate: true, animateDuration: 'fast' } ).on( 'resizestop', function()
{
setTimeout( function()
{
var Center = Map.getCenter();
google.maps.event.trigger( Map, 'resize' );
Map.setCenter( Center );
}, 500 );
} );
GoogleMap.draggable( { handle:'h3', cursor: "move" } );
var Canvas = GoogleMap.find( 'div[data-googleMapCanvas]' );
Map = new google.maps.Map( Canvas[ 0 ],
{
center: { lat: -34.397, lng: 150.644 },
zoom: 8
} );
DService = new google.maps.DirectionsService();
DDisplay = new google.maps.DirectionsRenderer();
DDisplay.setMap( Map );
});
</script>
PS: It makes not difference if I use the JQuery UI functions or not. Also I have tried changing the version of Google Maps API, it also make no difference.
Terry
来源:https://stackoverflow.com/questions/33162062/google-maps-uncaught-typeerror-cannot-read-property-stack-of-undefined