javascript google.maps.geometry.sphereal 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15226053/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
google.maps.geometry.spherical error
提问by Reven
I am having some troubles using the geometry at google maps. I am getting the following error: "Unable to get property 'spherical' of undefined or null reference."
我在使用谷歌地图的几何图形时遇到了一些麻烦。我收到以下错误:“无法获得未定义或空引用的属性‘球状’。”
This is some code from w3c with some additions of mine.
这是来自 w3c 的一些代码,并添加了我的一些内容。
var x=new google.maps.LatLng(52.395715,4.888916);
var stavanger=new google.maps.LatLng(58.983991,5.734863);
var amsterdam=new google.maps.LatLng(52.395715,4.888916);
var london=new google.maps.LatLng(51.508742,-0.120850);
function initialize()
{
var mapProp = {
center:x,
zoom:4,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map=new google.maps.Map(document.getElementById("googleMap"),mapProp);
var myTrip=[stavanger,amsterdam,london];
var flightPath=new google.maps.Polyline({
path:myTrip,
strokeColor:"#0000FF",
strokeOpacity:0.8,
strokeWeight:2
});
flightPath.setMap(map);
try{
var markerpos = google.maps.geometry.spherical.interpolate(flightPath.getAt(0), flightPath.getAt(1), .5);
// also tried //
//var Spherical = google.maps.geometry.spherical;
//var markerpos = Spherical.interpolate(flightPath.getAt(0), flightPath.getAt(1), .5);
//var markerpos = google.maps.geometry.spherical.interpolate(amsterdam, london, .5);
}
catch(ex){alert(ex);}
var marker = new google.maps.Marker({position: markerpos,
map: map, clickable: false
});
}
What is wrong with the above code?
上面的代码有什么问题?
回答by Marcelo
Are you loading the correct librarywith the libraries=
parameter?
<script src="https://maps.googleapis.com/maps/api/js?v=3&sensor=false&libraries=geometry"></script>
回答by Tarak
First you need to import libraries modules and then auth key module. I think here the problem is at the time of executing library functions importing library modules is not completed. If you import auth key module first what happened is immediately it calls the initialize() method by the time library modules loading is not completed. So you need to import library modules first and then auth module.
首先,您需要导入库模块,然后导入 auth 密钥模块。我觉得这里的问题是在执行库函数的时候导入库模块没有完成。如果首先导入 auth 密钥模块,它会立即在库模块加载未完成时调用 initialize() 方法。所以你需要先导入库模块,然后再导入 auth 模块。